db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r388771 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/jdbc/EmbedConnection40.java testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java
Date Sat, 25 Mar 2006 15:17:31 GMT
Author: rhillegas
Date: Sat Mar 25 07:17:30 2006
New Revision: 388771

URL: http://svn.apache.org/viewcvs?rev=388771&view=rev
Log:
Commit Olav's patch for DERBY-1090, implementing the JDBC4 Connection.isValid() method.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java?rev=388771&r1=388770&r2=388771&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedConnection40.java Sat
Mar 25 07:17:30 2006
@@ -30,6 +30,7 @@
 import java.sql.SQLXML;
 import java.util.Properties;
 
+import org.apache.derby.shared.common.reference.SQLState;
 import org.apache.derby.jdbc.InternalDriver;
 
 public class EmbedConnection40 extends EmbedConnection30 {
@@ -70,10 +71,31 @@
         throw Util.notImplemented();
     }
     
+    /**
+     * Checks if the connection has not been closed and is still valid. 
+     * The validity is checked by checking that the connection is not closed.
+     *
+     * @param timeout This should be the time in seconds to wait for the 
+     * database operation used to validate the connection to complete 
+     * (according to the JDBC4 JavaDoc). This is currently not supported/used.
+     *
+     * @return true if the connection is valid, false otherwise
+     * @exception SQLException if the parameter value is illegal or if a
+     * database error has occured
+     */
     public boolean isValid(int timeout) throws SQLException {
-        throw Util.notImplemented();
+        // Validate that the timeout has a legal value
+        if (timeout < 0) {
+            throw Util.generateCsSQLException(SQLState.INVALID_API_PARAMETER,
+                                              new Integer(timeout), "timeout",
+                                              "java.sql.Connection.isValid");
+        }
+
+        // Use the closed status for the connection to determine if the
+        // connection is valid or not
+        return !isClosed();
     }
-    
+
     public void setClientInfo(String name, String value)
     throws SQLException{
         throw Util.notImplemented();

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java?rev=388771&r1=388770&r2=388771&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java
Sat Mar 25 07:17:30 2006
@@ -29,12 +29,14 @@
 import java.sql.Blob;
 import java.sql.Clob;
 import java.sql.Connection;
+import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.NClob;
 import java.sql.SQLException;
 import java.sql.SQLXML;
 import java.sql.Statement;
 import java.util.Properties;
+import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.tools.ij;
 import org.apache.derby.shared.common.reference.SQLState;
 
@@ -46,6 +48,10 @@
 public class TestConnectionMethods {
     Connection conn = null;
     
+    /**
+     * Constructor for an object that is used for running test of the
+     * new connection methods defined by JDBC 4.
+     */
     public TestConnectionMethods(Connection connIn) {
         conn = connIn;
     }
@@ -173,8 +179,132 @@
         }
         
     }
-    
-    void t_isValid() {
+
+    /**
+     * Test the Connection.isValid method in the embedded driver.
+     */
+    void t_isValid_Embed() {
+
+        /*
+         * Test illegal parameter values
+         */
+        try {
+            conn.isValid(-1);  // Negative timeout
+            System.out.println("FAIL: isValid(-1): " + 
+                               "Invalid argument execption not thrown");
+        } catch (SQLException e) {
+            if(!StandardException.getSQLStateFromIdentifier(
+                SQLState.INVALID_API_PARAMETER).equals(e.getSQLState())) {
+                System.out.println("FAIL: isValid(-1): Unexpected SQLException" +
+                                   e);
+            }
+        }
+
+        /*
+         * Test with no timeout
+         */
+        try {
+            if (!conn.isValid(0)) {
+                System.out.println("FAIL: isValid(0): returned false");
+            }
+        } catch(Exception e) {
+            System.out.println("FAIL: isValid(0): Unexpected exception: " + e);
+        }
+
+        /*
+         * Test with a valid timeout
+         */
+        try {
+            if (!conn.isValid(1)) {
+                System.out.println("FAIL: isValid(1): returned false");
+            }
+        } catch(Exception e) {
+            System.out.println("FAIL: isValid(1): Unexpected exception: " + e);
+        }
+
+        /*
+         * Test on a closed connection
+         */
+        try {
+            conn.close();
+        } catch (SQLException e) {
+            System.out.println("FAIL: close failed: Unexpected exception: " + e);
+        }
+
+        try {
+            if (conn.isValid(0)) {
+                System.out.println("FAIL: isValid(0) on closed connection: " +
+                                   "returned true");
+            }
+        } catch(Exception e) {
+            System.out.println("FAIL: isValid(0) on closed connection: " + 
+                               "Unexpected exception: " + e);
+        }
+
+        /*
+         * Open a new connection and test it
+         */
+        try {
+            conn = ij.startJBMS();
+        } catch (Exception e) {
+            System.out.println("FAIL: failed to open new connection: " +
+                               "Unexpected exception: " + e);
+        }
+
+        try {
+            if (!conn.isValid(0)) {
+                System.out.println("FAIL: isValid(0) on open connection: " + 
+                                   "returned false");
+            }
+        } catch(Exception e) {
+            System.out.println("FAIL: isValid(0) on open connection: " + 
+                               "Unexpected exception: " + e);
+        }
+
+        /*
+         * Test on stopped DB: stop Derby
+         */
+        try {
+            DriverManager.getConnection("jdbc:derby:;shutdown=true");
+        } catch(SQLException e) {
+            // Ignore any exceptions from shutdown
+        }
+
+        /*
+         * Test if the connection is still valid
+         */
+        try {
+            if (conn.isValid(0)) {
+                System.out.println("FAIL: isValid(0) on stopped database: " + 
+                                   "returned true");
+            }
+        } catch(Exception e) {
+            System.out.println("FAIL: isValid(0) on a stopped database: " + 
+                               "Unexpected exception: " + e);
+        } 
+
+        /*
+         * Start Derby by getting a new connection and check that
+         * the new connection is valid.
+         */
+        try {
+            conn = ij.startJBMS();
+        } catch (Exception e) {
+            System.out.println("FAIL: failed to re-start database: " +
+                               "Unexpected exception: " + e);
+        }
+        try {
+            if (!conn.isValid(0)) {
+                System.out.println("FAIL: isValid(0) on new connection: " + 
+                                   "returned false");
+            }
+        } catch(Exception e) {
+            System.out.println("FAIL: isValid(0) on new connection: " + 
+                               "Unexpected exception: " + e);
+        }
+    }
+
+    void t_isValid_Client() {
         boolean ret;
         try {
             ret = conn.isValid(0);
@@ -249,7 +379,7 @@
         t_createBlob_Client();
         t_createNClob();
         t_createSQLXML();
-        t_isValid();
+        t_isValid_Client();
         t_setClientInfo1();
         t_setClientInfo2();
         t_getClientInfo1();
@@ -261,7 +391,7 @@
         t_createBlob();
         t_createNClob();
         t_createSQLXML();
-        t_isValid();
+        t_isValid_Embed();
         t_setClientInfo1();
         t_setClientInfo2();
         t_getClientInfo1();



Mime
View raw message