Search code examples
javaoracletomcatconnection-pooling

How to remove invalid database connection from pool


I am using connection pooling of tomcat with oracle database. 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. 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. But i don't know how to remove that invalid connection object from the pool.


Solution

  • I used validatationquery while configuring the datasource in server.xml file. It is going to check the validity of the connection by executing the query at database before giving to the application.

    for Oracle

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

    for MySql

    validationQuery="/* ping */"