Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attempt to reconnect jdbc pool datasource after database restarts

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?

like image 515
egerardus Avatar asked Jul 02 '12 21:07

egerardus


3 Answers

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"
            />
like image 179
Natan Cox Avatar answered Nov 08 '22 14:11

Natan Cox


While checking for the same issue I came across this post which has the auto connect configurations for all app servers.

Below are the configuration which I used for auto connect in tomcat for reference.

    <Resource auth="Container"
driverClassName="oracle.jdbc.OracleDriver"
initialSize="5"
maxActive="120"
maxIdle="5"
maxWait="5000"
name="jdbc/oracle/myds"
password="secret"
poolPreparedStatements="true"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:@DBHOSTNAME:1521/ServiceName"
username="testuser"
validationQuery="select 1 from tab"
testOnBorrow="true"/>

Complete auto connect configurations for all app servers can be found here in Datasource autoreconnect in Java Application Servers.

like image 43
user1472187 Avatar answered Nov 08 '22 13:11

user1472187


Just to add on to Natan Cox's answer

Reference - http://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html#Common_Attributes

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

As against Geronimo, I would still like to use validationQuery

Database validationQuery notes

hsqldb - select 1 from INFORMATION_SCHEMA.SYSTEM_USERS

Oracle - select 1 from dual

DB2 - select 1 from sysibm.sysdummy1

mysql - select 1

microsoft SQL Server - select 1

postgresql - select 1

ingres - select 1

derby - values 1

H2 - select 1

Firebird - select 1 from rdb$database

Reference - DBCP - validationQuery for different Databases

like image 1
Indu Devanath Avatar answered Nov 08 '22 12:11

Indu Devanath