attempt to reconnect jdbc pool datasource after database restarts

egerardus picture egerardus · Jul 2, 2012 · Viewed 29.8k times · Source

I have a web-app with a Java back-end that uses Tomcat jdbc-pool for database connections. This works fine.

However I am trying to foolproof it before exporting it to other locations, and recently a scenario occurred where someone restarted the SQL Server database service but did not restart the Tomcat service. This caused a SQLException: java.sql.SQLException: I/O Error: Connection reset by peer: socket write error until I restarted Tomcat, forcing the jdbc-pool datasource to reconnect.

I looked for some kind of a configuration in the Tomcat jdbc-pool docs to tell the datasource to attempt to reconnect but I couldn't find anything.

Does anyone know if there is some kind of configuration for this or should I check this condition before each request?

Answer

Natan Cox picture Natan Cox · Jul 3, 2012

Not 100% sure if this is your problem but on http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency it says you can use testOnBorrow with a validationQuery.

<Resource type="javax.sql.DataSource"
            ...
            testOnBorrow="true"
            validationQuery="SELECT 1"
            removeAbandoned="true"
            />