Changeset: 48ee2ca98975 for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=48ee2ca98975
Added Files:
        tests/Bug_IsValid_Timeout_Bug_6782.java
Modified Files:
        src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
        tests/build.xml
Branch: default
Log Message:

Added test and fix for bug 6782, ie set the connection's query timeout 
explicitly at the end of isValid method.

The previous value must be set whenever the an exception is thrown or not 
during the method.


diffs (117 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
@@ -1297,10 +1297,11 @@ public class MonetConnection
                Statement stmt = null;
                ResultSet rs = null;
                boolean isValid = false;
+               int original_timeout = 0;
                try {
                        stmt = createStatement();
                        if (stmt != null) {
-                               final int original_timeout = 
stmt.getQueryTimeout();
+                               original_timeout = stmt.getQueryTimeout();
                                if (timeout > 0 && original_timeout != timeout) 
{
                                        // we need to change the requested 
timeout for this test query
                                        stmt.setQueryTimeout(timeout);
@@ -1309,10 +1310,6 @@ public class MonetConnection
                                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();
@@ -1325,6 +1322,24 @@ public class MonetConnection
                        }
                        /* ignore stmt errors/exceptions, we are only testing 
if the connection is still alive and usable */
                } finally {
+                       /* restore the original server timeout value, whenever 
an Exception has occurred or not */
+                       if (timeout > 0 && original_timeout != timeout) {
+                               Statement stmt2 = null;
+                               this.lastSetQueryTimeout = original_timeout;
+                               try {
+                                       /* we have to set in the server 
explicitly, because the test 'queryTimeout != connection.lastSetQueryTimeout' 
+                                          on 'internalExecute' won't pass and 
the server won't be set back */
+                                       stmt2 = this.createStatement();
+                                       stmt2.execute("CALL 
\"sys\".\"settimeout\"(" + this.lastSetQueryTimeout + ")");
+                               } catch (SQLException se) {
+                                       String msg = se.getMessage();
+                                       if (msg != null && 
msg.equalsIgnoreCase("Current transaction is aborted (please ROLLBACK)")) {
+                                               isValid = true;
+                                       }
+                               } finally {
+                                       closeResultsetStatement(null, stmt2);
+                               }
+                       }
                        closeResultsetStatement(rs, stmt);
                }
                return isValid;
diff --git a/tests/Bug_IsValid_Timeout_Bug_6782.java 
b/tests/Bug_IsValid_Timeout_Bug_6782.java
new file mode 100644
--- /dev/null
+++ b/tests/Bug_IsValid_Timeout_Bug_6782.java
@@ -0,0 +1,38 @@
+/*
+ * 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 - 2019 MonetDB B.V.
+ */
+
+import java.sql.*;
+
+public class Bug_IsValid_Timeout_Bug_6782 {
+       public static void main(String[] args) throws Exception {
+               // Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");    // not 
needed anymore for self registering JDBC drivers
+               Connection con = DriverManager.getConnection(args[0]);
+               Statement st = null;
+
+               st = con.createStatement();
+               st.setQueryTimeout(5);
+               System.out.println("getQueryTimeout must give 5: " + 
st.getQueryTimeout());
+               st.close();
+
+               con.isValid(3);
+
+               st = con.createStatement();
+               System.out.println("getQueryTimeout must give 0: " + 
st.getQueryTimeout());
+
+               con.isValid(3);
+               System.out.println("getQueryTimeout must give 0: " + 
st.getQueryTimeout());
+               st.close();
+
+               st.setQueryTimeout(5);
+               con.isValid(3);
+               System.out.println("getQueryTimeout must give 5: " + 
st.getQueryTimeout());
+               st.close();
+
+               con.close();
+       }
+}
diff --git a/tests/build.xml b/tests/build.xml
--- a/tests/build.xml
+++ b/tests/build.xml
@@ -144,6 +144,7 @@ Copyright 1997 - July 2008 CWI, August 2
     <antcall target="Bug_Connect_as_voc_getMetaData_Failure_Bug_6388" />
     <antcall target="Bug_PrepStmtSetString_6382" />
     <antcall target="Bug_LargeQueries_6571_6693" />
+    <antcall target="Bug_IsValid_Timeout_Bug_6782" />
   </target>
 
   <target name="test_class" depends="compile,jdbc">
@@ -429,4 +430,10 @@ Copyright 1997 - July 2008 CWI, August 2
     </antcall>
   </target>
 
+  <target name="Bug_IsValid_Timeout_Bug_6782">
+    <antcall target="test_class">
+      <param name="test.class" value="Bug_IsValid_Timeout_Bug_6782" />
+    </antcall>
+  </target>
+
 </project>
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to