permaculture playing cards*
The moose likes JDBC and the fly likes Persistant connection for MySql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Persistant connection for MySql" Watch "Persistant connection for MySql" New topic
Author

Persistant connection for MySql

Tim Tully
Greenhorn

Joined: Sep 16, 2001
Posts: 8
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

Joined: Aug 22, 2001
Posts: 168
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


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
 
 
subject: Persistant connection for MySql
 
Similar Threads
Creating an HTTP Header
Is there a MySQL server running on the machine/port you are trying to connect to?
socket connection
tcp socket communication-persistence connection
Cookie question