• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database connection - how to identify if terminated?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our JDBC forum...
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic