问题描述
我有一个Runnable,它可以从连接池中获取连接,如下所示,并且有60秒的时间可以对连接进行处理:
I've got a Runnable which gets a connection from a connection pool as below and has 60 seconds to do something with the connection:
private static ConnectionPoolDataSource cpds; // MysqlConnectionPoolDataSource
public void run(){
while((System.currentTimeMillis()-created)<60000){
try(Connection conn = cpds.getPooledConnection().getConnection()){
//do something
}catch(SQLException sqle){}
}
}
当线程在60s之后死掉时,我假定连接已返回到池中,并且当创建新线程时,可以重新使用该连接.但是,当我列出我的网络连接时,随着创建更多线程,该列表会不断增长.如上创建的连接是否正确返回到池中,如果可以,我如何强制重新使用连接?
When the thread dies after 60s, i've assumed the connection is returned to the pool and when a new thread is created the connection can be re-used. But when I list my network connections, the list keeps growing as more threads are created. Are connections created as above being returned to the pool correctly and if so how can I force the connections to be re-used ?
推荐答案
您实际上并没有使用连接池. ConnectionPoolDataSource
不能直接使用.它用作PooledConnection
对象的(特殊)DataSource
,然后由提供连接池的(正常)DataSource
实现保留在连接池中.
You are not actually using a connection pool. A ConnectionPoolDataSource
isn't intended to be used directly. It is intended as a (special) DataSource
for PooledConnection
objects which are then kept in a connection pool by a (normal) DataSource
implementation that provides connection pooling.
普通开发人员不应直接使用ConnectionPoolDataSource
,它应与Application Server提供的连接池一起使用,或包装在提供连接池的通用DataSource
中.
A normal developer should not use a ConnectionPoolDataSource
directly, it is intended for use with connection pools provided by Application Servers, or to be wrapped into general purpose DataSource
s that provided connection pooling.
从连接池中请求Connection
时,它将检出现有的PooledConnection
(或从其ConnectionPoolDataSource
请求一个新的PooledConnection
),检索一个Connection
并将其返回给用户.当用户关闭Connection
时,PooledConnection
将向连接池发出信号,通知它再次可用.
When a Connection
is requested from the connection pool, it will checkout an existing PooledConnection
(or request a new one from its ConnectionPoolDataSource
), retrieve a Connection
and return that to the user. When the user closes the Connection
, the PooledConnection
will signal the connection pool that it is available again.
在这种情况下,您要创建一个PooledConnection
,从中检索Connection
,然后丢弃PooledConnection
.这意味着PooledConnection
被放弃,并且它与数据库的物理连接无法重用,并且在最终被垃圾回收时将被关闭/丢弃(通常在连接池想要关闭物理连接时,它将调用在PooledConnection
上.
In this case you are creating a PooledConnection
, retrieving a Connection
from it and then discarding the PooledConnection
. This means that the PooledConnection
gets abandoned, and its physical connection to the database cannot be reused and will be closed/discarded when it is finally garbage collected (normally when the connection pool wants to close the physical connection, it will call close()
on the PooledConnection
).
您要么需要使用Application Server提供的连接池,要么使用诸如DBCP,c3p0或BoneCP之类的通用连接池.
You either need to use connection pooling as provided by your Application Server, or use a general purpose connection pool like DBCP, c3p0 or BoneCP.
这篇关于如何强制重用jdbc池中的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!