0

I am working on a server-application written in java. There are up to 30 client requests per second, for every request a specific mysql table entry gets updated.

All the server threads use a single mySQL Connection, they obtain it from a singleton class. Then the server thread creates a Statement and executes a update query.

Although I close all the created Statements after execution, the server stops updating the mySQL table after some hours.

What could be wrong? Is this setup a misconcept?

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
cutze
  • 93
  • 1
  • 8
  • You may take a look at http://stackoverflow.com/questions/1683949/connection-timeout-for-drivermanager-getconnection. You may also implement or [reuse](http://commons.apache.org/proper/commons-pool/) [connection pool](http://en.wikipedia.org/wiki/Connection_pooll) to manage your `Connection`s – lifus May 13 '13 at 09:23

1 Answers1

0

Perhaps, your connection was closed due to timeout. You can make use of Connection#isValid in order to verify whether or not your connection is still open.

You may take a look at Connection timeout for DriverManager getConnection. There are several good tips listed.

Also, You may consider the use of Connection Pool in your code. Apache commons is a good place to start.

Community
  • 1
  • 1
lifus
  • 8,074
  • 1
  • 19
  • 24
  • Thank you for your answer, a connection pool seems the way to go. I am now looking for an easy example, as I don't understand the usage of connection pools yet... – cutze May 13 '13 at 10:06
  • http://www.roseindia.net/tutorial/java/jdbc/jdbcconnectionpooling.html I think I will try it this way. Would it be right to make this Thread a singleton, start it in my main class and let all the threads obtain their connections from it? And another question: How big should the connection pool be for ~30 requests/second? Unfortunately I can not test it yet, have to wait for the evening :) – cutze May 13 '13 at 10:15
  • You're welcome. Its not required to implement Connection Pool from scratch. There are several jars to choose from: [C3PO](http://www.mchange.com/projects/c3p0/index.html), [DBCP](http://commons.apache.org/dbcp) – lifus May 13 '13 at 10:15
  • You will be able to fine tune your connection pool later, I guess. As for now, set any reasonable min poll size and max pool size and let connection pool figure out how to adjust pool size. – lifus May 13 '13 at 10:35