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时间仍未关闭的连接强制关闭,并且打出异常信息(包含打开连接的代码位置)。
------解决方案--------------------