// ...
// ① 创建一个GenericObjectPool类的实例。
GenericObjectPool pool = new GenericObjectPool(null);
// ...
// ② 在前面讨论Pool组件时提到GenericObjectPool
// 要求有一个PoolableObjectFactory来创建需
// 要缓冲的Object的实例,对于DBCP来说,
// 这一功能现在由PoolableConnectionFactory提
// 供,如下面的例子所示:
DriverManagerConnectionFactory cf =
new DriverManagerConnectionFactory(
"jdbc:mysql://host/db", "username", "password");
PoolableConnectionFactory pcf =
new PoolableConnectionFactory(
CF, pool, null, "SELECT * FROM mysql.db", false, true);
// ...
// ③ 现在,我们只要创建并注册PoolingDriver:
new PoolingDriver().registerPool("myPool", pool); |