0

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user191207
  • 5
  • 1
  • 3
  • Likely the exception tells you what's wrong, so what does it say ? – nos Jun 06 '10 at 13:16
  • Same problem was posted recently: http://stackoverflow.com/questions/2979415/how-to-manage-db-connections-on-server – BalusC Jun 06 '10 at 17:55

3 Answers3

4

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
alex
  • 5,213
  • 1
  • 24
  • 33
1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

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.

matt b
  • 138,234
  • 66
  • 282
  • 345