读书人

dbcp怎么设置连接超时

发布时间: 2012-01-21 21:31:43 作者: rapoo

dbcp如何设置连接超时?
这个是一个获取DataSource的例子程序,请问如何设置超时呢?
public static DataSource setupDataSource(String connectURI) {
//
// First, we 'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We 'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool connectionPool = new GenericObjectPool(null);

//
// Next, we 'll create a ConnectionFactory that the
// pool will use to create Connections.
// We 'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);

//
// Now we 'll create the PoolableConnectionFactory, which wraps
// the "real " Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);

//
// Finally, we create the PoolingDriver itself,
// passing in the object pool we created.
//
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

return dataSource;
}

[解决办法]
设置数据源的removeAbandoned= "true ",removeAbandonedTimeout= "60 ",logAbandoned= "true "几个属性就可以了。
DBCP会自动把超过timeout时间仍未关闭的连接强制关闭,并且打出异常信息(包含打开连接的代码位置)。


[解决办法]

读书人网 >J2SE开发

热点推荐