Arun
Arun

Reputation: 2572

hibernate not able to reconnect to the mysql DB

In my spring-hibernate application, we are using org.apache.tomcat.jdbc.pool.DataSource for connection pooling. When we start the server, we are able to see the connection to the DB is established and after the mysql service is stopped the server starts throwing error saying that the connection is lost. When the mysql service is started again, should we have to restart the server to reestablish the connections to the DB? Cuz even after providing autoReconnect=true parameter, the application is not able to establish connection to the DB.

Upvotes: 2

Views: 1144

Answers (2)

Amit Sharma
Amit Sharma

Reputation: 6184

Try adding the following parameters :

validationQuery="SELECT 1"
testOnBorrow="true"

How it works: The connection-pool tries running validationQuery before returning the connection. If the validationQuesry fails, dbcp discard the connection, creates a new one and return it.

Here's an example:

<Resource   name="jdbc/cooldatabase"
            description="Strandls.com license database"
            auth="Container"
            type="javax.sql.DataSource"
            factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/cooldatabase?autoReconnect=true"
            username="cooluser"
            password="coolpassword"
            initialSize="0"
            maxActive="20"
            maxIdle="10"
            minIdle="0"
            maxWait="-1"
            validationQuery="SELECT 1"
            testOnBorrow="true"
            poolPreparedStatements="true"
            removeAbandoned="true"
            removeAbandonedTimeout="60"
            logAbandoned="true"/>

Complete details : http://amitcodes.com/2008/07/26/16/

Upvotes: 1

Arun
Arun

Reputation: 2572

I tried using dbcp and c3p0. I found certain problems in dbcp but c3p0 is working fine.

autoReconnect=true

Now my application is able to reconnect to MySQL DB automatically.

Upvotes: 0

Related Questions