• Post Reply Bookmark Topic Watch Topic
  • New Topic

Connecting to DB, when? how often?

 
Jean-Michel Vilain
Greenhorn
Posts: 27
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a game server which stores accounts in a mysql DB thru JDBC.

At first, I initiated a connection to the database once and only once: at server start up. All went well until I noticed that after 8 hours of database inactivity, the connection to the database gets broken...
As my code wasn't ready for that behavior, I did a bit of refactoring. Now, every time my server has to perform some database operation, I open a connection. And when the operation is done, I close the database connection.

While refactoring, I was thinking "cool this is how I should have done since the beginning" ... it works but now I'm afraid of performance issue. And maybe other issues I didn't consider?
What do you think?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connection pools were designed for this. Application servers generally provide one. Actually, I don't know of any good introduction to connection pools off the top of my head, try to google around a bit or have a look at your application server documentation.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to martins comments
a connection pool stores connections for you (as creating a connection can be quite slow).
Before giving you one it checks if any of it's connections are still alive, if not it gets you an alive one. So it is a cross between your two methods of database connecting.
 
Jean-Michel Vilain
Greenhorn
Posts: 27
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Orite, thanks ;-)
 
tim fox
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Apache site has a decent connection pooling tutorial. Their connection pooling product is called DRCP and it is part of Apache Commons.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!