Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql not reconnecting with JNDI Tomcat 6

I am using JNDI with Tomcat6 to manage Mysql connections, my Catalina/domain.com/ROOT.xml has:

<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"
   username="db1" password="somepass" driverClassName="com.mysql.jdbc.Driver"             
   url="jdbc:mysql://localhost:3306/db?autoReconnect=true" maxActive="15" maxIdle="3"  
   maxWait="5000" removeAbandoned="true" removeAbandonedTimeout="20" />

I though autoReconnect will do the job reconnecting to database but it does not, after about 8 hours of inactivity my app spits out lost connection to database errors. Any ideas?

Thanks, Fedor

like image 666
Fedor Avatar asked Dec 03 '09 20:12

Fedor


1 Answers

Dont use autoReconnect. There are problems with it and it's been deprecated. For example, you could have a disconnect/reconnect event happen while a thread is using the connection. I would instead have your connection pool test connections with testOnBorrow before passing them to the app. Here is an example:

<Resource name="jdbc/db"
          auth="Container"
          type="javax.sql.DataSource"
          username="db1"
          password="somepass"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/db"
          maxActive="15"
          maxIdle="3"
          maxWait="5000"
          removeAbandoned="true"
          removeAbandonedTimeout="20"
          logAbandoned="true"
          factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
          validationQuery="select 1"
          minEvictableIdleTimeMillis="3600000"
          timeBetweenEvictionRunsMillis="1800000"
          numTestsPerEvictionRun="10"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          />
like image 185
Asaph Avatar answered Sep 27 '22 22:09

Asaph