Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have a stand-alone java application that makes some database read/write business on a postgresql database. I used the isValid function available on a java.sql.Connection object in order to re-establish the database connection in case it is lost/fails possibly due to a network problem/crash. The following code snippet shows this idea shortly:

  //dbConnection is an instance of java.sql.Connection

  if(dbConnection.isValid(1)){
     reEstablishConnection();
  }

Nevertheless, at run-time I am getting an exception within the condition of above if block, which says that the method 'isValid' is not implemented yet. I am using the latest JDBC driver (v9.1-101) and my postgesql DBMS version is 8.3.9. I installed it using Yast2 on a SUSe Linux (SLES 11 SP2 - 64 bit). Can someone tell me me how I can check if the connection is lost programmatically in Java for postgresql? My inention is to reconnect automatically without the user intervention.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You could implement your own connection validity check function (something like executing "select 1" and returning true if 1 is returned from db without error).

However, I suggest you to use connection pooling and I recommend to use the the Apache Tomcat Pool, that will handle for you validity checking on connections (among many other things)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.