数据库连接池分析

参考文献

1.一个效果非常不错的 JAVA 数据库连接池

2.使用 JAVA 中的动态代理实现数据库连接池

3.MySql 与 oracle 的 JDBC 测试程序

分析

参考文献 1 是一个用 java 实现的数据库连接池,可以参考其代码实现,最好也看看 java 源代码是怎么实现数据库连接池的,两者进行比较。

参考文献 2 中提到了动态代理,之前写过的一片博客:设计模式之代理模式之二 (Proxy)中也讲到了动态代理,这里可以参考一下。

下面首先给出参考 1 中的代码示例,具体见 ConnectionPool.java,修改了部门代码与注释,并该处了一个测试 demo,具体见 ConnectionPoolDemo.java:

ConnectionPool.java

View Code
package edu.sjtu.erplab.connectionpool;

//http://hi.baidu.com/guowei4634/blog/item/bb16c85c66e90a47faf2c076.html
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 {

</span><span style="color: rgba(0, 0, 255, 1)">private</span> String jdbcDriver = ""; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库驱动</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> String dbUrl = ""; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据 URL</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> String dbUsername = ""; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库用户名</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> String dbPassword = ""; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库用户密码</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> String testTable = ""; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 测试连接是否可用的测试表名,默认没有测试表</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> initialConnections = 10; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接池的初始大小</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> incrementalConnections = 5;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接池自动增加的大小</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> maxConnections = 50; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接池最大的大小</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> Vector connections = <span style="color: rgba(0, 0, 255, 1)">null</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 存放连接池中数据库连接的向量 , 初始时为 null, 它中存放的对象为 PooledConnection 型</span>

<span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 构造函数
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jdbcDriver
 *            String JDBC 驱动类串
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dbUrl
 *            String 数据库 URL
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dbUsername
 *            String 连接数据库用户名
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dbPassword
 *            String 连接数据库用户的密码      
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ConnectionPool(String jdbcDriver, String dbUrl, String dbUsername,
        String dbPassword) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.jdbcDriver =<span style="color: rgba(0, 0, 0, 1)"> jdbcDriver;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dbUrl =<span style="color: rgba(0, 0, 0, 1)"> dbUrl;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dbUsername =<span style="color: rgba(0, 0, 0, 1)"> dbUsername;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dbPassword =<span style="color: rgba(0, 0, 0, 1)"> dbPassword;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 返回连接池的初始大小
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 初始连接池中可获得的连接数量
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getInitialConnections() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.initialConnections;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 设置连接池的初始大小
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 用于设置初始连接池中连接的数量
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setInitialConnections(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> initialConnections) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.initialConnections =<span style="color: rgba(0, 0, 0, 1)"> initialConnections;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 返回连接池自动增加的大小 、
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 连接池自动增加的大小
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getIncrementalConnections() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.incrementalConnections;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 设置连接池自动增加的大小
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 连接池自动增加的大小
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setIncrementalConnections(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> incrementalConnections) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.incrementalConnections =<span style="color: rgba(0, 0, 0, 1)"> incrementalConnections;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 返回连接池中最大的可用连接数量
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 连接池中最大的可用连接数量
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getMaxConnections() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.maxConnections;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 设置连接池中最大可用的连接数量
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 设置连接池中最大可用的连接数量值
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setMaxConnections(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> maxConnections) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.maxConnections =<span style="color: rgba(0, 0, 0, 1)"> maxConnections;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取测试数据库表的名字
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 测试数据库表的名字
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getTestTable() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.testTable;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 设置测试表的名字
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> testTable
 *            String 测试表的名字
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setTestTable(String testTable) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.testTable =<span style="color: rgba(0, 0, 0, 1)"> testTable;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 创建一个数据库连接池,连接池中的可用连接的数量采用类成员
 * initialConnections 中设置的值
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">void</span> createPool() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 确保连接池没有创建
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果连接池己经创建了,保存连接的向量 connections 不会为空</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (connections != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果己经创建,则返回</span>

}
// 实例化 JDBC Driver 中指定的驱动类实例
Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance());
DriverManager.registerDriver(driver);
// 注册 JDBC 驱动程序
// 创建保存连接的向量 , 初始时有 0 个元素
connections = new Vector();
// 根据 initialConnections 中设置的值,创建连接。
createConnections(this.initialConnections);
System.out.println(
"createPool: 数据库连接池创建成功!");
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 创建由 numConnections 指定数目的数据库连接 , 并把这些连接
 * 放入 connections 向量中
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> numConnections
 *            要创建的数据库连接的数目
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">

@SuppressWarnings(</span>"unchecked"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> createConnections(<span style="color: rgba(0, 0, 255, 1)">int</span> numConnections) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 循环创建指定数目的数据库连接</span>
    <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> x = 0; x &lt; numConnections; x++<span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否连接池中的数据库连接的数量己经达到最大?最大值由类成员 maxConnections
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 指出,如果 maxConnections 为 0 或负数,表示连接数量没有限制。
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果连接数己经达到最大,即退出。</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.maxConnections &gt; 0
                &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">this</span>.connections.size() &gt;= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.maxConnections) {
            </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> add a new PooledConnection object to connections vector
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 增加一个连接到连接池中(向量 connections 中)</span>
        <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            connections.addElement(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PooledConnection(newConnection()));
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
            System.out.println(</span>"createConnections:创建数据库连接失败! " +<span style="color: rgba(0, 0, 0, 1)"> e.getMessage());
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SQLException();
        }
        System.out.println(</span>"createConnections:数据库连接己创建 ......"<span style="color: rgba(0, 0, 0, 1)">);
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 创建一个新的数据库连接并返回它
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 返回一个新创建的数据库连接
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">private</span> Connection newConnection() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个数据库连接</span>
    Connection conn =<span style="color: rgba(0, 0, 0, 1)"> DriverManager.getConnection(dbUrl, dbUsername,
            dbPassword);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果这是第一次创建数据库连接,即检查数据库,获得此数据库允许支持的
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最大客户连接数目
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> connections.size()==0 表示目前没有连接己被创建</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (connections.size() == 0<span style="color: rgba(0, 0, 0, 1)">) {
        DatabaseMetaData metaData </span>=<span style="color: rgba(0, 0, 0, 1)"> conn.getMetaData();
        </span><span style="color: rgba(0, 0, 255, 1)">int</span> driverMaxConnections =<span style="color: rgba(0, 0, 0, 1)"> metaData.getMaxConnections();
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库返回的 driverMaxConnections 若为 0 ,表示此数据库没有最大
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接限制,或数据库的最大连接限制不知道
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> driverMaxConnections 为返回的一个整数,表示此数据库允许客户连接的数目
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果连接池中设置的最大连接数量大于数据库允许的连接数目 , 则置连接池的最大
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接数目为数据库允许的最大数目</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span> (driverMaxConnections &gt; 0
                &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">this</span>.maxConnections &gt;<span style="color: rgba(0, 0, 0, 1)"> driverMaxConnections) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.maxConnections =<span style="color: rgba(0, 0, 0, 1)"> driverMaxConnections;
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> conn; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回创建的新的数据库连接</span>

}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 
 * 通过调用 getFreeConnection() 函数返回一个可用的数据库连接 ,
 * 如果当前没有可用的数据库连接,并且更多的数据库连接不能创
 * 建(如连接池大小的限制),此函数等待一会再尝试获取。
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 返回一个可用的数据库连接对象
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> Connection getConnection() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 确保连接池己被创建</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (connections == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接池还没创建,则返回 null</span>

}
Connection conn
= getFreeConnection(); // 获得一个可用的数据库连接
// 如果目前没有可以使用的连接,即所有的连接都在使用中
while (conn == null) {
// 等一会再试
wait(250);
conn
= getFreeConnection(); // 重新再试,直到获得可用的连接,如果
// getFreeConnection() 返回的为 null
// 则表明创建一批连接后也不可获得可用连接
}
return conn;// 返回获得的可用的连接
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 本函数从连接池向量 connections 中返回一个可用的的数据库连接,如果
 * 当前没有可用的数据库连接,本函数则根据 incrementalConnections 设置
 * 的值创建几个数据库连接,并放入连接池中。
 * 如果创建后,所有的连接仍都在使用中,则返回 null
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 返回一个可用的数据库连接
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">private</span> Connection getFreeConnection() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从连接池中获得一个可用的数据库连接</span>
    Connection conn =<span style="color: rgba(0, 0, 0, 1)"> findFreeConnection();
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (conn == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果目前连接池中没有可用的连接
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一些连接</span>

createConnections(incrementalConnections);
// 重新从池中查找是否有可用连接
conn = findFreeConnection();
if (conn == null) {
// 如果创建连接后仍获得不到可用的连接,则返回 null
return null;
}
}
return conn;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 查找连接池中所有的连接,查找一个可用的数据库连接,
 * 如果没有可用的连接,返回 null
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 返回一个可用的数据库连接
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">private</span> Connection findFreeConnection() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {
    Connection conn </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    PooledConnection pConn </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得连接池向量中所有的对象</span>
    Enumeration enumerate =<span style="color: rgba(0, 0, 0, 1)"> connections.elements();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历所有的对象,看是否有可用的连接</span>
    <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (enumerate.hasMoreElements()) {
        pConn </span>=<span style="color: rgba(0, 0, 0, 1)"> (PooledConnection) enumerate.nextElement();
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">pConn.isBusy()) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果此对象不忙,则获得它的数据库连接并把它设为忙</span>
            conn =<span style="color: rgba(0, 0, 0, 1)"> pConn.getConnection();
            pConn.setBusy(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 测试此连接是否可用</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">testConnection(conn)) {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果此连接不可再用了,则创建一个新的连接,
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 并替换此不可用的连接对象,如果创建失败,返回 null</span>
                <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                    conn </span>=<span style="color: rgba(0, 0, 0, 1)"> newConnection();
                } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
                    System.out.println(</span>"findFreeConnection:创建数据库连接失败! " +<span style="color: rgba(0, 0, 0, 1)"> e.getMessage());
                    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
                }
                pConn.setConnection(conn);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">break</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 己经找到一个可用的连接,退出</span>

}
}
return conn;// 返回找到到的可用连接
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 测试一个连接是否可用,如果不可用,关掉它并返回 false
 * 否则可用返回 true
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> conn
 *            需要测试的数据库连接
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 返回 true 表示此连接可用, false 表示不可用
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> testConnection(Connection conn) {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断测试表是否存在</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span> (testTable.equals(""<span style="color: rgba(0, 0, 0, 1)">)) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果测试表为空,试着使用此连接的 setAutoCommit() 方法
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 来判断连接否可用(此方法只在部分数据库可用,如果不可用 ,
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 抛出异常)。注意:使用测试表的方法更可靠</span>
            conn.setAutoCommit(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
        } </span><span style="color: rgba(0, 0, 255, 1)">else</span> {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 有测试表的时候使用测试表测试
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> check if this connection is valid</span>
            Statement stmt =<span style="color: rgba(0, 0, 0, 1)"> conn.createStatement();
            stmt.execute(</span>"select count(*) from " +<span style="color: rgba(0, 0, 0, 1)"> testTable);
        }
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 上面抛出异常,此连接己不可用,关闭它,并返回 false;</span>

closeConnection(conn);
return false;
}
// 连接可用,返回 true
return true;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 此函数返回一个数据库连接到连接池中,并把此连接置为空闲。
 * 所有使用连接池获得的数据库连接均应在不使用此连接时返回它。
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 需返回到连接池中的连接对象
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> returnConnection(Connection conn) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 确保连接池存在,如果连接没有创建(不存在),直接返回</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (connections == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        System.out.println(</span>"returnConnection:连接池不存在,无法返回此连接到连接池中 !"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    PooledConnection pConn </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    Enumeration enumerate </span>=<span style="color: rgba(0, 0, 0, 1)"> connections.elements();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历连接池中的所有连接,找到这个要返回的连接对象</span>
    <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (enumerate.hasMoreElements()) {
        pConn </span>=<span style="color: rgba(0, 0, 0, 1)"> (PooledConnection) enumerate.nextElement();
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 先找到连接池中的要返回的连接对象</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span> (conn ==<span style="color: rgba(0, 0, 0, 1)"> pConn.getConnection()) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 找到了 , 设置此连接为空闲状态</span>
            pConn.setBusy(<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
        }
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 刷新连接池中所有的连接对象
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">void</span> refreshConnections() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 确保连接池己创新存在</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (connections == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        System.out.println(</span>"refreshConnections:连接池不存在,无法刷新 !"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    PooledConnection pConn </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    Enumeration enumerate </span>=<span style="color: rgba(0, 0, 0, 1)"> connections.elements();
    </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (enumerate.hasMoreElements()) {
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得一个连接对象</span>
        pConn =<span style="color: rgba(0, 0, 0, 1)"> (PooledConnection) enumerate.nextElement();
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果对象忙则等 5 秒 ,5 秒后直接刷新</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (pConn.isBusy()) {
            wait(</span>5000); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 等 5 秒</span>

}
// 关闭此连接,用一个新的连接代替它。
closeConnection(pConn.getConnection());
pConn.setConnection(newConnection());
pConn.setBusy(
false);
}
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 关闭连接池中所有的连接,并清空连接池。
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">void</span> closeConnectionPool() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SQLException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 确保连接池存在,如果不存在,返回</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (connections == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
        System.out.println(</span>"closeConnectionPool:连接池不存在,无法关闭 !"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    PooledConnection pConn </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    Enumeration enumerate </span>=<span style="color: rgba(0, 0, 0, 1)"> connections.elements(); 
    </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (enumerate.hasMoreElements()) {
        pConn </span>=<span style="color: rgba(0, 0, 0, 1)"> (PooledConnection) enumerate.nextElement();
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果忙,等 5 秒</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (pConn.isBusy()) {
            wait(</span>5000); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 等 5 秒</span>

}
// 5 秒后直接关闭它
closeConnection(pConn.getConnection());
// 从连接池向量中删除它,vector 类型
connections.removeElement(pConn);
}
// 置连接池为空
connections = null;
System.out.println(
"createPool: 数据库连接池关闭成功!");
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 关闭一个数据库连接
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 需要关闭的数据库连接
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> closeConnection(Connection conn) {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        conn.close();
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
        System.out.println(</span>"closeConnection:关闭数据库连接出错: " +<span style="color: rgba(0, 0, 0, 1)"> e.getMessage());
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 使程序等待给定的毫秒数
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 给定的毫秒数
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> wait(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> mSeconds) {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Thread.sleep(mSeconds);
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) {
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 内部类
 * 用于保存连接池中连接对象
 * 此类中有两个成员,一个是数据库的连接,另一个是指示此连接是否正在使用的标志。
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> PooledConnection {
    Connection connection </span>= <span style="color: rgba(0, 0, 255, 1)">null</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库连接</span>
    <span style="color: rgba(0, 0, 255, 1)">boolean</span> busy = <span style="color: rgba(0, 0, 255, 1)">false</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 此连接是否正在使用的标志,默认没有正在使用</span>
    <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * 构造函数
     * 根据一个 Connection 构告一个 PooledConnection 对象,两者一一对应。
     * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> 数据连接
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> PooledConnection(Connection connection) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.connection =<span style="color: rgba(0, 0, 0, 1)"> connection;
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">getter and setter
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回此对象中的连接</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Connection getConnection() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> connection;
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置此对象的,连接</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setConnection(Connection connection) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.connection =<span style="color: rgba(0, 0, 0, 1)"> connection;
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得对象连接是否忙</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isBusy() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> busy;
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置对象的连接正在忙</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setBusy(<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> busy) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.busy =<span style="color: rgba(0, 0, 0, 1)"> busy;
    }
}

}

ConnectionPoolDemo.java

View Code
package edu.sjtu.erplab.connectionpool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionPoolDemo {

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    String jdbcDriver </span>= "com.mysql.jdbc.Driver"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库驱动</span>
    String dbUrl = "jdbc:mysql://localhost:3306/test"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据 URL</span>
    String dbUsername = "root"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库用户名</span>
    String dbPassword = "123456"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数据库用户密码</span>
    ConnectionPool cp = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ConnectionPool(jdbcDriver, dbUrl, dbUsername,
            dbPassword);
    cp.setTestTable(</span>"emp"<span style="color: rgba(0, 0, 0, 1)">);
    
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        cp.createPool();
        Connection conn </span>=<span style="color: rgba(0, 0, 0, 1)"> cp.getConnection();
        Statement stmt </span>=<span style="color: rgba(0, 0, 0, 1)"> conn.createStatement();
        ResultSet rs </span>= stmt.executeQuery("select * from userinfo"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (rs.next()) {
            System.out.println(rs.getString(</span>"username"<span style="color: rgba(0, 0, 0, 1)">));
        }

    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
        e.printStackTrace();
    }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">{
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            cp.closeConnectionPool();
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

运行结果如下:

View Code
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createConnections: 数据库连接己创建 ......
createPool: 数据库连接池创建成功! 
zhangsan
lisi
createPool: 数据库连接池关闭成功! 

ConnectionPool 的类图结构如下图所示:

参考之前写 singleton 的时候,不适用同步方法,而是使用二次加锁的方式来使用 synchronized。考虑此处是否可以使用二次加锁。

在 synchronized 代码块中加锁是对整个对象加锁了,扩大了锁的粒度。是否是现在的对方法加锁更优。