Changeset: 89e954e7acbb for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=89e954e7acbb
Modified Files:
        src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
Branch: default
Log Message:

Correct implementation of method Connection.isValid().
It should restore the original query timeout after changing the query timeout 
via Stmt.setQueryTimeout()
Do the close() statements in the finally clause, so its is always done (and 
resources are released), also when an SQLException occurs.
In case the connection is in "Current transaction is aborted (please ROLLBACK)" 
state, the connection is valid and usable
 and thus method Connection.isValid() should return true.


diffs (58 lines):

diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java 
b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
@@ -1232,27 +1232,44 @@ public class MonetConnection
                // ping db using query: select 1;
                Statement stmt = null;
                ResultSet rs = null;
+               boolean isValid = false;
                try {
                        stmt = createStatement();
-                       stmt.setQueryTimeout(timeout);
-                       rs = stmt.executeQuery("SELECT 1");
-                       rs.close();
-                       rs = null;
-                       stmt.close();
-                       return true;
-               } catch (Exception e) {
+                       if (stmt != null) {
+                               int original_timeout = stmt.getQueryTimeout();
+                               if (timeout > 0 && original_timeout != timeout) 
{
+                                       // we need to change the requested 
timeout for this test query
+                                       stmt.setQueryTimeout(timeout);
+                               }
+                               rs = stmt.executeQuery("SELECT 1");
+                               if (rs != null && rs.next()) {
+                                       isValid = true;
+                               }
+                               if (timeout > 0 && original_timeout != timeout) 
{
+                                       // restore the original server timeout 
value
+                                       stmt.setQueryTimeout(original_timeout);
+                               }
+                       }
+               } catch (SQLException se) {
+                       String msg = se.getMessage();
+                       // System.out.println("Con.isValid(): " + msg);
+                       if (msg != null && msg.equals("Current transaction is 
aborted (please ROLLBACK)")) {
+                               isValid = true;
+                       }
+                       /* ignore stmt errors/exceptions, we are only testing 
if the connection is still alive and usable */
+               } finally {
                        if (rs != null) {
                                try {
                                        rs.close();
-                               } catch (Exception e2) {}
+                               } catch (Exception e2) { /* ignore error */ }
                        }
                        if (stmt != null) {
                                try {
                                        stmt.close();
-                               } catch (Exception e2) {}
+                               } catch (Exception e2) { /* ignore error */ }
                        }
                }
-               return false;
+               return isValid;
        }
 
        /**
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to