/* ======================================= 这个例子是根据POSTGRESQL数据库写的, 请用的时候根据实际的数据库调整。 调用方法如下: ① ConnectionPool connPool = new ConnectionPool("org.postgresql.Driver", "jdbc:postgresql://dbURI:5432/DBName", "postgre","postgre"); ② connPool .createPool(); Connection conn = connPool .getConnection(); */ /** 虽然现在用APACHE COMMONS DBCP可以非常方便的建立数据库连接池, 但是像这篇文章把数据库连接池的内部原理写的这么透彻,注视这么完整, 真是非常难得,让开发人员可以更深层次的理解数据库连接池,真是非常感 谢这篇文章的作者。 */ import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Enumeration; import java.util.Vector; public class ConnectionPool { private String jdbcDriver = ""; // 数据库驱动 private String dbUrl = ""; // 数据 URL private String dbUsername = ""; // 数据库用户名 private String dbPassword = ""; // 数据库用户密码 private String testTable = ""; // 测试连接是否可用的测试表名,默认没有测试表 private int initialConnections = 10; // 连接池的初始大小 private int incrementalConnections = 5; // 连接池自动增加的大小 private int maxConnections = 50; // 连接池最大的大小 private Vector connections = null; // 存放连接池中数据库连接的向量 , 初始时为 null // 它中存放的对象为 PooledConnection 型 /** * * 构造函数 * * @param jdbcDriver * String JDBC 驱动类串 * @param dbUrl * String 数据库 URL * @param dbUsername * String 连接数据库用户名 * @param dbPassword * String 连接数据库用户的密码 */ public ConnectionPool(String jdbcDriver, String dbUrl, String dbUsername, String dbPassword) { this.jdbcDriver = jdbcDriver; this.dbUrl = dbUrl; this.dbUsername = dbUsername; this.dbPassword = dbPassword; }
* * 此函数返回一个数据库连接到连接池中,并把此连接置为空闲。 * * 所有使用连接池获得的数据库连接均应在不使用此连接时返回它。 * * @param 需返回到连接池中的连接对象 */ public void returnConnection(Connection conn) { // 确保连接池存在,如果连接没有创建(不存在),直接返回 if (connections == null) { System.out.println(" 连接池不存在,无法返回此连接到连接池中 !"); return; } PooledConnection pConn = null; Enumeration enumerate = connections.elements(); // 遍历连接池中的所有连接,找到这个要返回的连接对象 while (enumerate.hasMoreElements()) { pConn = (PooledConnection) enumerate.nextElement(); // 先找到连接池中的要返回的连接对象 if (conn == pConn.getConnection()) { // 找到了 , 设置此连接为空闲状态 pConn.setBusy(false); break; } } } /** * * 刷新连接池中所有的连接对象 * * */ public synchronized void refreshConnections() throws SQLException { // 确保连接池己创新存在 if (connections == null) { System.out.println(" 连接池不存在,无法刷新 !"); return; } PooledConnection pConn = null; Enumeration enumerate = connections.elements(); while (enumerate.hasMoreElements()) { // 获得一个连接对象 pConn = (PooledConnection) enumerate.nextElement(); // 如果对象忙则等 5 秒 ,5 秒后直接刷新 if (pConn.isBusy()) { wait(5000); // 等 5 秒 } // 关闭此连接,用一个新的连接代替它。 closeConnection(pConn.getConnection()); pConn.setConnection(newConnection()); pConn.setBusy(false); } } /** * * 关闭连接池中所有的连接,并清空连接池。 */ public synchronized void closeConnectionPool() throws SQLException { // 确保连接池存在,如果不存在,返回 if (connections == null) { System.out.println(" 连接池不存在,无法关闭 !"); return; } PooledConnection pConn = null; Enumeration enumerate = connections.elements(); while (enumerate.hasMoreElements()) { pConn = (PooledConnection) enumerate.nextElement(); // 如果忙,等 5 秒 if (pConn.isBusy()) { wait(5000); // 等 5 秒 } // 5 秒后直接关闭它 closeConnection(pConn.getConnection()); // 从连接池向量中删除它 connections.removeElement(pConn); } // 置连接池为空 connections = null; } /** * * 关闭一个数据库连接 * * * * @param 需要关闭的数据库连接 */ private void closeConnection(Connection conn) { try { conn.close(); } catch (SQLException e) { System.out.println(" 关闭数据库连接出错: " + e.getMessage()); } } /** * * 使程序等待给定的毫秒数 * * * * @param 给定的毫秒数 */ private void wait(int mSeconds) { try { Thread.sleep(mSeconds); } catch (InterruptedException e) { } } /** * * * * 内部使用的用于保存连接池中连接对象的类 * * 此类中有两个成员,一个是数据库的连接,另一个是指示此连接是否 * * 正在使用的标志。 */ class PooledConnection { Connection connection = null;// 数据库连接 boolean busy = false; // 此连接是否正在使用的标志,默认没有正在使用 // 构造函数,根据一个 Connection 构告一个 PooledConnection 对象 public PooledConnection(Connection connection) { this.connection = connection; } // 返回此对象中的连接 public Connection getConnection() { return connection; } // 设置此对象的,连接 public void setConnection(Connection connection) { this.connection = connection; } // 获得对象连接是否忙 public boolean isBusy() { return busy; } // 设置对象的连接正在忙 public void setBusy(boolean busy) { this.busy = busy; } } }
|
数据库连接池
最新推荐文章于 2024-11-17 17:19:50 发布