db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r382906 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/jdbc/ engine/org/apache/derby/jdbc/ testing/org/apache/derbyTesting/functionTests/suites/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/
Date Fri, 03 Mar 2006 18:43:36 GMT
Author: rhillegas
Date: Fri Mar  3 10:43:34 2006
New Revision: 382906

URL: http://svn.apache.org/viewcvs?rev=382906&view=rev
Log:
Checkin Anurag's patch for DERBY-819: Triaging of SQLExceptions into new JDBC4 subclasses.

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java  
(with props)
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory40.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception_app.properties
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java
    db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver40.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/jdbc4.runall
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant

Added: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java?rev=382906&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java Fri
Mar  3 10:43:34 2006
@@ -0,0 +1,47 @@
+/*
+ 
+   Derby - Class org.apache.derby.impl.jdbc.SQLExceptionFactory
+ 
+   Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ 
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+ 
+      http://www.apache.org/licenses/LICENSE-2.0
+ 
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ 
+ */
+
+package org.apache.derby.impl.jdbc;
+
+import java.io.IOException;
+import org.apache.derby.iapi.error.ExceptionSeverity;
+import org.apache.derby.iapi.reference.SQLState;
+import org.apache.derby.iapi.error.StandardException;
+import org.apache.derby.iapi.services.i18n.MessageService;
+import org.apache.derby.iapi.reference.MessageId;
+
+import java.sql.SQLException;
+
+/**
+ *Class to create SQLException
+ *
+ */
+public class SQLExceptionFactory {
+    /**
+     * method to construct SQLException
+     * version specific drivers can overload this method to create
+     * version specific exceptions
+     */
+    public SQLException getSQLException(String message, String messageId,
+            SQLException next, int severity, Throwable t, Object[] args) {
+        return new EmbedSQLException(message, messageId, next, severity,
+                t, args);
+    }
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory40.java?rev=382906&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory40.java
(added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory40.java
Fri Mar  3 10:43:34 2006
@@ -0,0 +1,93 @@
+/*
+ 
+   Derby - Class org.apache.derby.impl.jdbc.SQLExceptionFactory40
+ 
+   Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ 
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+ 
+      http://www.apache.org/licenses/LICENSE-2.0
+ 
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ 
+ */
+
+package org.apache.derby.impl.jdbc;
+
+import java.sql.SQLDataException;
+import java.sql.SQLException;
+import java.sql.SQLIntegrityConstraintViolationException;
+import java.sql.SQLInvalidAuthorizationSpecException;
+import java.sql.SQLSyntaxErrorException;
+import java.sql.SQLTransactionRollbackException;
+import java.sql.SQLTransientConnectionException;
+import org.apache.derby.iapi.error.StandardException;
+
+/**
+ * SQLExceptionFactory40 overwrites getSQLException method
+ * to return SQLException or one of its sub class
+ */
+
+public class SQLExceptionFactory40 extends SQLExceptionFactory {
+    
+    /**
+     * overwrites super class method to create JDBC4 exceptions      
+     * SQLSTATE CLASS (prefix)     Exception
+     * 08                          java.sql.SQLTransientConnectionException
+     * 22                          java.sql.SQLDataException
+     * 28                          java.sql.SQLInvalidAuthorizationSpecException
+     * 40                          java.sql.SQLTransactionRollbackException
+     * 42                          java.sql.SQLSyntaxErrorException
+     * 
+     * This method sets the stack trace of the newly created exception to the
+     * root cause of the original Throwable.
+     * Note the following divergence from JDBC3 behavior: When running
+     * a JDBC3 client, we return EmbedSQLException. That exception class
+     * overrides Throwable.toString() and strips off the Throwable's class name.
+     * In contrast, the following JDBC4 implementation returns
+     * subclasses of java.sql.Exception. These subclasses inherit the behavior 
+     * of Throwable.toString(). That is, their toString() output includes
+     * their class name. This will break code which relies on the
+     * stripping behavior of EmbedSQLSxception.toString(). 
+     */
+    
+    public SQLException getSQLException(String message, String messageId,
+            SQLException next, int severity, Throwable t, Object[] args) {
+        String sqlState = StandardException.getSQLStateFromIdentifier(messageId);
+        SQLException ex = new SQLException(message, sqlState, severity, t);
+        if (sqlState.startsWith("08")) {
+            //none of the sqlstate supported by derby belongs to
+            //NonTransientConnectionException
+            ex = new SQLTransientConnectionException(message, sqlState,
+                    severity, t);
+        } else if (sqlState.startsWith("22")) {
+            ex = new SQLDataException(message, sqlState, severity, t);
+        } else if (sqlState.startsWith("23")) {
+            ex = new SQLIntegrityConstraintViolationException(message, sqlState,
+                    severity, t);
+        } else if (sqlState.startsWith("28")) {
+            ex = new SQLInvalidAuthorizationSpecException(message, sqlState,
+                    severity, t);
+        }        
+        else if (sqlState.startsWith("40")) {
+            ex = new SQLTransactionRollbackException(message, sqlState,
+                    severity, t);
+        } else if (sqlState.startsWith("42")) {
+            ex = new SQLSyntaxErrorException(message, sqlState, severity, t);
+        }
+        
+        if (next != null) {
+            ex.setNextException(next);
+        }
+        if (t != null) {
+            ex.setStackTrace (t.getStackTrace ());
+        }
+        return ex;
+    }        
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory40.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java?rev=382906&r1=382905&r2=382906&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java Fri Mar  3 10:43:34
2006
@@ -64,22 +64,28 @@
 //the actual changes made.
 public abstract class Util  {
 
+
+    private static SQLExceptionFactory exceptionFactory = 
+                                    new SQLExceptionFactory ();
+
 	/*
 	** Methods of Throwable
 	*/
 
 	// class implementation
 
-	/**
-		This looks up the message and sqlstate values and generates
-		the appropriate exception off of them.
-	 */
+    /**
+     * This looks up the message and sqlstate values and calls
+     * the SQLExceptionFactory method to generate
+     * the appropriate exception off of them.
+     */
 
 	private static SQLException newEmbedSQLException(String messageId,
 			Object[] args, SQLException next, int severity, Throwable t) {
-		return new EmbedSQLException(
-			MessageService.getCompleteMessage(messageId, args),
-			messageId, next, severity, t, args);
+        String message = MessageService.getCompleteMessage
+                                        (messageId, args);
+        return exceptionFactory.getSQLException (
+			    message, messageId, next, severity, t, args);
 	}
 
 	public static SQLException newEmbedSQLException(String messageId,
@@ -192,9 +198,10 @@
 	}
 
 	public static SQLException generateCsSQLException(StandardException se) {
-		return new EmbedSQLException(
-            	se.getMessage(), se.getMessageId(), null, se.getSeverity(), se, se.getArguments());
-	}
+        return exceptionFactory.getSQLException(
+                se.getMessage(), se.getMessageId(), (SQLException) null,
+                se.getSeverity(), se, se.getArguments());
+    }
 
 	public static SQLException noCurrentConnection() {
 		return newEmbedSQLException(SQLState.NO_CURRENT_CONNECTION,
@@ -241,6 +248,15 @@
 			new Object[] {typeName(targetSQLType)},
                 StandardException.getSeverityFromIdentifier(SQLState.TYPE_MISMATCH));
 	}
+
+    /**
+     * this method is called to replace the exception factory to be 
+     * used to generate the SQLException or the subclass
+     */
+
+    public static void setExceptionFactory (SQLExceptionFactory factory) {
+        exceptionFactory = factory;
+    }
 
 
   public static String typeName(int jdbcType) {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver40.java?rev=382906&r1=382905&r2=382906&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver40.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver40.java Fri Mar  3 10:43:34
2006
@@ -26,6 +26,7 @@
 import org.apache.derby.iapi.jdbc.BrokeredConnection40;
 import org.apache.derby.iapi.sql.ResultSet;
 import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.impl.jdbc.EmbedConnection;
 import org.apache.derby.impl.jdbc.EmbedConnection30;
 import org.apache.derby.impl.jdbc.EmbedPreparedStatement40;
@@ -35,6 +36,8 @@
 import org.apache.derby.impl.jdbc.EmbedResultSet;
 import org.apache.derby.impl.jdbc.EmbedResultSet40;
 import org.apache.derby.impl.jdbc.EmbedDatabaseMetaData40;
+import org.apache.derby.impl.jdbc.SQLExceptionFactory40;
+import org.apache.derby.impl.jdbc.Util;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.CallableStatement;
@@ -42,6 +45,7 @@
 import java.util.Properties;
 
 
+
 public class Driver40 extends Driver30 {
     
     public Connection getNewNestedConnection(EmbedConnection conn) {
@@ -108,6 +112,17 @@
     
     public EmbedRowId newEmbedRowId() throws SQLException {
         return new EmbedRowId();
+    }
+    
+    /**
+     * Overwriting the super class boot method to set exception factory
+     * @see InternalDriver.boot
+     */
+
+	public void boot(boolean create, Properties properties) 
+          throws StandardException {
+        Util.setExceptionFactory (new SQLExceptionFactory40 ());
+        super.boot (create, properties);
     }
 
     public DatabaseMetaData newEmbedDatabaseMetaData(EmbedConnection conn, String dbname)


Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/jdbc4.runall
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/jdbc4.runall?rev=382906&r1=382905&r2=382906&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/jdbc4.runall
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/jdbc4.runall
Fri Mar  3 10:43:34 2006
@@ -4,3 +4,4 @@
 jdbc4/TestPreparedStatementMethods.java
 jdbc4/TestResultSetMethods.java
 jdbc4/TestDbMetaData.java
+jdbc4/TestJDBC40Exception.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception.java?rev=382906&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception.java
Fri Mar  3 10:43:34 2006
@@ -0,0 +1,158 @@
+/*
+ 
+   Derby - Class 
+        org.apache.derbyTesting.functionTests.tests.jdbc4.TestJDBC40Exception
+ 
+   Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ 
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+ 
+      http://www.apache.org/licenses/LICENSE-2.0
+ 
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ 
+ */
+
+package org.apache.derbyTesting.functionTests.tests.jdbc4;
+
+import java.sql.Connection;
+import java.sql.Statement;
+import java.sql.SQLDataException;
+import java.sql.SQLException;
+import java.sql.SQLIntegrityConstraintViolationException;
+import java.sql.SQLSyntaxErrorException;
+import java.sql.SQLTransientConnectionException;
+import java.sql.SQLTransactionRollbackException;
+import junit.framework.Test;
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.functionTests.tests.derbynet.testconnection;
+
+public class TestJDBC40Exception {    
+    
+    private static final String EXCEPTION_TABLE1 = "EXCEPTION_TABLE1";
+    public TestJDBC40Exception() {
+    }
+    
+    /*
+     * Stub methods to be removed after 623 is fixed and test is 
+     * moved to use junit
+     */
+    private Connection getConnection () throws SQLException {
+        return new TestConnection ().createEmbeddedConnection();
+    }
+    
+    /*
+     * Stub methods to be removed after 623 is fixed and test is 
+     * moved to use junit
+     */
+    private void close (Connection conn) throws SQLException {
+        conn.close ();
+    }
+
+    /*
+     * Stub methods to be removed after 623 is fixed and test is 
+     * moved to use junit
+     */    
+    private void execute (Connection conn, String sql) throws SQLException {
+        Statement stmt = conn.createStatement ();
+        stmt.execute (sql);
+        stmt.close ();
+    }
+    
+    public static Test suite() {
+        TestSuite testSuite = new TestSuite();
+        testSuite.addTestSuite(TestJDBC40Exception.class);
+        return testSuite;
+    }
+    
+    
+    public void testException() throws Exception{
+        Connection conn = getConnection();
+        execute(conn,  "create table " + EXCEPTION_TABLE1 + "(id integer " +
+                "primary key, data varchar (5))");
+        execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
+                "values (1, 'data1')");
+        close(conn);
+        checkDataException();
+        checkIntegrityConstraintViolationException();
+        checkSyntaxErrorException();
+        checkConnectionException();
+        checkTimeout();
+    }
+    
+    private void checkIntegrityConstraintViolationException() throws Exception {
+        Connection conn = getConnection();
+        try {
+            execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
+                    "values (1, 'data1')");
+        } catch (SQLIntegrityConstraintViolationException e) {
+              if (!e.getSQLState().startsWith ("23"))
+                System.out.println ("Unexpected SQL State" + e.getSQLState());
+        }
+    }
+    
+    private void checkDataException() throws Exception{
+        Connection conn = getConnection();
+        try {
+            execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
+                    "values (2, 'data1234556')");
+        } catch (SQLDataException e) {
+             if (!e.getSQLState().startsWith ("22"))
+                System.out.println ("Unexpected SQL State" + e.getSQLState());
+        }
+    }
+    
+    private void checkConnectionException() throws Exception {
+        Statement stmt = null;
+        Connection con = null;
+        try {
+            con = getConnection();
+            stmt = con.createStatement();
+            con.close();
+            stmt.execute("select * from exception1");
+        } catch (SQLTransientConnectionException cone) {
+            if (!cone.getSQLState().startsWith ("08"))
+                System.out.println ("Unexpected SQL State" + cone.getSQLState());
+        }
+    }
+    
+    private void checkSyntaxErrorException() throws Exception{
+        Connection conn = getConnection();
+        try {
+            execute(conn, "insert into " + EXCEPTION_TABLE1 + "(id, data)" +
+                    "values ('2', 'data1')");
+        } catch (SQLSyntaxErrorException e) {
+            if (!e.getSQLState().startsWith ("42"))
+                System.out.println ("Unexpected SQL State" + e.getSQLState());
+        }
+    }
+    
+    private void checkTimeout() throws Exception {
+        Connection con1 = getConnection();
+        Connection con2 = getConnection();
+        try {
+            con1.setAutoCommit(false);
+            con2.setAutoCommit(false);
+            con1.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
+            con2.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
+            execute(con1, "select * from " + EXCEPTION_TABLE1 + " for update");
+            execute(con2, "select * from " + EXCEPTION_TABLE1 + " for update");
+        } catch (SQLTransactionRollbackException e) {
+              if (!e.getSQLState().startsWith ("40"))
+                System.out.println ("Unexpected SQL State" + e.getSQLState());
+        }
+    }
+    
+    
+    public static void main(String [] args) throws Exception {    
+        TestJDBC40Exception test = new TestJDBC40Exception ();
+        test.testException ();
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception_app.properties
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception_app.properties?rev=382906&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception_app.properties
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception_app.properties
Fri Mar  3 10:43:34 2006
@@ -0,0 +1,13 @@
+#This test is testing JDBC40 SQLException subclasses avaialble from JDK1.6
+#Ignoring the earlier jvm versions
+derby.locks.waitTimeout=5
+ij.database=jdbc:derby:wombat;create=true
+ij.showNoConnectionsAtStart=true
+ij.showNoCountForSelect=true
+
+runwithibm13=false
+runwithibm14=false
+runwithj9=false
+runwithjdk12=false
+runwithjdk13=false
+runwithjdk14=false

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestJDBC40Exception_app.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant?rev=382906&r1=382905&r2=382906&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant
Fri Mar  3 10:43:34 2006
@@ -7,3 +7,7 @@
 aclob.txt
 littleclob.txt
 short.txt
+TestConnectionMethods_app.properties
+TestResultSetMethods_app.properties
+TestDbMetaData_app.properties
+TestJDBC40Exception_app.properties



Mime
View raw message