简体   繁体   English

如何从池中删除无效的数据库连接

[英]How to remove invalid database connection from pool

I am using connection pooling of tomcat with oracle database. 我正在使用tomcatoracle数据库的连接池。 It is working fine, but when i use my application after a long time it is giving error that " connection reset ". 它运行正常,但是当我长时间使用我的应用程序后,它会给出“ 连接重置 ”错误。 I am getting this error because of physical connection at oracle server closed before logical connection closed at tomcat datasource. 我收到此错误是因为在tomcat数据源关闭逻辑连接之前oracle服务器上的物理连接已关闭。 So before getting the connection from datasource i am checking the connection validity with isValid(0) method of connection object which gives false if the physical connection was closed. 因此,在从数据源获取连接之前,我使用连接对象的isValid(0)方法检查连接有效性,如果物理连接已关闭则返回false。 But i don't know how to remove that invalid connection object from the pool. 但我不知道如何从池中删除无效的连接对象。

This could be because on the db server, there is a timeout to not allow connections to live beyond a set time, or to die if it does not receive something saying it is still valid. 这可能是因为在数据库服务器上,超时不允许连接超过设定的时间,或者如果它没有收到说它仍然有效的信息就会死掉。 One way to fix this is to turn on keepalives. 解决此问题的一种方法是打开Keepalive。 These basically ping the db server saying that they are still valid connections. 这些基本上ping数据库服务器,说它们仍然是有效的连接。

This is a pretty good link on Tomcats DBCP configurations. 是关于Tomcats DBCP配置的一个非常好的链接。 Take a look at the section titled "Preventing dB connection pool leaks". 请查看标题为“防止dB连接池泄漏”的部分。 That looks like it may be a good place to start. 看起来这可能是一个好的起点。

I used validatationquery while configuring the datasource in server.xml file. 我在server.xml文件中配置数据源时使用了validatationquery It is going to check the validity of the connection by executing the query at database before giving to the application. 它将在给予应用程序之前通过在数据库中执行查询来检查连接的有效性。

for Oracle 对于Oracle

validationQuery="/* select 1 from dual */"

for MySql 对于MySql

validationQuery="/* ping */"

If we want to dispose an ill java.sql.connection from Tomcat jdbc connection pool, 如果我们想从Tomcat jdbc连接池中处理一个病态的java.sql.connection,

we may do this explicitly in the program. 我们可以在程序中明确地这样做。 Unwrap it into an org.apache.tomcat.jdbc.pool.PooledConnection, setDiscarded(true) and close the JDBC connection finally. 将其解包到org.apache.tomcat.jdbc.pool.PooledConnection,setDiscarded(true)并最终关闭JDBC连接。 The ConnectionPool will remove the underlying connection once it has been returned. ConnectionPool将在返回后删除基础连接。

(ConnectionPool.returnConnection(....)) (ConnectionPool.returnConnection(....))

eg PooledConnection pconn = conn.unwrap(PooledConnection.class); 例如,PooledConnection pconn = conn.unwrap(PooledConnection.class); pconn.setDiscarded(true); pconn.setDiscarded(真); conn.close(); conn.close();

Try closing it and opening it if it's invalid. 尝试关闭它并打开它,如果它无效。 I mean u would reinitialize it in this way so u won't need to remove it from the pool and reuse it. 我的意思是你会以这种方式重新初始化它,所以你不需要将它从池中删除并重用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM