Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

JDBC Connection object lifetime / timeout?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using the latest JDBC 5 connector to connect to MySQL. Everything works swimmingly, most of the time !!!.

I wrote my own ConnectionPool.java class that basically creates 20
connections in a queue and manages checkin/ out of JDBC connections by the
application.

...
...
...
connectionQueue = new LinkedBlockingQueue<Connection>(20);

while(connectionQueue.remainingCapacity(...
tempConnection = DriverManager.getConnection(URL,
UserName, Password);
connectionQueue.add(tempConnection);
}

...
checkin()
checkout(Connection c)
...

This works fine most of the time, even under high use/pressure... However...if I leave the program running but idle for an extended
while... where basically it's just in a listening state and none of the
connections are getting taken and they are all residing in the
Pool...whenever a new message arrives and it requires a connection to the DB
i always get error:

mysql.jdbc.exceptions.jdbc4.Communicat... Communication link
failure

A program restart will fix immediately. I'm guessing the idle time is the key here with
the connections somehow dying, getting corrupt, exceeded time to live
etc or something...? but I've no idea how to fix...

All suggestions very welcome
Thanks
 
Bartender
Posts: 2658
19
Netbeans IDE C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shane,

the best way to fix it, is to use one of the existing connection pooling libraries. They have built in mechanisms to check connection health before giving the connection to your application.

You could build in this functionality in your own connection pool:
Before handing over a connection, execute a small sql statement with that connection.
If it returns, you can give the connection to the application.
If it fails, throw away the connection, replace it with a fresh one, and give it to the application.

Regards,
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic