db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bakk...@apache.org
Subject svn commit: r293585 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/sql/ engine/org/apache/derby/impl/jdbc/ engine/org/apache/derby/impl/sql/ engine/org/apache/derby/impl/sql/execute/ testing/org/apache/derbyTesting/functionTests/master/ t...
Date Tue, 04 Oct 2005 11:05:59 GMT
Author: bakksjo
Date: Tue Oct  4 04:05:26 2005
New Revision: 293585

URL: http://svn.apache.org/viewcvs?rev=293585&view=rev
Log:
Fixes DERBY-579 by not storing the timeout value in the
GenericPreparedStatement class, just passing it through.

SetQueryTimeoutTest updated, now uses a server-side function to delay
queries. This gives predictability and because of that, the running
time of the test has been significantly reduced. Also, the test now
uses the same tables in multiple statements, in order to hit the
statement cache and check that the timeout affects only the right
statements.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/PreparedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericPreparedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ConstraintConstantAction.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/SetQueryTimeoutTest.out
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/SetQueryTimeoutTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/PreparedStatement.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/PreparedStatement.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/PreparedStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/PreparedStatement.java Tue Oct
 4 04:05:26 2005
@@ -106,6 +106,7 @@
  	 * @param rollbackParentContext True if 1) the statement context is
 	 *  NOT a top-level context, AND 2) in the event of a statement-level
 	 *	 exception, the parent context needs to be rolled back, too.
+     * @param timeoutMillis timeout value in milliseconds.
 	 *
 	 * @return	A ResultSet for a statement. A ResultSet represents
 	 *		the results returned from the statement, if any.
@@ -114,25 +115,21 @@
 	 *
 	 * @exception StandardException		Thrown on failure
 	 */
-	ResultSet	execute(Activation activation, boolean executeQuery, boolean executeUpdate,
-		boolean rollbackParentContext) throws StandardException;
+    ResultSet execute(Activation activation,
+                      boolean executeQuery,
+                      boolean executeUpdate,
+                      boolean rollbackParentContext,
+                      long timeoutMillis)
+        throws StandardException;
 
 	/**
 		Simple form of execute(). Creates a new single use activation and executes it,
 		but also passes rollbackParentContext parameter (see above).
 	*/
-	ResultSet	execute(LanguageConnectionContext lcc, boolean rollbackParentContext)
-		throws StandardException;
-
-    /**
-     * Sets the timeout for execution of this statement.
-     * The timeout value will be set in the assigned StatementContext object.
-     *
-     * @param timeoutMillis timeout value in milliseconds.
-     *
-     * @see org.apache.derby.iapi.sql.conn.StatementContext
-     */
-    void setQueryTimeout(long timeoutMillis);
+    ResultSet execute(LanguageConnectionContext lcc,
+                      boolean rollbackParentContext,
+                      long timeoutMillis)
+        throws StandardException;
 
 	/**
 	 * Get the ResultDescription for the statement.  The ResultDescription

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java Tue Oct
 4 04:05:26 2005
@@ -3200,8 +3200,7 @@
                     act.getParameterValueSet().getParameterForSet(paramPosition++).setValue(currentRow.getColumn(i));
             }
             // Don't set any timeout when updating rows (use 0)
-            ps.setQueryTimeout(0L);
-            org.apache.derby.iapi.sql.ResultSet rs = ps.execute(act, false, true, true);
//execute the update where current of sql
+            org.apache.derby.iapi.sql.ResultSet rs = ps.execute(act, false, true, true, 0L);
//execute the update where current of sql
             rs.close();
             rs.finish();
             //For forward only resultsets, after a update, the ResultSet will be positioned
right before the next row.
@@ -3246,8 +3245,7 @@
                 StatementContext statementContext = lcc.pushStatementContext(isAtomic, deleteWhereCurrentOfSQL.toString(),
null, false, 0L);
                 org.apache.derby.iapi.sql.PreparedStatement ps = lcc.prepareInternalStatement(deleteWhereCurrentOfSQL.toString());
                 // Don't set any timeout when deleting rows (use 0)
-                ps.setQueryTimeout(0L);
-                org.apache.derby.iapi.sql.ResultSet rs = ps.execute(lcc, true); //execute
delete where current of sql
+                org.apache.derby.iapi.sql.ResultSet rs = ps.execute(lcc, true, 0L); //execute
delete where current of sql
                 rs.close();
                 rs.finish();
                 //For forward only resultsets, after a delete, the ResultSet will be positioned
right before the next row.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java Tue Oct
 4 04:05:26 2005
@@ -1138,8 +1138,11 @@
 				a.reset();
 				a.setMaxRows(maxRows);
                 long timeoutMillis = (long)timeoutSeconds * 1000L;
-                ps.setQueryTimeout(timeoutMillis);
-				ResultSet resultsToWrap = ps.execute(a, executeQuery, executeUpdate, false);
+                ResultSet resultsToWrap = ps.execute(a,
+                                                     executeQuery,
+                                                     executeUpdate,
+                                                     false,
+                                                     timeoutMillis);
 				getWarnings(a.getWarnings());
 
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericPreparedStatement.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericPreparedStatement.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericPreparedStatement.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericPreparedStatement.java
Tue Oct  4 04:05:26 2005
@@ -138,7 +138,6 @@
 	private String execSchemaName;
 	protected boolean isAtomic;
 	protected String sourceTxt;
-    private long timeoutMillis; // Timeout value, in milliseconds.
 
 	private int inUseCount;
 
@@ -181,7 +180,6 @@
 		UUIDValue = uuidFactory.createUUID();
 		UUIDString = UUIDValue.toString();
 		spsAction = false;
-        timeoutMillis = 0L; // 0 means no timeout; default.
 	}
 
 	/**
@@ -238,24 +236,14 @@
 		return ac;
 	}
 
-    /**
-     * Sets a timeout value for execution of this statement.
-     * Will also apply to each row fetch from the ResultSet
-     * produced by this statement.
-     *
-     * @param timeoutMillis Timeout value in milliseconds. 0 means no timeout.
-     */
-    public void setQueryTimeout(long timeoutMillis)
-    {
-        this.timeoutMillis = timeoutMillis;
-    }
-
-	public ResultSet execute(LanguageConnectionContext lcc, boolean rollbackParentContext)
+    public ResultSet execute(LanguageConnectionContext lcc,
+                             boolean rollbackParentContext,
+                             long timeoutMillis)
 		throws StandardException
 	{
 		Activation a = getActivation(lcc, false);
 		a.setSingleExecution();
-		return execute(a, false, false, rollbackParentContext);
+		return execute(a, false, false, rollbackParentContext, timeoutMillis);
 	}
 
 	/**
@@ -267,14 +255,19 @@
 	  * @param rollbackParentContext True if 1) the statement context is
 	  *  NOT a top-level context, AND 2) in the event of a statement-level
 	  *	 exception, the parent context needs to be rolled back, too.
+      * @param timeoutMillis timeout value in milliseconds.
 	  *	@return	the result set to be pawed through
 	  *
 	  *	@exception	StandardException thrown on error
 	  */
 
-	public ResultSet execute
-	(Activation activation, boolean executeQuery, boolean executeUpdate,
-		boolean rollbackParentContext) throws StandardException 
+    public ResultSet execute(Activation activation,
+                             boolean executeQuery,
+                             boolean executeUpdate,
+                             boolean rollbackParentContext,
+                             long timeoutMillis)
+        throws
+            StandardException 
 	{
 		boolean				needToClearSavePoint = false;
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
Tue Oct  4 04:05:26 2005
@@ -2056,9 +2056,7 @@
         // This is a substatement; for now, we do not set any timeout
         // for it. We might change this behaviour later, by linking
         // timeout to its parent statement's timeout settings.
-        ps.setQueryTimeout(0L);
-
-		ResultSet rs = ps.execute(lcc, true);
+		ResultSet rs = ps.execute(lcc, true, 0L);
 		rs.close();
 		rs.finish();
 	}
@@ -2080,9 +2078,7 @@
 
         // This is a substatement, for now we do not set any timeout for it
         // We might change this later by linking timeout to parent statement
-        ps.setQueryTimeout(0L);
-
-		ResultSet rs = ps.execute(lcc, false);
+		ResultSet rs = ps.execute(lcc, false, 0L);
 		DataValueDescriptor[] rowArray = rs.getNextRow().getRowArray();
 		rs.close();
 		rs.finish();

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ConstraintConstantAction.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ConstraintConstantAction.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ConstraintConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ConstraintConstantAction.java
Tue Oct  4 04:05:26 2005
@@ -313,9 +313,7 @@
             // This is a substatement; for now, we do not set any timeout
             // for it. We might change this behaviour later, by linking
             // timeout to its parent statement's timeout settings.
-            ps.setQueryTimeout(0L);
-
-			rs = ps.execute(lcc, false);
+			rs = ps.execute(lcc, false, 0L);
 			ExecRow row = rs.getNextRow();
 			if (SanityManager.DEBUG)
 			{

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java
Tue Oct  4 04:05:26 2005
@@ -166,9 +166,7 @@
                 // This is a substatement; for now, we do not set any timeout
                 // for it. We might change this behaviour later, by linking
                 // timeout to its parent statement's timeout settings.
-                ps.setQueryTimeout(0L);
-
-				ResultSet rs = ps.execute(spsActivation, false, false, false);
+				ResultSet rs = ps.execute(spsActivation, false, false, false, 0L);
                 if( rs.returnsRows())
                 {
                     // Fetch all the data to ensure that functions in the select list or
values statement will

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/SetQueryTimeoutTest.out
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/SetQueryTimeoutTest.out?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/SetQueryTimeoutTest.out
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/SetQueryTimeoutTest.out
Tue Oct  4 04:05:26 2005
@@ -2,17 +2,13 @@
 Got connections
 Preparing for testing queries with timeout
 Initializing tables with prefix t
-Initializing tables with prefix u
-Initializing tables with prefix v
-Initializing tables with prefix x
 Testing timeout with fetch operations
 Statement 0 timed out
 Statement 1 completed
 Statement 2 completed
 Statement 3 completed
 Testing timeout with an execute operation
-Statement 0 timed out
-Statement 1 completed
+Statements that should time out timed out, and statements that should complete completed
 Testing setting a negative timeout value
 Negative timeout value caused exception, as expected
 Execute returned a ResultSet

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/SetQueryTimeoutTest.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/SetQueryTimeoutTest.java?rev=293585&r1=293584&r2=293585&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/SetQueryTimeoutTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/SetQueryTimeoutTest.java
Tue Oct  4 04:05:26 2005
@@ -39,12 +39,11 @@
  * This test consists of three parts:
  *
  * 1. Executes a SELECT query in 4 different threads concurrently.
- *    The query is designed to make the execution time for each
- *    ResultSet.next() operation unpredictable, and in the order
- *    of seconds for many of them. The executeQuery() call finishes
- *    quickly, but the fetch operations may take longer time than
- *    the timeout value set. Hence, this part tests getting timeouts
- *    from calls to ResultSet.next().
+ *    The query calls a user-defined, server-side function which
+ *    delays the execution, so that it takes several seconds even
+ *    though the data volume is really low. The fetch operations
+ *    take longer time than the timeout value set. Hence, this part
+ *    tests getting timeouts from calls to ResultSet.next().
  * 
  *    Two connections are used, two threads execute their statement
  *    in the context of one connection, the other two threads in the
@@ -54,7 +53,7 @@
  *    regardless of what connection/transaction it and other statements
  *    are executed in the context of.
  *    
- * 2. Executes a long-running INSERT query in two threads.
+ * 2. Executes an INSERT query in multiple threads.
  *    This part tests getting timeouts from calls to Statement.execute().
  *    Each thread executes the query in the context of a separate
  *    connection. There is no point in executing multiple statements
@@ -64,9 +63,9 @@
  *    calls to ResultSet.next() may be interleaved between the different
  *    threads).
  *
- *    Only one thread executes its statement with a timeout value set,
- *    this is to verify that the correct statement is affected by the
- *    timeout, while the other statement executes to completion.
+ *    Half of the threads execute their statement with a timeout value set,
+ *    this is to verify that the correct statements are affected by the
+ *    timeout, while the other statements execute to completion.
  *
  * 3. Sets an invalid (negative) timeout. Verifies that the correct
  *    exception is thrown.
@@ -75,7 +74,8 @@
  */
 public class SetQueryTimeoutTest
 {
-    private static final int TIMEOUT = 3; // In seconds
+    private static final int TIMEOUT = 1; // In seconds
+    private static final int CONNECTIONS = 100;
 
     private static void printSQLException(SQLException e)
     {
@@ -151,10 +151,10 @@
         throws
             TestFailedException
     {
-        PreparedStatement statement = null;
+        Statement statement = null;
         try {
-            statement = connection.prepareStatement(queryString);
-            statement.execute();
+            statement = connection.createStatement();
+            statement.execute(queryString);
         } catch (SQLException e) {
             String sqlState = e.getSQLState();
             if (!ignoreExceptions.contains(sqlState)) {
@@ -192,12 +192,8 @@
         Collection ignore = new HashSet();
         ignore.add(SQLState.LANG_OBJECT_DOES_NOT_EXIST.substring(0,5));
         
-        exec(conn, "drop table " + tablePrefix + "0", ignore);
-        exec(conn, "drop table " + tablePrefix + "1", ignore);
-        exec(conn, "drop table " + tablePrefix + "2", ignore);
-        exec(conn, "drop table " + tablePrefix + "3", ignore);
-        exec(conn, "drop table " + tablePrefix + "4", ignore);
-        exec(conn, "drop table " + tablePrefix + "5", ignore);
+        exec(conn, "drop table " + tablePrefix + "_orig", ignore);
+        exec(conn, "drop table " + tablePrefix + "_copy", ignore);
     }
     
     private static void prepareTables(Connection conn, String tablePrefix)
@@ -209,64 +205,32 @@
         dropTables(conn, tablePrefix);
         
         exec(conn,
-             "create table " + tablePrefix + "1 (a int, b char(1))");
+             "create table " + tablePrefix + "_orig (a int)");
 
         exec(conn,
-             "create table " + tablePrefix + "2 (a int, b char(2))");
-        
-        exec(conn,
-             "create table " + tablePrefix + "3 (a int, b char(4))");
-
-        exec(conn,
-             "create table " + tablePrefix + "4 (a int, b char(6))");
-
-        exec(conn,
-             "create table " + tablePrefix + "5 (a int, b char(8))");
+             "create table " + tablePrefix + "_copy (a int)");
 
         exec(conn,
              "insert into "
-             + tablePrefix + "1"
-             + " values(3,'a')"
-             + ",(7,'b')"
-             + ",(13,'c')"
-             + ",(37,'d')"
-             + ",(141,'e')"
-             + ",(1,'f')");
-        
+             + tablePrefix + "_orig"
+             + " values(0),(1),(2),(3),(4),(5),(6)");
+    }
 
-        exec(conn,
-             "insert into "
-             + tablePrefix + "2 select "
-             + tablePrefix + "1.a+"
-             + tablePrefix + "x.a,"
-             + tablePrefix + "1.b||"
-             + tablePrefix + "x.b from "
-             + tablePrefix + "1 join "
-             + tablePrefix + "1 as "
-             + tablePrefix + "x on 1=1");
-        
-        exec(conn,
-             "insert into "
-             + tablePrefix + "3 select "
-             + tablePrefix + "2.a+"
-             + tablePrefix + "x.a,"
-             + tablePrefix + "2.b||"
-             + tablePrefix + "x.b from "
-             + tablePrefix + "2 join "
-             + tablePrefix + "2 as "
-             + tablePrefix + "x on 1=1");
-        
-        exec(conn,
-             "insert into "
-             + tablePrefix + "4 select "
-             + tablePrefix + "3.a+"
-             + tablePrefix + "2.a,"
-             + tablePrefix + "3.b||"
-             + tablePrefix + "2.b from "
-             + tablePrefix + "3 join "
-             + tablePrefix + "2 on 1=1");
+    /**
+     * This is the user-defined function which is called from our queries
+     */
+    public static int delay(int seconds, int value)
+        throws
+            SQLException
+    {
+        try {
+            Thread.sleep(seconds * 1000);
+        } catch (InterruptedException e) {
+            // Ignore
+        }
+        return value;
     }
-    
+
     private static void prepareForTimedQueries(Connection conn)
         throws
             TestFailedException
@@ -279,35 +243,29 @@
             throw new TestFailedException("Should not happen", e);
         }
 
+        exec(conn, "CREATE FUNCTION DELAY(SECONDS INTEGER, VALUE INTEGER) RETURNS INTEGER
PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.tests.jdbcapi.SetQueryTimeoutTest.delay'");
+
         prepareTables(conn, "t");
-        prepareTables(conn, "u");
-        prepareTables(conn, "v");
-        prepareTables(conn, "x");
     }
     
     private static String getFetchQuery(String tablePrefix)
     {
-        return "select "
-            + tablePrefix + "4.a+"
-            + tablePrefix + "3.a,"
-            + tablePrefix + "4.b||"
-            + tablePrefix + "3.b from "
-            + tablePrefix + "4 left join "
-            + tablePrefix + "3 on 1=1 where mod("
-            + tablePrefix + "4.a+"
-            + tablePrefix + "3.a,1000)=0";
+        /**
+         * The reason for using the mod function here is to force
+         * at least one invocation of ResultSet.next() to read
+         * more than one row from the table before returning.
+         * This is necessary since timeout is checked only when
+         * reading rows from base tables, and when the first row
+         * is read, the query still has not exceeded the timeout.
+         */
+        return "select a from " + tablePrefix + "_orig where mod(DELAY(1,a),3)=0";
     }
     
     private static String getExecQuery(String tablePrefix)
     {
         return "insert into "
-            + tablePrefix + "5 select "
-            + tablePrefix + "3.a+"
-            + tablePrefix + "x.a,"
-            + tablePrefix + "3.b from "
-            + tablePrefix + "3 left join "
-            + tablePrefix + "3 as "
-            + tablePrefix + "x on 1=1";
+            + tablePrefix + "_copy select a from "
+            + tablePrefix + "_orig where DELAY(1,1)=1";
     }
     
     private static class StatementExecutor
@@ -330,10 +288,12 @@
             this.timeout = timeout;
             highestRunTime = 0;
             sqlException = null;
-            try {
-                statement.setQueryTimeout(timeout);
-            } catch (SQLException e) {
-                sqlException = e;
+            if (timeout > 0) {
+                try {
+                    statement.setQueryTimeout(timeout);
+                } catch (SQLException e) {
+                    sqlException = e;
+                }
             }
         }
         
@@ -469,9 +429,9 @@
         // D - here just to create equal contention on conn1 and conn2
         
         PreparedStatement statementA = prepare(conn1, getFetchQuery("t"));
-        PreparedStatement statementB = prepare(conn1, getFetchQuery("u"));
-        PreparedStatement statementC = prepare(conn2, getFetchQuery("v"));
-        PreparedStatement statementD = prepare(conn2, getFetchQuery("x"));
+        PreparedStatement statementB = prepare(conn1, getFetchQuery("t"));
+        PreparedStatement statementC = prepare(conn2, getFetchQuery("t"));
+        PreparedStatement statementD = prepare(conn2, getFetchQuery("t"));
 
         StatementExecutor[] statementExecutor = new StatementExecutor[4];
         statementExecutor[0] = new StatementExecutor(statementA, true, TIMEOUT);
@@ -538,34 +498,44 @@
     /**
      * Part two of this test.
      */
-    private static void testTimeoutWithExec(Connection conn1,
-                                            Connection conn2)
+    private static void testTimeoutWithExec(Connection[] connections)
         throws
             TestFailedException
     {
         System.out.println("Testing timeout with an execute operation");
 
-        try {
-            conn1.setAutoCommit(true);
-            conn2.setAutoCommit(true);
-        } catch (SQLException e) {
-            throw new TestFailedException("Should not happen", e);
+        for (int i = 0; i < connections.length; ++i) {
+            try {
+                connections[i].setAutoCommit(true);
+            } catch (SQLException e) {
+                throw new TestFailedException("Should not happen", e);
+            }
         }
 
-        PreparedStatement statementA = prepare(conn1, getExecQuery("t"));
-        PreparedStatement statementB = prepare(conn2, getExecQuery("u"));
-        
-        StatementExecutor exec0 = new StatementExecutor(statementA, false, TIMEOUT);
-        StatementExecutor exec1 = new StatementExecutor(statementB, false, 0);
-        
-        exec1.start();
-        exec0.start();
+        PreparedStatement statements[] = new PreparedStatement[connections.length];
+        for (int i = 0; i < statements.length; ++i) {
+            statements[i] = prepare(connections[i], getExecQuery("t"));
+        }
 
-        try {
-            exec0.join();
-            exec1.join();
-        } catch (InterruptedException e) {
-            throw new TestFailedException("Should never happen", e);
+        StatementExecutor[] executors = new StatementExecutor[statements.length];
+        for (int i = 0; i < executors.length; ++i) {
+            int timeout =
+                (i % 2 == 0)
+                ? TIMEOUT
+                : 0;
+            executors[i] = new StatementExecutor(statements[i], false, timeout);
+        }
+
+        for (int i = 0; i < executors.length; ++i) {
+            executors[i].start();
+        }
+
+        for (int i = 0; i < executors.length; ++i) {
+            try {
+                executors[i].join();
+            } catch (InterruptedException e) {
+                throw new TestFailedException("Should never happen", e);
+            }
         }
         
         /**
@@ -584,24 +554,32 @@
          * in this class (note that the TIMEOUT constant is in seconds,
          * while the execution time is in milliseconds). 
          */
-        expectException(SQLState.LANG_STATEMENT_CANCELLED_OR_TIMED_OUT,
-                        exec0.getSQLException(),
-                        "exec did not time out. Execution time: "
-                        + exec0.getHighestRunTime() + " ms");
-
-        System.out.println("Statement 0 timed out");
-
-        SQLException sqlException = exec1.getSQLException();
-        if (sqlException != null) {
-            throw new TestFailedException(sqlException);
+        for (int i = 0; i < executors.length; ++i) {
+            int timeout =
+                (i % 2 == 0)
+                ? TIMEOUT
+                : 0;
+            if (timeout > 0) {
+                expectException(SQLState.LANG_STATEMENT_CANCELLED_OR_TIMED_OUT,
+                                executors[i].getSQLException(),
+                                "exec did not time out. Execution time: "
+                                + executors[i].getHighestRunTime() + " ms");
+            } else {
+                SQLException sqlException = executors[i].getSQLException();
+                if (sqlException != null) {
+                    throw new TestFailedException(sqlException);
+                }
+            }
         }
 
-        System.out.println("Statement 1 completed");
-        try {
-            statementA.close();
-            statementB.close();
-        } catch (SQLException e) {
-            throw new TestFailedException(e);
+        System.out.println("Statements that should time out timed out, and statements that
should complete completed");
+
+        for (int i = 0; i < statements.length; ++i) {
+            try {
+                statements[i].close();
+            } catch (SQLException e) {
+                throw new TestFailedException(e);
+            }
         }
     }
     
@@ -673,45 +651,44 @@
     public void go(String[] args)
     {
         System.out.println("Test SetQueryTimeoutTest starting");
-        
-        Connection conn1 = null;
-        Connection conn2 = null;
+
+        Connection[] connections = new Connection[CONNECTIONS];
+        for (int i = 0; i < connections.length; ++i) {
+            connections[i] = null;
+        }
 
         try {
             // Load the JDBC Driver class
             // use the ij utility to read the property file and
             // create connections
             ij.getPropertyArg(args);
-            conn1 = ij.startJBMS();
-            conn2 = ij.startJBMS();
+            for (int i = 0; i < connections.length; ++i) {
+                connections[i] = ij.startJBMS();
+            }
 
             System.out.println("Got connections");
-            
-            conn1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
-            conn2.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
-
-            prepareForTimedQueries(conn1);
-            testTimeoutWithFetch(conn1, conn2);
-            testTimeoutWithExec(conn1, conn2);
-            testInvalidTimeoutValue(conn1);
+
+            for (int i = 0; i < connections.length; ++i) {
+                connections[i].setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+            }
+
+            prepareForTimedQueries(connections[0]);
+            testTimeoutWithFetch(connections[0], connections[1]);
+            testTimeoutWithExec(connections);
+            testInvalidTimeoutValue(connections[0]);
   
             System.out.println("Test SetQueryTimeoutTest PASSED");
         } catch (Throwable e) {
             System.out.println("Test SetQueryTimeoutTest FAILED");
             e.printStackTrace();
         } finally {
-            if (conn2 != null) {
-                try {
-                    conn2.close();
-                } catch (SQLException ex) {
-                    printSQLException(ex);
-                }
-            }
-            if (conn1 != null) {
-                try {
-                    conn1.close();
-                } catch (SQLException ex) {
-                    printSQLException(ex);
+            for (int i = connections.length - 1; i >= 0; --i) {
+                if (connections[i] != null) {
+                    try {
+                        connections[i].close();
+                    } catch (SQLException ex) {
+                        printSQLException(ex);
+                    }
                 }
             }
             System.out.println("Closed connections");



Mime
View raw message