MonoThreaded MonoThreaded - 1 year ago 169
SQL Question

Java JDBC connection status

I am (successfully) connecting to a database using the following:

java.sql.Connection connect = DriverManager.getConnection(

What should I be checking to see if the connection is still open and up after some time?

I was hoping for something like
available for me to use.

dty dty
Answer Source

Nothing. Just execute your query. If the connection has died, either your JDBC driver will reconnect (if it supports it, and you enabled it in your connection string--most don't support it) or else you'll get an exception.

If you check the connection is up, it might fall over before you actually execute your query, so you gain absolutely nothing by checking.

That said, a lot of connection pools validate a connection by doing something like SELECT 1 before handing connections out. But this is nothing more than just executing a query, so you might just as well execute your business query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download