Database connection - how to identify if terminated?
Joined: Sep 04, 2003
Hi, 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.
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
Joined: Sep 29, 2002
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.