This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database connection - how to identify if terminated?

 
Ravi Srinivas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ravi
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our JDBC forum...
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic