How can I avoid a java.sql.SQLException due to I/O Error: Connection reset?

James picture James · Feb 13, 2013 · Viewed 7k times · Source

After a SQL Server database restart, my Tomcat 6 Spring web applications receives an SQL exception on the first attempt to query the database. Note: Tomcat 6 has continued to run during the SQL Server restart and I'm using myBatis. The exception is as follows:

org.springframework.dao.DataAccessResourceFailureException: Error querying database. Cause: java.sql.SQLException: I/O Error: Connection reset

Any subsequent queries execute fine (without exception). How can I prevent this exception on the first query?

Answer

Stéphane picture Stéphane · Feb 14, 2013

Set up your pool to test connections before giving them to your application

use testOnBorrow=true and (for instance) validationQuery="select 1" in your db pool connection