I'm using a singleton class for a PostgresSQL connection inside a servelet. The problem is that once it is open it works for a while (I guess until some timeout), and then it starts throwing a I/O exception. Any idea what is happening to the singleton class inside Tomcat VM? Thanks
-
Likely the exception tells you what's wrong, so what does it say ?nos– nos2010-06-06 13:16:01 +00:00Commented Jun 6, 2010 at 13:16
-
Same problem was posted recently: stackoverflow.com/questions/2979415/…BalusC– BalusC2010-06-06 17:55:06 +00:00Commented Jun 6, 2010 at 17:55
3 Answers
I have no idea. Just do the right thing and do not reinvent the wheel.
Use a DataSource. Either obtain it via JNDI, or do it yourself (I like using Spring, but if your web application is very simple, it's probably overkill).
Use a DataSource.
Comments
There's no singleton inside Tomcat; that's just the way connections work when you only have one and keep it open for a long time. It's called "timeout".
This design cannot scale. A better solution is to keep connections open for as short a time as possible. Your code should open a connection, use it, and close it in transaction scope.
You should also set up a connection pool in Tomcat.
Comments
and then it starts throwing a I/O exception
Well, what is the exception exactly?
Also, as a note, it's safe to use the same Postgres JDBC connection from multiple threads, but it is not recommended to do so.