wood burning stoves 2.0
The moose likes JDBC and Relational Databases and the fly likes Database connection - how to identify if terminated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Database connection - how to identify if terminated?" Watch "Database connection - how to identify if terminated?" New topic

Database connection - how to identify if terminated?

Ravi Srinivas

Joined: Sep 04, 2003
Posts: 26
I have a Java program that creates a java.sql.Connection to a database, inserts records, closes the connection and exits.
My question is - how do I know if the connection has been terminated by some external means (e.g. DBA terminates my db session, database shuts down etc.)? Right now, my program keeps trying to insert records even if the connection is terminated and hence keeps giving errors.
I tried connection.isClosed(), but it returns true only if the connection has been closed by the Java program itself. If the connection is terminated outside of the Java program, it seems to return false.

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to our JDBC forum...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
As far as I know, the only way to test a Connection is to try and use it. If you receive an SQLException, you should abandon further DB access until the problem is resolved.

If you are in a JTA transaction, invoke UserTransaction.setRollbackOnly() so that your code can then ascertain by invoking getStatus() to see if it is worthwhile continuing with the transaction.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link: http://aspose.com/file-tools
subject: Database connection - how to identify if terminated?
It's not a secret anymore!