Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persistant connection for MySql

 
Tim Tully
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I keep my connection to MySql persistant? That is, I want the socket connection to stay open for a week for example. If I leave a static connection open overnight, it seems to be auto-closed for me. Thanks.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim
This may not be a suitable soultion for what you are doing, but one way to ensure you always have an active connection is to write a class to encapsulate the java.sql.Connection object.
So, instead of using the JDBC code directly to create an instance of connection and then hold a reference to it in your calling object, create an instance of your encapsulating class.
The encapsulating class (let's call it DBConnection) should have the JDBC connection code in a private method, which is called from the constructor. In order to get a refence to the java.sql.connection, you should have to call a getConnection() method on your DBConnection instance. Inside this method's implementation, it should first check if its member connection is null or is closed, and if so, make a call to the private method that opens the connection, then return it.
That way, when you need to use a connection in your calling object, call DBConnection.getConnection and you will always get an open connection to use. Replace the java.sql.Connection member with a DBConnection member.
Another solution is to write a conenction pool, which does the same thing (i.e. making sure it always returns an open connection) but holds multiple instances of connections.
I get the feeling this is not the exact type of problem you are describing, but if this sounds acceptable and you would like some sample code, let me know and I'll post it here or e-mail it to you.
Hope this helps
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic