Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Reconnect of Database Connection

I have a DBCP connection pool in Tomcat. The problem is that when the connection is lost briefly the appliction is broken because DBCP won't try to reconnect again later when there is a connection. Can I get DBCP to reconnect automatically?

like image 816
wesley Avatar asked Mar 30 '10 19:03

wesley


People also ask

How to reestablish connection in MySQL?

To check for reconnection, call mysql_thread_id() to get the original connection identifier before calling mysql_ping() , then call mysql_thread_id() again to see whether the identifier changed.

How long do MySQL connections last?

MySQL has its wait_timeout variable default value set to 28800 seconds (8 hours).

What is connection pooling in MySQL?

Connection pooling enables the idle connection to be used by some other thread to do useful work. In practice, when a thread needs to do work against a MySQL or other database with JDBC, it requests a connection from the pool.


2 Answers

There are 2 ways to "solve" this, though both have some issues:

  1. You can use a "validationQuery" (see below) to have a test query run before you go (generally something like 'select 1 from dual' which will be used to test connections before/after you get/give them to the pool. This adds an extra call per connection request from the pool. See: http://wiki.apache.org/commons/DBCP

  2. Instead of doing this per query, you can have the idleEvictorThread do it by setting testWhileIdle, though in some versions that thread can cause deadlocking under high-load. See: http://commons.apache.org/dbcp/configuration.html for more details on that and other options

like image 60
jayshao Avatar answered Sep 28 '22 06:09

jayshao


Don't think DBCP does that, but BoneCP (http://jolbox.com) can be configured to automatically replay any transactions when the DB or network goes down. It's completely transparent to your application.

like image 29
wwadge Avatar answered Sep 28 '22 08:09

wwadge