0

I am writing a game server that requires connecting to a MySQL database server to retrieve information about players or certain items in the game (the latter is only taken at server start). Should I have one connection to the database constantly open, and then check it for having failed / broken before use, or make a single connection for every request.

The current system (new connection for every player join), is adding noticeable latency to the login process.

Is it possible to have an always open MySQL connection in Java?

jSherz
  • 927
  • 4
  • 14
  • 33

2 Answers2

2

Have a look at the ConnectionPool classes of Java to have a set of persistent connections open. See here for an article on MySql connection pooling with Java. For some existing implementations you might refer to this question: Java JDBC connection pool library choice in 2011/2012?

That way you save the overhead of opening a connection over and over again, but have the amount of connections you need available, when you need them.

Community
  • 1
  • 1
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • 1
    Interesting article on that: http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html – Lukas Knuth Mar 25 '12 at 10:40
1

Is it possible to have an always open MySQL connection in Java?

Yes of course. You can make all methods of you DataProvider class static (not necessarily synchronized causes mysql connector for java is thread safe) In this way you will have one method (ex: Initialization) that will make the connection. After that all other method will use this connection.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
savionok
  • 485
  • 4
  • 11