db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From myrn...@apache.org
Subject svn commit: r827793 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang: DBOperations.java UniqueConstraintMultiThreadedTest.java _Suite.java
Date Tue, 20 Oct 2009 21:08:49 GMT
Author: myrnavl
Date: Tue Oct 20 21:08:48 2009
New Revision: 827793

URL: http://svn.apache.org/viewvc?rev=827793&view=rev
Log:
DERBY-3473; write test cases to test locking during tree operation
  patch contributed by Anurag Shekhar (anurag dot shekhar at sun dot com)

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DBOperations.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UniqueConstraintMultiThreadedTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DBOperations.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DBOperations.java?rev=827793&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DBOperations.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DBOperations.java
Tue Oct 20 21:08:48 2009
@@ -0,0 +1,110 @@
+/*
+
+Derby - Class org.apache.derbyTesting.functionTests.tests.lang.DBOperations
+
+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.lang;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+class DBOperations implements Runnable {
+    private Connection con;
+    private int keyVal;
+    private SQLException exception;
+    private Throwable unexpectedException;
+    
+    /**
+     * Instantiates DBOperation object.
+     * @param con Connection to be used within this object.
+     * @param keyValue key value while executing dmls.
+     * @return 
+     */
+    DBOperations(Connection con, int keyValue) throws SQLException {
+        this.con = con;
+        this.keyVal = keyValue;
+        con.setAutoCommit(false);
+    }
+    
+    /**
+     * Deletes the record with key value passed in constroctor.
+     */
+    void delete () throws SQLException {
+        Statement stmt = con.createStatement();
+        stmt.execute("delete from tab1 where i = " + keyVal);
+        stmt.close();
+    }
+    
+    /**
+     * Inserts a record with key value passed in constroctor.
+     */
+    void insert () throws SQLException {
+        Statement stmt = con.createStatement();
+        try {
+            
+            stmt.executeUpdate("insert into tab1 values ("+keyVal+")");
+        }
+        catch (SQLException e) {
+            exception = e;
+        }
+        stmt.close();
+    }
+    
+    /**
+     * Rollbacks the transaction.
+     */
+    void rollback () throws SQLException {
+        con.rollback();
+    }
+    
+    /**
+     * Returns the SQLException received while executing insert.
+     * Null if no transaction was received.
+     * @return SQLException
+     */
+    SQLException getException () {
+        return exception;
+    }
+    
+    /**
+     * commits the trasnaction.
+     */
+    void commit () throws SQLException {
+        con.commit();
+    } 
+    
+    /**
+     * Returns if any unexpected trasnaction was thrown during any 
+     * of the operation.
+     * @return Throwable
+     */
+    public Throwable getUnexpectedException() {
+        return unexpectedException;
+    } 
+
+    public void run() {
+        try {
+            insert();
+        }
+        catch (Throwable e) {
+            unexpectedException = e;
+        }
+    }
+}

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

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UniqueConstraintMultiThreadedTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UniqueConstraintMultiThreadedTest.java?rev=827793&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UniqueConstraintMultiThreadedTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UniqueConstraintMultiThreadedTest.java
Tue Oct 20 21:08:48 2009
@@ -0,0 +1,141 @@
+/*
+
+Derby - Class org.apache.derbyTesting.functionTests.tests.lang.UniqueConstraintMultiThrededTest
+
+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.lang;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.Statement;
+import javax.sql.DataSource;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.JDBCDataSource;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+public class UniqueConstraintMultiThreadedTest extends BaseJDBCTestCase {
+    
+    private DataSource ds;
+    public UniqueConstraintMultiThreadedTest() {
+        super ("Multi Threaded Unique Constraint Test");
+    }
+    
+    /**
+     * Deletes a record in a transaction and tries to insert the same 
+     * from a different transaction. Once second transaction goes on wait
+     * first trasnaction is commited or rolled back based on third 
+     * param (boolean commit).
+     * 
+     * @param isolation1 isolation level for 1st thread
+     * @param isolation2 isolation level for 2nd thread
+     * @param commit wether to commit or commit
+     */
+    private void executeThreads (int isolation1, int isolation2, 
+            boolean commit) throws Exception {
+        Connection con1 = ds.getConnection();
+        con1.setTransactionIsolation(isolation1);
+        Connection con2 = ds.getConnection();
+        try {
+            con2.setTransactionIsolation(isolation2);
+            DBOperations dbo1 = new DBOperations (con1, 5);
+            DBOperations dbo2 = new DBOperations (con2, 5);
+            dbo1.delete();
+            Thread t = new Thread (dbo2);
+            t.start();
+            //wait for 2 sec should be enough for dbo2 so on wait
+            t.sleep(2000);
+            if (commit) {
+                dbo1.rollback();
+                t.join();
+                assertSQLState("isolation levels: " + isolation1
+                        + " " + isolation2, "23505", dbo2.getException());
+            }
+            else {
+                dbo1.commit();
+                t.join();
+                assertNull("isolation levels: " + isolation1
+                        + " " + isolation2, dbo2.getException());
+            }
+            assertNull("unexpected failure: " + isolation1
+                        + " " + isolation2, dbo2.getUnexpectedException());
+        }
+        finally {
+            con1.commit();
+            con2.commit();
+            con1.close();
+            con2.close();
+        }
+        
+    }
+    
+    /**
+     * Test inserting a duplicate record while original is deleted in
+     * a transaction and latter commited.
+     */
+    public void testLockingWithcommit () throws Exception {
+        ds = JDBCDataSource.getDataSource();
+        for (int i = 0; i < 4; i++) {
+            for (int j = 0; j < 4; j++) {
+                executeThreads((int) Math.pow(2,i),
+                        (int) Math.pow (2,j), true);
+            }
+        }        
+    }
+    
+    /**
+     * Test inserting a duplicate record while original is deleted in
+     * a transaction and latter rolled back.
+     */
+    public void testLockingWithRollback () throws Exception {
+        ds = JDBCDataSource.getDataSource();
+        for (int i = 0; i < 4; i++) {
+            for (int j = 0; j < 4; j++) {
+                executeThreads((int) Math.pow (2,i),
+                        (int) Math.pow (2,j), false);
+            }
+        }
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite("UniqueConstraintTest");
+        suite.addTest(TestConfiguration.embeddedSuite(UniqueConstraintMultiThreadedTest.class));
+        return suite;
+    }
+    
+    protected void setUp() throws Exception {
+        Connection con = getConnection();
+        Statement stmt = con.createStatement();
+        stmt.execute("create table tab1 (i integer)");
+        stmt.executeUpdate("alter table tab1 add constraint con1 unique (i)");
+        PreparedStatement ps = con.prepareStatement("insert into tab1 " +
+                "values (?)");
+        for (int i = 0; i < 10; i++) {
+            ps.setInt(1, i);
+            ps.executeUpdate();
+        }
+        con.commit ();
+    }
+
+    protected void tearDown() throws java.lang.Exception {
+        Connection con = getConnection();
+        con.createStatement().executeUpdate("drop table tab1");
+    }
+}

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?rev=827793&r1=827792&r2=827793&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
Tue Oct 20 21:08:48 2009
@@ -140,6 +140,7 @@
         suite.addTest(ConglomerateSharingTest.suite());
         suite.addTest(NullableUniqueConstraintTest.suite());
         suite.addTest(UniqueConstraintSetNullTest.suite());
+        suite.addTest(UniqueConstraintMultiThreadedTest.suite());
         suite.addTest(ViewsTest.suite());
         suite.addTest(DeadlockModeTest.suite());
         suite.addTest(AnsiSignaturesTest.suite());



Mime
View raw message