• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Persistant connection for MySql

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic