db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpendle...@apache.org
Subject svn commit: r1096991 - in /db/derby/code/trunk/java: engine/org/apache/derby/catalog/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/tools/
Date Wed, 27 Apr 2011 04:28:20 GMT
Author: bpendleton
Date: Wed Apr 27 04:28:20 2011
New Revision: 1096991

URL: http://svn.apache.org/viewvc?rev=1096991&view=rev
Log:
DERBY-4443: Wrap rollback in exception handlers in try-catch

This patch was contributed by Houx Zhang (houxzhang at gmail dot com)

The patch modifies the error handling in SystemProcedures.java so that
secondary exceptions during rollback are chained to the primary exception.
Also, EmbedSQLException's wrapping behavior is adjusted so that the
StandardException logic doesn't try to unwrap the chained exceptions.

A new test is added to the tools suite; it simulates rollback errors
using a special mock driver and mock connection, and verifies that
the exceptions are chained properly.


Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/RollBackWrappingWhenFailOnImportTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedSQLException.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java?rev=1096991&r1=1096990&r2=1096991&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/catalog/SystemProcedures.java Wed Apr
27 04:28:20 2011
@@ -1466,14 +1466,25 @@ public class SystemProcedures  {
                                replace, false);
 		}catch(SQLException se)
 		{
-			//issue a rollback on any errors
-			conn.rollback();
-			throw  se;
+			rollBackAndThrowSQLException(conn, se);
 		}
 		//import finished successfull, commit it.
 		conn.commit();
 	}
-
+	
+    /**
+     * issue a rollback when SQLException se occurs. If SQLException ouccurs when rollback,
+     * the new SQLException will be added into the chain of se. 
+     */
+    private static void rollBackAndThrowSQLException(Connection conn,
+            SQLException se) throws SQLException {
+        try {
+            conn.rollback();
+        } catch (SQLException e) {
+            se.setNextException(e);
+        }
+        throw se;
+    }
 
     /**
      * Import  data from a given file to a table. Data for large object 
@@ -1507,9 +1518,7 @@ public class SystemProcedures  {
                                );
         }catch(SQLException se)
         {
-            //issue a rollback on any errors
-            conn.rollback();
-            throw  se;
+            rollBackAndThrowSQLException(conn, se);
         }
         //import finished successfull, commit it.
         conn.commit();
@@ -1550,9 +1559,7 @@ public class SystemProcedures  {
 								  codeset, replace, false);
 		}catch(SQLException se)
 		{
-			//issue a rollback on any errors
-			conn.rollback();
-			throw  se;
+		    rollBackAndThrowSQLException(conn, se);
 		}
 
 		//import finished successfull, commit it.
@@ -1598,9 +1605,7 @@ public class SystemProcedures  {
                               codeset, replace, true);
         }catch(SQLException se)
         {
-            //issue a rollback on any errors
-            conn.rollback();
-            throw  se;
+            rollBackAndThrowSQLException(conn, se);
         }
 
         //import finished successfull, commit it.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedSQLException.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedSQLException.java?rev=1096991&r1=1096990&r2=1096991&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedSQLException.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedSQLException.java Wed
Apr 27 04:28:20 2011
@@ -104,6 +104,10 @@ public class EmbedSQLException extends S
 		return csqle;	
 	}
 	public boolean isSimpleWrapper() {
+		if (getNextException() != null) {
+			return false;
+		}
+		
 		return simpleWrapper;
 	}
 }

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/RollBackWrappingWhenFailOnImportTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/RollBackWrappingWhenFailOnImportTest.java?rev=1096991&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/RollBackWrappingWhenFailOnImportTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/RollBackWrappingWhenFailOnImportTest.java
Wed Apr 27 04:28:20 2011
@@ -0,0 +1,162 @@
+/*
+ *
+ * Derby - Class RollBackWrappingWhenFailOnImportTest
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.tools;
+
+import java.sql.CallableStatement;
+
+
+import java.sql.Connection;
+
+import java.sql.SQLException;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derby.impl.jdbc.EmbedConnection;
+import org.apache.derby.impl.jdbc.EmbedConnection30;
+import org.apache.derby.jdbc.Driver30;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.SupportFilesSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/**
+ * This test case comes from DERBY-4443. It's for show changes related to 
+ * wrap rollback in exception handlers in try-catch.
+ * In this test case, a MockInternalDriver is used to create a MockConnectionFailWhenRollBack
+ * which will fail when rollback() is called.
+ * 
+ */
+public class RollBackWrappingWhenFailOnImportTest extends BaseJDBCTestCase {
+    class MockInternalDriver extends Driver30 {
+
+        public class MockConnectionFailWhenRollBack extends EmbedConnection30 {
+
+            public MockConnectionFailWhenRollBack(Connection connection) {
+                super((EmbedConnection)connection);
+            }
+
+            public void rollback() throws SQLException {
+                throw new SQLException("error in roll back", "XJ058");
+            }
+        }
+
+        public Connection connect(String url, Properties info) {
+            Connection conn = null;
+            try {
+                conn = super.connect(url, info);
+            } catch (Exception e) {
+                //this exception is ignored for mocking
+            }
+            return new MockConnectionFailWhenRollBack(conn);
+        }
+    }
+
+    private String nonexistentFileName = "test/test.dat";
+
+    public RollBackWrappingWhenFailOnImportTest(String name) {
+        super(name);        
+    }
+    
+    public static Test suite() {
+        TestSuite suite = new TestSuite("RollBackWrappingWhenFailOnImportTest");
+        
+        if (!JDBC.vmSupportsJDBC3()) {
+            return suite;
+        }       
+        
+        Test test = new CleanDatabaseTestSetup(
+                TestConfiguration.embeddedSuite(
+                        RollBackWrappingWhenFailOnImportTest.class));
+                        
+        suite.addTest(test);
+        
+        return suite;
+    }
+
+    protected void setUp() throws Exception {
+        openDefaultConnection();
+        
+        MockInternalDriver dvr = new MockInternalDriver();
+        dvr.boot(false, null);
+        
+        SupportFilesSetup.deleteFile(nonexistentFileName);
+    }
+    
+    protected void tearDown() throws Exception {        
+        try {           
+            getTestConfiguration().shutdownEngine();            
+        } catch (Exception e) {
+            //Ignore exception for shut down mock driver            
+        }        
+        
+        super.tearDown();
+    }
+
+    public void testRollBackWhenFailOnImportTable() throws SQLException { 
+        String callSentence = "call SYSCS_UTIL.SYSCS_IMPORT_TABLE (" +
+                "null, 'IMP_EMP', '"  + nonexistentFileName + "test/test.dat" + 
+                "' , null, null, null, 0) ";
+        realTestRollBackWhenImportOnNonexistentFile(callSentence);
+    }
+    
+    public void testRollBackWhenFailOnImportTableLobsFromEXTFile() throws SQLException {
+        String callSentence = "call SYSCS_UTIL.SYSCS_IMPORT_TABLE_LOBS_FROM_EXTFILE(" +
+                "null, 'IET1' , '" + nonexistentFileName + "', null, null, null, 0)";
+        realTestRollBackWhenImportOnNonexistentFile(callSentence);
+    }
+    
+    public void testRollBackWhenFailOnImportData() throws SQLException {
+        String callSentence = "call SYSCS_UTIL.SYSCS_IMPORT_DATA(null, 'IMP_EMP', " +
+                "null, null, '" + nonexistentFileName +  "', null, null, null, 1) ";
+        realTestRollBackWhenImportOnNonexistentFile(callSentence);        
+    }  
+    
+    public void testRollBackWhenFailOnImportDataLobsFromExtFile() throws SQLException {
+        String callSentence = "call SYSCS_UTIL.SYSCS_IMPORT_DATA_LOBS_FROM_EXTFILE(" +
+                "null, 'IET1', null, null, '" + nonexistentFileName +
+                "', null, null, null, 1)";
+        
+        realTestRollBackWhenImportOnNonexistentFile(callSentence);
+    }
+    
+    /**
+     * Call passed importSentence and process the error.
+     * @param importSentence a call sentence to to import data from a nonexistent file.
+     */
+    private void realTestRollBackWhenImportOnNonexistentFile(
+            String importSentence) throws SQLException {
+      //import a non-existing file will certainly fail
+        CallableStatement cSt = prepareCall(importSentence);
+        
+        try {
+            cSt.executeUpdate();
+            fail("a SQLException should be thrown " +
+                    "as we import data from a nonexistent file");
+        } catch (SQLException e) {            
+            assertSQLState("XIE0M", e);
+            assertSQLState("XJ058", e.getNextException());            
+        } finally {
+            cSt.close();
+        }
+    }
+}

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java?rev=1096991&r1=1096990&r2=1096991&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
Wed Apr 27 04:28:20 2011
@@ -56,6 +56,7 @@ public class _Suite extends BaseTestCase
         suite.addTest(SysinfoLocaleTest.suite());
         suite.addTest(IjSecurityManagerTest.suite());
         suite.addTest(IjConnNameTest.suite());
+        suite.addTest(RollBackWrappingWhenFailOnImportTest.suite());
         
         // SysinfoAPITest currently fails when run against jars, so is
         // disabled. Only the first jar file on the classpath properly



Mime
View raw message