📄 connectionpool.java
字号:
package com.jsc.database;import java.util.*;import java.sql.*;public class ConnectionPool { private String m_driver = new String(""); private String m_url = new String(""); private int m_size = 0; private String m_username = new String(""); private String m_password = new String(""); private Vector m_pool = null; public ConnectionPool() { } public void setDriver(String value) { if (value != null) { m_driver = value; } } public String getDriver() { return m_driver; } public void setURL(String value) { if (value != null) { m_url = value; } } public String getURL() { return m_url; } public void setSize(int value) { if (value > 1) { m_size = value; } } public int getSize() { return m_size; } public void setUsername(String value) { if (value != null) { m_username = value; } } public String getUserName() { return m_username; } public void setPassword(String value) { if (value != null) { m_password = value; } } public String getPassword() { return m_password; } private Connection createConnection() throws Exception { Connection _conn = null; _conn = DriverManager.getConnection(m_url, m_username, m_password); return _conn; } public synchronized void initializePool() throws Exception { if (m_driver == null) { throw new Exception("No Driver Name Specified!"); } if (m_url == null) { throw new Exception("No URL Specified!"); } if (m_size < 1) { throw new Exception("Pool size is less than 1!"); } try { Class.forName(m_driver); for (int x=0;x<m_size;x++) { System.err.println("# Opening JDBC Connection " + x); Connection _conn = createConnection(); if ( _conn != null ) { PooledConnection pc = new PooledConnection(_conn); addConnection(pc); } } } catch (SQLException e) { System.err.println(e.getMessage()); } catch (ClassNotFoundException e) { System.err.println(e.getMessage()); } catch (Exception e) { System.err.println(e.getMessage()); } } private void addConnection(PooledConnection value) { if (m_pool == null) { m_pool = new Vector(m_size); } m_pool.addElement(value); } public synchronized void releaseConnection(Connection conn) { for (int x=0;x<m_pool.size();x++) { PooledConnection pc = (PooledConnection)m_pool.elementAt(x); if (pc.getConnection() == conn ) { System.err.println("Releasing Connection " + x); pc.setInUse(false); break; } } } public synchronized Connection getConnection() throws Exception { PooledConnection pc = null; for (int x=0;x<m_pool.size();x++) { pc = (PooledConnection)m_pool.elementAt(x); if (pc.inUse() == false) { pc.setInUse(true); return pc.getConnection(); } } try { Connection _conn = createConnection(); pc = new PooledConnection(_conn); pc.setInUse(true); m_pool.addElement(pc); } catch (Exception e) { System.err.println(e.getMessage()); } return pc.getConnection(); } public synchronized void emptyPool() { for (int x=0;x<m_pool.size();x++) { System.err.println("Closing JDBC Connection " + x); PooledConnection pc = (PooledConnection)m_pool.elementAt(x); if (pc.inUse() == false) { pc.close(); } else { try { java.lang.Thread.sleep(30000); pc.close(); } catch (InterruptedException e) { System.err.println(e.getMessage()); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -