Changeset: 72b892ed670f for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=72b892ed670f
Added Files:
        java/tests/Test_CisValid.java
        sql/jdbc/tests/Tests/Test_CisValid.SQL.bat
        sql/jdbc/tests/Tests/Test_CisValid.SQL.sh
Modified Files:
        java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
        java/tests/build.xml
        sql/jdbc/tests/Tests/All
Branch: Jul2015
Log Message:

Connect.isValid() should never alter the state of the connection (e.g., close 
it), in case of any error (other than a negative timeout value), just return 
FALSE.


diffs (113 lines):

diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java 
b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
--- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
+++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
@@ -795,14 +795,20 @@ public class MonetConnection extends Mon
                if (closed)
                        return false;
                // ping db using select 1;
+               Statement stmt = null;
                try {
-                       Statement stmt = createStatement();
+                       stmt = createStatement();
+                       // the timeout parameter is ignored here, since
+                       // MonetStatement.setQueryTimeout(timeout) is not 
supported.
                        stmt.executeQuery("SELECT 1");
                        stmt.close();
                        return true;
-               } catch (SQLException e) {
-                       // close this connection
-                       close();
+               } catch (Exception e) {
+                       if (stmt != null) {
+                               try {
+                                       stmt.close();
+                               } catch (Exception e2) {}
+                       }
                }
                return false;
        }
diff --git a/java/tests/Test_CisValid.java b/java/tests/Test_CisValid.java
new file mode 100644
--- /dev/null
+++ b/java/tests/Test_CisValid.java
@@ -0,0 +1,33 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
+ */
+
+import java.sql.*;
+
+public class Test_CisValid {
+       /* Test that after an error has occurred during a transaction, one can
+        * still test if the connection is valid or not. 
+        * The function Connection.isValid() should only return TRUE or FALSE. 
It
+        * shall never alter the state of this connection */
+       public static void main(String[] args) throws Exception {
+               Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
+               Connection conn = DriverManager.getConnection(args[0]);
+               Statement stmt = conn.createStatement();
+
+               conn.setAutoCommit(false); // start a transaction
+               try {
+                       stmt.execute("SELECT COUNT(*) FROM doesnotexist;"); // 
let's trigger an error
+               } catch (SQLException e) {
+                       e.printStackTrace();
+                       System.out.println("Validating connection: 
conn.isValid? " + conn.isValid(30)); // Can we rollback on this connection?
+                       conn.rollback();
+               }
+                       
+               stmt.close();
+               conn.close();
+       }
+}
diff --git a/java/tests/build.xml b/java/tests/build.xml
--- a/java/tests/build.xml
+++ b/java/tests/build.xml
@@ -98,6 +98,7 @@ Copyright 1997 - July 2008 CWI, August 2
     <antcall target="Test_Clargequery" />
     <antcall target="Test_Cmanycon" />
     <antcall target="Test_Cforkbomb" />
+    <antcall target="Test_CisValid" />
     <antcall target="Test_Creplysize" />
     <antcall target="Test_Csavepoints" />
     <antcall target="Test_Ctransaction" />
@@ -173,6 +174,12 @@ Copyright 1997 - July 2008 CWI, August 2
     </antcall>
   </target>
 
+  <target name="Test_CisValid">
+    <antcall target="test_class">
+      <param name="test.class" value="Test_CisValid" />
+    </antcall>
+  </target>
+
   <target name="Test_Ctransaction">
     <antcall target="test_class">
       <param name="test.class" value="Test_Ctransaction" />
diff --git a/sql/jdbc/tests/Tests/All b/sql/jdbc/tests/Tests/All
--- a/sql/jdbc/tests/Tests/All
+++ b/sql/jdbc/tests/Tests/All
@@ -1,4 +1,5 @@
 HAVE_JDBCTESTS?Test_Cautocommit
+HAVE_JDBCTESTS?Test_CisValid
 HAVE_JDBCTESTS?Test_Clargequery
 HAVE_JDBCTESTS?Test_Cmanycon
 HAVE_JDBCTESTS?Test_Creplysize
diff --git a/sql/jdbc/tests/Tests/Test_CisValid.SQL.bat 
b/sql/jdbc/tests/Tests/Test_CisValid.SQL.bat
new file mode 100755
--- /dev/null
+++ b/sql/jdbc/tests/Tests/Test_CisValid.SQL.bat
@@ -0,0 +1,1 @@
+@call "%TSTSRCDIR%\Test.SQL.bat" %*
diff --git a/sql/jdbc/tests/Tests/Test_CisValid.SQL.sh 
b/sql/jdbc/tests/Tests/Test_CisValid.SQL.sh
new file mode 100755
--- /dev/null
+++ b/sql/jdbc/tests/Tests/Test_CisValid.SQL.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+$TSTSRCDIR/Test.SQL.sh $*
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to