1、; String CLOSE = close String PREPARESTATEMENT = prepareStatement String COMMIT = commit String ROLLBACK = rollbakc /* * 构造函数,采用私有,防止被直接创建 * param param * 连接参数 private ConnectionProxy(ConnectionParam param) / 记录日志 try / 创建连接 Class.forName(oracle.jdbc.driver.OracleDriver); conn = DriverManager.getCon
2、nection(jdbc:oracle:thin:10.8.1.234:1521:WF4PPDB,PP42, DatabaseMetaData dm = null; dm = conn.getMetaData(); / 判断是否支持事务 supportTransaction = dm.supportsTransactions(); catch (Exception ex) ex.printStackTrace(); public Object invoke(Object proxy, Method method, Object args) throws Throwable Object obj
3、 = null; / 判断是否调用了close的方法,如果调用close方法则把连接置为无用状态 if (CLOSE.equals(method.getName() / 设置不使用标志 setFree(false); if (statRef != null) statRef.close(); if (preStatRef ! preStatRef.close(); return null; / 判断是使用了createStatement语句 if (CREATESTATE.equals(method.getName() obj = method.invoke(conn, args); stat
4、Ref = (Statement) obj; return obj; catch (ClassCastException ex) / 判断是使用了prepareStatement语句 if (PREPARESTATEMENT.equals(method.getName() preStatRef = (PreparedStatement) obj; / 如果不支持事务,就不执行该事物的代码 if (COMMIT.equals(method.getName() | ROLLBACK.equals(method .getName() & (!isSupportTransaction() / 设置最后
5、一次访问时间,以便及时清除超时的连接 lastAccessTime = System.currentTimeMillis(); * 创建连接的工厂,只能让工厂调用 * param factory * 要调用工厂,并且一定被正确初始化 * return 连接 public static ConnectionProxy getConnection(ConnectionFactory factory, ConnectionParam param) / 判断是否正确初始化的工厂 if (factory.isCreate() ConnectionProxy _conn = new ConnectionP
6、roxy(param); return _conn; else public Connection getFreeConnection() / 返回数据库连接conn的接管类,以便截住close方法 Connection cn = (Connection) Proxy.newProxyInstance(conn.getClass() .getClassLoader(), conn.getClass().getInterfaces(), this); return cn; * 该方法真正的关闭了数据库的连接 * throws SQLException public void close() th
7、rows SQLException / 由于类属性conn是没有被接管的连接,因此一旦调用close方法后就直接关闭连接 conn.close(); public void setFree(boolean isFree) this.isFree = isFree; public boolean isFree() return isFree; * 判断是否支持事务 * return boolean public boolean isSupportTransaction() return supportTransaction; import java.sql.Connection;import j
8、ava.sql.SQLException;import java.util.*;public class ConnectionFactory private static ConnectionFactory m_instance = null; / 在使用的连接池 private LinkedHashSet ConnectionPool = null; / 空闲连接池 private LinkedHashSet FreeConnectionPool = null; / 最大连接数 private int MaxConnectionCount = 10; / 最小连接数S private int
9、 MinConnectionCount = 2; / 当前连接数 private int current_conn_count = 0; / 连接参数 private ConnectionParam connparam = null; / 是否创建工厂的标志 private boolean isflag = false; / 是否支持事务 / 定义管理策略 private int ManageType = 0; * 构造器 private ConnectionFactory() ConnectionPool = new LinkedHashSet(); FreeConnectionPool =
10、 new LinkedHashSet(); * 使用指定的参数创建一个连接池 public ConnectionFactory(ConnectionParam param, FactoryParam fparam) throws SQLException if (param = null) | (fparam = null) throw new SQLException(ConnectionParam和FactoryParam不能为空 if (m_instance = null) synchronized (ConnectionFactory.class) / 参数定制 m_instance
11、= new ConnectionFactory(); connparam = param; m_instance.MaxConnectionCount = fparam .getMaxConnectionCount(); m_instance.MinConnectionCount = fparam .getMinConnectionCount(); m_instance.ManageType = fparam.getManageType(); m_instance.isflag = true; / 初始化,创建MinConnectionCount个连接 System.out.println(c
12、onnection factory 创建! for (int i = 0; i m_instance.MinConnectionCount; i+) ConnectionProxy conn = ConnectionProxy .getConnection(m_instance,m_instance.connparam); if (conn = null) continue;connection创建 m_instance.FreeConnectionPool.add(conn); / 加入空闲连接池 m_instance.current_conn_count+; / 标志是否支持事务 m_in
13、stance.supportTransaction = conn .isSupportTransaction(); / 根据策略判断是否需要查询 if (m_instance.ManageType != 0) Thread t = new Thread(new FactoryManageThread( m_instance); t.start(); * 标志工厂是否已经创建 public boolean isCreate() return m_instance.isflag; * 从连接池中取一个空闲的连接 * return Connection public synchronized Con
14、nection getFreeConnection() throws SQLException Connection cn = null; / 获取空闲连接 Iterator ir = m_instance.FreeConnectionPool.iterator(); while (ir.hasNext() ConnectionProxy conn = (ConnectionProxy) ir.next(); / 找到未用的连接 if (!conn.isFree() cn = conn.getFreeConnection(); conn.setFree(true); / 移出空闲区 m_ins
15、tance.FreeConnectionPool.remove(conn); / 加入连接池 m_instance.ConnectionPool.add(conn); break; / 检查空闲池是否为空 if (m_instance.FreeConnectionPool.isEmpty() / 再检查是否能够分配 if (m_instance.current_conn_count = m_instance.MinConnectionCount) newCount = m_instance.MinConnectionCount; newCount = m_instance.MaxConnect
16、ionCount - m_instance.current_conn_count; newCount; ConnectionProxy _conn = ConnectionProxy.getConnection( m_instance, connparam); m_instance.FreeConnectionPool.add(_conn); / 如果不能新建,检查是否有已经归还的连接 ir = m_instance.ConnectionPool.iterator(); ConnectionProxy _conn = (ConnectionProxy) ir.next();_conn.isFr
17、ee() cn = _conn.getFreeConnection(); _conn.setFree(false); m_instance.ConnectionPool.remove(_conn); / 再次检查是否能分配连接 if (cn = null) ir = m_instance.FreeConnectionPool.iterator(); _conn.setFree(true); m_instance.FreeConnectionPool.remove(_conn); m_instance.ConnectionPool.add(_conn); if (cn = null) / 如果不
18、能则说明无连接可用 没有可用的数据库连接Get Connection * 关闭该连接池中的所有数据库连接 public synchronized void close() throws SQLException this.isflag = false; SQLException sqlError = null; / 关闭空闲池 (ConnectionProxy) ir.next().close();Close connection:Free m_instance.current_conn_count-; if (ex instanceof SQLException) sqlError = (SQLException) ex; / 关闭在使用的连接池 Using if (sqlError ! throw sqlError; * 返回是否支持事务 return m_instance.supportTransaction; * 连接池调度管理 public void schedule() Iterator ir = null; / 检查是否有已经归还的连接 if (m_instance.MaxConnectionCount - m_instance.current_conn_count m_instance.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1