Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 66608 invoked from network); 10 Feb 2007 01:27:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 10 Feb 2007 01:27:20 -0000 Received: (qmail 81153 invoked by uid 500); 10 Feb 2007 01:27:27 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 81128 invoked by uid 500); 10 Feb 2007 01:27:26 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 81117 invoked by uid 99); 10 Feb 2007 01:27:26 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Feb 2007 17:27:26 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Feb 2007 17:27:17 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 21A321A981A; Fri, 9 Feb 2007 17:26:57 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r505603 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi: BatchUpdateTest.java _Suite.java Date: Sat, 10 Feb 2007 01:26:56 -0000 To: derby-commits@db.apache.org From: myrnavl@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070210012657.21A321A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: myrnavl Date: Fri Feb 9 17:26:56 2007 New Revision: 505603 URL: http://svn.apache.org/viewvc?view=rev&rev=505603 Log: DERBY-2293 - further adjustments as per patches of Feb. 8 and 9. Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BatchUpdateTest.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/_Suite.java Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BatchUpdateTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BatchUpdateTest.java?view=diff&rev=505603&r1=505602&r2=505603 ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BatchUpdateTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BatchUpdateTest.java Fri Feb 9 17:26:56 2007 @@ -43,32 +43,94 @@ import org.apache.derbyTesting.junit.JDBC; import org.apache.derbyTesting.junit.TestConfiguration; - /** * Test BatchUpdate functionality. *

* This test examines the behavior fo BatchUpdate test. - * There are 5 actual fixtures, and even the setup is executing in batch, to - * verify basic create table and insert statements also work in batch. - * The five actual fixtures are: - * testStatementBatchUpdatePositive - verifies correct usage with Statements - * work as expected - * testStatementBatchUpdateNegative - verifies incorrect usage with Statments - * gives appropriate errors - * testCallableStatementBatchUpdate - verifies usage with callableStatements - * works as expected - * testPreparedStatementBatchUpdatePositive - * - verifies correct usage with preparedStatements - * testPreparedStatementBatchUpdateNegative - * - verifies incorrect use with preparedStatements + * One fixture tests creating tables in batch, the other fixtures can be grouped + * into 5 rough categories: + * - tests that verify that correct usage with Statements work as expected + * - testEmptyStatementBatch() + * try executing a batch which nothing in it. + * - testSingleStatementBatch() + * try executing a batch which one statement in it. + * - testMultipleStatementsBatch() + * try executing a batch with 3 different statements in it. + * - test1000StatementsBatch() + * try executing a batch with 1000 statements in it. + * - testAutoCommitTrueBatch() + * try batch with autocommit true + * - testCombinationsOfClearBatch() + * try clear batch + * - testAssociatedParams() + * confirm associated parameters run ok with batches + * + * - tests that verify that incorrect usage with Statments give appropriate + * errors + * - testStatementWithResultSetBatch() + * statements which will return a resultset are not allowed in batch + * update. The following case should throw an exception for select. + * Below trying various placements of select statement in the batch, + * i.e. as 1st stmt, nth stmt and last stmt + * - testStatementNonBatchStuffInBatch() + * try executing a batch with regular statement intermingled. + * - testStatementWithErrorsBatch() + * Below trying various placements of overflow update statement + * in the batch, i.e. as 1st stmt, nth stat and last stmt + * - testTransactionErrorBatch() + * try transaction error, i.e. time out while getting the lock + * + * - tests that verify that usage with callableStatements work as expected + * - testCallableStatementBatch() + * try callable statements + * - testCallableStatementWithOutputParamBatch() + * try callable statement with output parameters + * + * - tests that verify that correct usage with preparedStatements work as + * expected + * - testEmptyValueSetPreparedBatch() + * try executing a batch which nothing in it. + * - testNoParametersPreparedBatch() + * try executing a batch with no parameters. + * (fails with NullPointerException with NetworkServer. See DERBY-2112 + * - testSingleValueSetPreparedBatch() + * try executing a batch which one parameter set in it. + * - testMultipleValueSetPreparedBatch() + * try executing a batch with 3 parameter sets in it. + * - testMultipleValueSetNullPreparedBatch() + * try executing a batch with 2 parameter sets in it and they are set + * to null. + * - test1000ValueSetPreparedBatch() + * try executing a batch with 1000 statements in it. + * - testPreparedStatRollbackAndCommitCombinations() + * try executing batches with various rollback and commit combinations. + * - testAutoCommitTruePreparedStatBatch() + * try prepared statement batch with autocommit true + * - testCombinationsOfClearPreparedStatBatch() + * try clear batch + * + * - tests that verify that incorrect use with preparedStatements give + * appropriate errors + * - testPreparedStmtWithResultSetBatch() + * statements which will return a resultset are not allowed in batch + * update. The following case should throw an exception for select. + * - testPreparedStmtNonBatchStuffInBatch(); + * try executing a batch with regular statement intermingled. + * - testPreparedStmtWithErrorsBatch(); + * trying various placements of overflow update statement + * in the batch + * - testTransactionErrorPreparedStmtBatch() + * try transaction error, in this particular case time out while + * getting the lock * - * The test executes almost all submethods of these fixtures with both - * embedded and NetworkServer/DerbyNetClient - however, there is a difference - * in functionality between the two when an error condition is reaches. Thus, + * Almost all fixtures but 1 execute with embedded and + * NetworkServer/DerbyNetClient - however, there is a difference in + * functionality between the two when an error condition is reaches. Thus, * the negative tests have if / else if blocks for embedded and client. * - * The three subtests that are not running with network server are - * identified with //TODO: tags and have an if (usingEmbedded()) block. + * The 1 fixture that ise not running with network server is + * identified with //TODO: tags and has an if (usingEmbedded()) block and + * a JIRA issue attached to it. * */ @@ -84,56 +146,55 @@ * This is itself a test of statements creating tables in batch. */ public void setUp() throws Exception { - Connection conn = getConnection(); - conn.setAutoCommit(false); - Statement stmt = createStatement(); - stmt.addBatch("create table t1(c1 int)"); - stmt.addBatch("create procedure Integ() language java " + - "parameter style java external name 'java.lang.Integer'"); - stmt.addBatch("create table datetab(c1 date)"); - stmt.addBatch("create table timetab(c1 time)"); - stmt.addBatch("create table timestamptab(c1 timestamp)"); - stmt.addBatch("create table usertypetab(c1 DATE)"); - - int expectedCount[] = {0,0,0,0,0,0}; - assertBatchUpdateCounts(expectedCount, stmt.executeBatch()); - - // for method checkAssociatedParams - stmt.executeUpdate("create table assoc" + - "(x char(10) not null primary key, y char(100))"); - stmt.executeUpdate("create table assocout(x char(10))"); - - conn.commit(); - } - - protected void tearDown() throws Exception { - Statement stmt = createStatement(); - stmt.executeUpdate("DROP TABLE datetab"); - stmt.executeUpdate("DROP TABLE timetab"); - stmt.executeUpdate("DROP TABLE timestamptab"); - stmt.executeUpdate("DROP TABLE usertypetab"); - stmt.executeUpdate("DROP PROCEDURE Integ"); - stmt.executeUpdate("DROP TABLE t1"); - // for method checkAssociatedParams - stmt.executeUpdate("drop table assoc"); - stmt.executeUpdate("drop table assocout"); + getConnection().setAutoCommit(false); + Statement s = createStatement(); + try { + s.execute("delete from t1"); + } catch (SQLException e) {} // ignore if this fails, + // if it's the first time, it *will* fail, thereafter, other things + // will fail anyway. + s.close(); commit(); - super.tearDown(); } public static Test suite() { TestSuite suite = new TestSuite("BatchUpdateTest"); - suite.addTest( - TestConfiguration.defaultSuite(BatchUpdateTest.class)); - + suite.addTest(baseSuite("BatchUpdateTest:embedded")); + suite.addTest(TestConfiguration.clientServerDecorator( + baseSuite("BatchUpdateTest:client"))); + return suite; + } + + protected static Test baseSuite(String name) { + TestSuite suite = new TestSuite(name); + suite.addTestSuite(BatchUpdateTest.class); return new CleanDatabaseTestSetup( - DatabasePropertyTestSetup.setLockTimeouts(suite, 2, 4)); - } + DatabasePropertyTestSetup.setLockTimeouts(suite, 2, 4)) + { + /** + * Creates the tables used in the test cases. + * @exception SQLException if a database error occurs + */ + protected void decorateSQL(Statement stmt) throws SQLException + { + stmt.execute("create table t1(c1 int)"); + // for fixture testCallableStatementBatch + stmt.execute("create table datetab(c1 date)"); + stmt.execute("create table timetab(c1 time)"); + stmt.execute("create table timestamptab(c1 timestamp)"); + stmt.execute("create table usertypetab(c1 DATE)"); + // for fixture testAssociatedParams + stmt.execute("create table assoc" + + "(x char(10) not null primary key, y char(100))"); + stmt.execute("create table assocout(x char(10))"); + } + }; + } /* * helper method to check each count in the return array of batchExecute */ - public void assertBatchUpdateCounts( + private void assertBatchUpdateCounts( int[] expectedBatchResult, int[] executeBatchResult ) { assertEquals("length of array should be identical", @@ -162,218 +223,48 @@ * @param int[] The expectedUpdateCount array. */ protected void assertBatchExecuteError( - String expectedError, - Statement stmt, - int[] expectedUpdateCount) throws SQLException + String expectedError, + Statement stmt, + int[] expectedUpdateCount) + throws SQLException { int[] updateCount; try { updateCount = stmt.executeBatch(); - fail("Expected stmt.batchExecute to fail"); - } catch (SQLException sqle) { - assertSQLState(expectedError, sqle); - assertTrue("Expect BatchUpdateException", - (sqle instanceof BatchUpdateException)); - updateCount = ((BatchUpdateException)sqle).getUpdateCounts(); + fail("Expected batchExecute to fail"); + } catch (BatchUpdateException bue) { + assertSQLState(expectedError, bue); + updateCount = ((BatchUpdateException)bue).getUpdateCounts(); assertBatchUpdateCounts(expectedUpdateCount, updateCount); - } - } - - - /** - * helper method to evaluate negative tests where we expect a - * batchExecuteException to be returned - * - * @exception SQLException Thrown if the expected error occurs. - * We expect a BatchUpdateException, and - * verify it is so. - * - * @param String The sqlstate to look for. - * @param PreparedStatement The PreparedStatement that contains the - * batch to be executed. - * @param int[] The expectedUpdateCount array. - * - */ - protected void assertBatchExecuteError( - String expectedError, - PreparedStatement pstmt, - int[] expectedUpdateCount) throws SQLException - { - int[] updateCount; - try { - updateCount = pstmt.executeBatch(); - fail("Expected pstmt.batchExecute to fail"); - } catch (SQLException sqle) { - assertSQLState(expectedError, sqle); - assertTrue("Expect BatchUpdateException", - (sqle instanceof BatchUpdateException)); - updateCount = ((BatchUpdateException)sqle).getUpdateCounts(); - assertBatchUpdateCounts(expectedUpdateCount, updateCount); - } + } } - /** - * Positive tests for statement batch update. - * - * @exception SQLException Thrown if some unexpected error happens - */ - public void testStatementBatchUpdatePositive() - throws SQLException - { - Connection conn = getConnection(); - Statement stmt = createStatement(); - // try executing a batch which nothing in it. - runEmptyStatementBatch(conn, stmt); - // try executing a batch which one statement in it. - runSingleStatementBatch(conn, stmt); - // try executing a batch with 3 different statements in it. - runMultipleStatementsBatch(conn, stmt); - // try executing a batch with 1000 statements in it. - run1000StatementsBatch(conn, stmt); - // try batch with autocommit true - runAutoCommitTrueBatch(conn, stmt); - // try clear batch - runCombinationsOfClearBatch(conn, stmt); - // confirm associated parameters run ok with batches - checkAssociatedParams(conn, stmt); - conn.commit(); - } - - /** - * Negative tests for statement batch update. - * - * @exception SQLException Thrown if some unexpected error happens - */ - public void testStatementBatchUpdateNegative() throws SQLException - { - Connection conn = getConnection(); - Connection conn2 = openDefaultConnection(); - conn.setAutoCommit(false); - conn2.setAutoCommit(false); - Statement stmt = conn.createStatement(); - Statement stmt2 = conn2.createStatement(); - - // statements which will return a resultset are not allowed in batch - // update. The following case should throw an exception for select. - // Below trying various placements of select statement in the batch, - // i.e. as 1st stmt, nth stmt and last stmt - runStatementWithResultSetBatch(conn, stmt); - - // try executing a batch with regular statement intermingled. - runStatementNonBatchStuffInBatch(conn, stmt); - - // Below trying various placements of overflow update statement - // in the batch, i.e. as 1st stmt, nth stat and last stmt - runStatementWithErrorsBatch(conn, stmt); - - // TODO: When running this with networkserver, we won't be able - // to drop t1 afterwards. Needs researching. - if (usingEmbedded()) - // try transaction error, i.e. time out while getting the lock - runTransactionErrorBatch(conn, stmt, conn2, stmt2); + /* Fixture that verifies tables can be created in batch */ + public void testMinimalDDLInBatch() throws SQLException { - } - - /** - * Tests for callable statement batch update. - * - * @exception SQLException Thrown if some unexpected error happens - */ - public void testCallableStatementBatchUpdate() - throws SQLException - { - Connection conn = getConnection(); - - // try callable statements - runCallableStatementBatch(conn); - - // try callable statement with output parameters - runCallableStatementWithOutputParamBatch(conn); - } - - /** - * Positive tests for prepared statement batch update. - * - * @exception SQLException Thrown if some unexpected error happens - */ - public void testPreparedStatementBatchUpdatePositive() - throws SQLException - { - Connection conn = getConnection(); Statement stmt = createStatement(); + stmt.addBatch("create table ddltsttable1(c1 int)"); + stmt.addBatch("create procedure ddlinteg() language java " + + "parameter style java external name 'java.lang.Integer'"); + stmt.addBatch("create table ddltable2(c1 date)"); + int expectedCount[] = {0,0,0}; + assertBatchUpdateCounts(expectedCount, stmt.executeBatch()); + ResultSet rs = stmt.executeQuery( + "select count(*) from SYS.SYSTABLES where tablename like 'DDL%'"); + JDBC.assertFullResultSet(rs, new String[][] {{"2"}}, true); + rs = stmt.executeQuery( + "select count(*) from SYS.SYSALIASES where alias like 'DDL%'"); + JDBC.assertFullResultSet(rs, new String[][] {{"1"}}, true); - //try executing a batch which nothing in it. - runEmptyValueSetPreparedBatch(conn, stmt); - - // following fails with NullPointerException with NetworkServer - // see DERBY-2112 - if (usingEmbedded()) - // try executing a batch with no parameters. - runNoParametersPreparedBatch(conn, stmt); - - // try executing a batch which one parameter set in it. - runSingleValueSetPreparedBatch(conn, stmt); - - // try executing a batch with 3 parameter sets in it. - runMultipleValueSetPreparedBatch(conn, stmt); - - // try executing a batch with 2 parameter sets in it - // and they are set to null. - runMultipleValueSetNullPreparedBatch(conn, stmt); - - // try executing a batch with 1000 statements in it. - run1000ValueSetPreparedBatch(conn, stmt); - - // try executing batches with various rollback and commit combinations. - runPreparedStatRollbackAndCommitCombinations(conn, stmt); - - // try prepared statement batch with autocommit true - runAutoCommitTruePreparedStatBatch(conn, stmt); - - // try clear batch - runCombinationsOfClearPreparedStatBatch(conn, stmt); - + commit(); } - - /** - * Negative tests for prepared statement batch update. - * - * @exception SQLException Thrown if some unexpected error happens - */ - public void testPreparedStatementBatchUpdateNegative() throws SQLException - { - - Connection conn = getConnection(); - Connection conn2 = openDefaultConnection(); - conn.setAutoCommit(false); - conn2.setAutoCommit(false); - Statement stmt = conn.createStatement(); - Statement stmt2 = conn2.createStatement(); - - // statements which will return a resultset are not allowed in batch - // update. The following case should throw an exception for select. - runPreparedStmtWithResultSetBatch(conn, stmt); - - // try executing a batch with regular statement intermingled. - runPreparedStmtNonBatchStuffInBatch(conn, stmt); - - // Below trying various placements of overflow update statement - // in the batch - runPreparedStmtWithErrorsBatch(conn, stmt); - // TODO: when running this test with NetworkServer, t1 can - // no longer be dropped. Needs research. - if (usingEmbedded()) - // try transaction error, in this particular case time out while - // getting the lock - runTransactionErrorPreparedStmtBatch(conn, stmt, conn2, stmt2); - } - - /* Following are methods used in testStatementUpdateBatchPositive */ + + /* Fixtures that test correct usage of batch handling with Statements */ // try executing a batch which nothing in it. Should work. - protected void runEmptyStatementBatch(Connection conn, Statement stmt) - throws SQLException { + public void testEmptyStatementBatch() throws SQLException { + Statement stmt = createStatement(); int updateCount[]; // try executing a batch which nothing in it. Should work. @@ -383,25 +274,25 @@ assertEquals("expected updateCount of 0", 0, updateCount.length); stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // try executing a batch which single statement in it. Should work. - protected void runSingleStatementBatch(Connection conn, Statement stmt) - throws SQLException { + public void testSingleStatementBatch() throws SQLException { + Statement stmt = createStatement(); println("Positive Statement: testing 1 statement batch"); stmt.addBatch("insert into t1 values(2)"); assertBatchUpdateCounts(new int[] {1}, stmt.executeBatch()); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // try executing a batch with 3 different statements in it. - protected void runMultipleStatementsBatch( - Connection conn, Statement stmt) throws SQLException { + public void testMultipleStatementsBatch() throws SQLException { + + Statement stmt = createStatement(); ResultSet rs; println("Positive Statement: testing 2 inserts and 1 update batch"); @@ -431,14 +322,14 @@ assertEquals("expect 2 rows total", 2, rs.getInt(1)); rs.close(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // try executing a batch with 1000 statements in it. - protected void run1000StatementsBatch(Connection conn, Statement stmt) - throws SQLException { + public void test1000StatementsBatch() throws SQLException { int updateCount[]; + + Statement stmt = createStatement(); ResultSet rs; println("Positive Statement: 1000 statements batch"); @@ -455,16 +346,16 @@ 1000, rs.getInt(1)); rs.close(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // try batch with autocommit true - protected void runAutoCommitTrueBatch(Connection conn, Statement stmt) - throws SQLException { + public void testAutoCommitTrueBatch() throws SQLException { + + getConnection().setAutoCommit(true); + Statement stmt = createStatement(); ResultSet rs; - conn.setAutoCommit(true); // try batch with autocommit true println("Positive Statement: stmt testing with autocommit true"); stmt.addBatch("insert into t1 values(1)"); @@ -477,15 +368,16 @@ assertEquals("expect 0 rows", 0,rs.getInt(1)); rs.close(); - //turn it true again after the above negative test - conn.setAutoCommit(false); - stmt.executeUpdate("delete from t1"); - conn.commit(); + // turn it false again after the above negative test. + // should happen automatically, but just in case + getConnection().setAutoCommit(false); + commit(); } // try combinations of clear batch. - protected void runCombinationsOfClearBatch( - Connection conn, Statement stmt) throws SQLException { + public void testCombinationsOfClearBatch() throws SQLException { + + Statement stmt = createStatement(); ResultSet rs; println("Positive Statement: add 3 statements, clear and execute batch"); @@ -516,8 +408,7 @@ JDBC.assertFullResultSet(rs, new String[][] {{"3"}}, true); rs.close(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } /* @@ -528,15 +419,15 @@ ** that we use for predicates that we give to the access ** manager. */ - protected void checkAssociatedParams(Connection conn, Statement stmt) - throws SQLException + public void testAssociatedParams() throws SQLException { + + Statement stmt = createStatement(); int i; - conn.setAutoCommit(false); println("Positive Statement: testing associated parameters"); - PreparedStatement checkps = conn.prepareStatement( + PreparedStatement checkps = prepareStatement( "select x from assocout order by x"); - PreparedStatement ps = conn.prepareStatement( + PreparedStatement ps = prepareStatement( "insert into assoc values (?, 'hello')"); for ( i = 10; i < 60; i++) { @@ -544,7 +435,7 @@ ps.executeUpdate(); } - ps = conn.prepareStatement( + ps = prepareStatement( "insert into assocout select x from assoc where x like ?"); ps.setString(1, "33%"); ps.addBatch(); @@ -561,7 +452,7 @@ stmt.executeUpdate("delete from assocout"); - ps = conn.prepareStatement( + ps = prepareStatement( "insert into assocout select x from assoc where x like ?"); ps.setString(1, "3%"); ps.addBatch(); // expectedCount 10: values 10-19 @@ -583,7 +474,7 @@ JDBC.assertFullResultSet(rs, expectedStrArray, true); stmt.executeUpdate("delete from assocout"); - ps = conn.prepareStatement( + ps = prepareStatement( "insert into assocout select x from assoc where x like ?"); ps.setString(1, "%");// values 10-59 ps.addBatch(); @@ -610,15 +501,15 @@ JDBC.assertFullResultSet(rs, expectedStrArray2, true); } - /* Following are methods used in testStatementBatchUpdateNegative */ + /* Fixtures that test incorrect batch usage with Statements */ // statements which will return a resultset are not allowed in batch // update. The following case should throw an exception for select. // Below trying various placements of select statement in the batch, // i.e. as 1st stmt, nth stat and last stmt - protected void runStatementWithResultSetBatch( - Connection conn, Statement stmt) throws SQLException { + public void testStatementWithResultSetBatch() throws SQLException { + Statement stmt = createStatement(); ResultSet rs; // trying select as the first statement @@ -684,22 +575,21 @@ "There should now be 5 rows in the table", 5, rs.getInt(1)); rs.close(); - conn.rollback(); + rollback(); rs = stmt.executeQuery("select count(*) from t1"); rs.next(); assertEquals("There should be no rows in the table after rollback", - 0, rs.getInt(1)); + 0, rs.getInt(1)); rs.close(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // try executing a batch with regular statement intermingled. - protected void runStatementNonBatchStuffInBatch( - Connection conn, Statement stmt) throws SQLException { + public void testStatementNonBatchStuffInBatch() throws SQLException { + Statement stmt = createStatement(); int[] updateCount=null; ResultSet rs; @@ -716,7 +606,7 @@ stmt.addBatch("insert into t1 values(1)"); assertBatchExecuteError("XJ208",stmt, new int[] {-3,1}); // pull level with embedded situation - conn.rollback(); + rollback(); } // do clearBatch so we can proceed stmt.clearBatch(); @@ -726,27 +616,30 @@ assertEquals("There should be no rows in the table", 0, rs.getInt(1)); rs.close(); - try + // trying executeQuery after addBatch + println("Negative Statement: " + + "statement testing executeQuery in the middle of batch"); + stmt.addBatch("insert into t1 values(1)"); + if (usingEmbedded()) + { + try + { + stmt.executeQuery("SELECT * FROM SYS.SYSTABLES"); + fail("Expected executeQuerywith embedded"); + } catch (SQLException sqle) { + /* Check to be sure the exception is the MIDDLE_OF_BATCH */ + assertSQLState("XJ068", sqle); + // do clearBatch so we can proceed + stmt.clearBatch(); + } + } + else if (usingDerbyNetClient()) { - // trying executeQuery after addBatch - println("Negative Statement: " + - "statement testing executeQuery in the middle of batch"); - stmt.addBatch("insert into t1 values(1)"); stmt.executeQuery("SELECT * FROM SYS.SYSTABLES"); updateCount = stmt.executeBatch(); - if (usingEmbedded()) - fail("Expected executeBatch to fail with embedded"); - else if (usingDerbyNetClient()) - { - assertBatchUpdateCounts(new int[] {1}, updateCount); - // set to same spot as embedded - conn.rollback(); - } - } catch (SQLException sqle) { - /* Check to be sure the exception is the MIDDLE_OF_BATCH */ - assertSQLState("XJ068", sqle); - // do clearBatch so we can proceed - stmt.clearBatch(); + assertBatchUpdateCounts(new int[] {1}, updateCount); + // set to same spot as embedded + rollback(); } rs = stmt.executeQuery("select count(*) from t1"); @@ -756,12 +649,12 @@ println("Negative Statement: " + "statement testing executeUpdate in the middle of batch"); + // trying executeUpdate after addBatch + println("Negative Statement: " + + "statement testing executeUpdate in the middle of batch"); + stmt.addBatch("insert into t1 values(1)"); try { - // trying executeUpdate after addBatch - println("Negative Statement: " + - "statement testing executeUpdate in the middle of batch"); - stmt.addBatch("insert into t1 values(1)"); stmt.executeUpdate("insert into t1 values(1)"); stmt.addBatch("insert into t1 values(1)"); stmt.addBatch("SELECT * FROM SYS.SYSCOLUMNS"); @@ -794,22 +687,21 @@ 3, rs.getInt(1)); rs.close(); - conn.rollback(); + rollback(); rs = stmt.executeQuery("select count(*) from t1"); rs.next(); assertEquals("There should be no rows in the table", 0, rs.getInt(1)); rs.close(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // Below trying various placements of overflow update statement in the // batch, i.e. as 1st stmt, nth stmt and last stmt - protected void runStatementWithErrorsBatch( - Connection conn, Statement stmt) throws SQLException { + public void testStatementWithErrorsBatch() throws SQLException { + Statement stmt = createStatement(); ResultSet rs; stmt.executeUpdate("insert into t1 values(1)"); @@ -878,62 +770,70 @@ assertEquals("expected: 6 rows", 6, rs.getInt(1)); rs.close(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } // try transaction error, in this particular case time out while // getting the lock - protected void runTransactionErrorBatch( - Connection conn, Statement stmt, - Connection conn2, Statement stmt2) throws SQLException { + public void testTransactionErrorBatch() throws SQLException { + // conn is just default connection + Connection conn = getConnection(); + Connection conn2 = openDefaultConnection(); + conn.setAutoCommit(false); + conn2.setAutoCommit(false); + Statement stmt = conn.createStatement(); + Statement stmt2 = conn2.createStatement(); + int[] updateCount = null; - try - { - println("Negative Statement: statement testing time out" + - " while getting the lock in the batch"); + println("Negative Statement: statement testing time out" + + " while getting the lock in the batch"); + + stmt.execute("insert into t1 values(1)"); + stmt2.execute("insert into t1 values(2)"); - stmt.execute("insert into t1 values(1)"); - stmt2.execute("insert into t1 values(2)"); + stmt.addBatch("update t1 set c1=3 where c1=2"); + stmt2.addBatch("update t1 set c1=4 where c1=1"); - stmt.addBatch("update t1 set c1=3 where c1=2"); - stmt2.addBatch("update t1 set c1=4 where c1=1"); - + try + { stmt.executeBatch(); - updateCount = stmt2.executeBatch(); fail ("Batch is expected to fail"); - } catch (SQLException sqle) { + updateCount = stmt2.executeBatch(); + } catch (BatchUpdateException bue) { /* Ensure the exception is time out while getting lock */ - assertSQLState("40XL1",sqle); - assertTrue("we should get a BatchUpdateException", - (sqle instanceof BatchUpdateException)); - updateCount = ((BatchUpdateException)sqle).getUpdateCounts(); + if (usingEmbedded()) + assertSQLState("40XL1", bue); + else if (usingDerbyNetClient()) + assertSQLState("XJ208", bue); + updateCount = ((BatchUpdateException)bue).getUpdateCounts(); if (updateCount != null) { - assertEquals("first statement in the batch caused time out" + - " while getting the lock, expect no update count", - 0, updateCount.length); + if (usingEmbedded()) + assertEquals("first statement in the batch caused time out" + + " while getting the lock, there should be no update count", + 0, updateCount.length); + else if (usingDerbyNetClient()) + /* first statement in the batch caused time out while getting + * the lock, there should be 1 update count of -3 */ + assertBatchUpdateCounts(new int[] {-3}, updateCount); } } conn.rollback(); conn2.rollback(); stmt.clearBatch(); stmt2.clearBatch(); - stmt.executeUpdate("delete from t1"); - conn.commit(); + commit(); } - /* Following are methods used in testCallableStatementBatchUpdate */ + /* Fixtures that test batch updates with CallableStatements */ // try callable statements - protected void runCallableStatementBatch(Connection conn) - throws SQLException { - conn.setAutoCommit(false); - + public void testCallableStatementBatch() throws SQLException { + println("Positive Callable Statement: " + "statement testing callable statement batch"); - CallableStatement cs = conn.prepareCall("insert into t1 values(?)"); + CallableStatement cs = prepareCall("insert into t1 values(?)"); cs.setInt(1, 1); cs.addBatch(); @@ -947,14 +847,14 @@ { fail("The executeBatch should have succeeded"); } - cleanUpCallableStatement(conn, cs, "t1"); + cleanUpCallableStatement(cs, "t1"); /* For 'beetle' bug 2813 - setDate/setTime/setTimestamp * calls on callableStatement throws ClassNotFoundException * verify setXXXX() works with Date, Time and Timestamp * on CallableStatement. */ - cs = conn.prepareCall("insert into datetab values(?)"); + cs = prepareCall("insert into datetab values(?)"); cs.setDate(1, Date.valueOf("1990-05-05")); cs.addBatch(); @@ -968,9 +868,9 @@ { fail("The executeBatch should have succeeded"); } - cleanUpCallableStatement(conn, cs, "datetab"); + cleanUpCallableStatement(cs, "datetab"); - cs = conn.prepareCall("insert into timetab values(?)"); + cs = prepareCall("insert into timetab values(?)"); cs.setTime(1, Time.valueOf("11:11:11")); cs.addBatch(); @@ -984,9 +884,9 @@ { fail("The executeBatch should have succeeded"); } - cleanUpCallableStatement(conn, cs, "timestamptab"); + cleanUpCallableStatement(cs, "timestamptab"); - cs = conn.prepareCall("insert into timestamptab values(?)"); + cs = prepareCall("insert into timestamptab values(?)"); cs.setTimestamp(1, Timestamp.valueOf("1990-05-05 11:11:11.1")); cs.addBatch(); @@ -1000,10 +900,10 @@ { fail("The executeBatch should have succeeded"); } - cleanUpCallableStatement(conn, cs, "timestamptab"); + cleanUpCallableStatement(cs, "timestamptab"); // Try with a user type - cs = conn.prepareCall("insert into usertypetab values(?)"); + cs = prepareCall("insert into usertypetab values(?)"); cs.setObject(1, Date.valueOf("1990-05-05")); cs.addBatch(); @@ -1017,10 +917,10 @@ { fail("The executeBatch should have succeeded"); } - cleanUpCallableStatement(conn, cs, "usertypetab"); + cleanUpCallableStatement(cs, "usertypetab"); } - // helper method to runCallableStatementBatch + // helper method to testCallableStatementBatch // executes and evaluates callable statement private static void executeBatchCallableStatement(CallableStatement cs) throws SQLException @@ -1029,53 +929,46 @@ updateCount = cs.executeBatch(); assertEquals("there were 2 statements in the batch", - 2, updateCount.length); + 2, updateCount.length); for (int i=0; i