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

2
  • Likely the exception tells you what's wrong, so what does it say ? Commented Jun 6, 2010 at 13:16
  • Same problem was posted recently: stackoverflow.com/questions/2979415/… Commented Jun 6, 2010 at 17:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.