org.apache.derbyTesting.functionTests.tests.jdbcapi
Class SetQueryTimeoutTest

java.lang.Object
  extended by org.apache.derbyTesting.functionTests.tests.jdbcapi.SetQueryTimeoutTest

public class SetQueryTimeoutTest
extends java.lang.Object

Functional test for the Statement.setQueryTimeout() method. This test consists of four parts: 1. Executes a SELECT query in 4 different threads concurrently. 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 context of the other connection. Of the 4 threads, only one executes its statement with a timeout value. This way, the test ensures that the correct statement is affected by setQueryTimeout(), regardless of what connection/transaction it and other statements are executed in the context of. 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 on the same connection; since only one statement per connection executes at a time, there will be no interleaving of execution between them (contrary to the first part of this test, where calls to ResultSet.next() may be interleaved between the different threads). 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. 4. Tests that the query timeout value is not forgotten after the execution of a statement (DERBY-1692).


Nested Class Summary
private static class SetQueryTimeoutTest.StatementExecutor
           
private static class SetQueryTimeoutTest.TestFailedException
          This Exception class is used for getting fail-fast behaviour in this test.
 
Field Summary
private static int CONNECTIONS
           
private static int TIMEOUT
           
 
Constructor Summary
SetQueryTimeoutTest()
           
 
Method Summary
static int delay(int seconds, int value)
          This is the user-defined function which is called from our queries
private static void dropTables(java.sql.Connection conn, java.lang.String tablePrefix)
           
private static void exec(java.sql.Connection connection, java.lang.String queryString)
           
private static void exec(java.sql.Connection connection, java.lang.String queryString, java.util.Collection ignoreExceptions)
          Used for executing the SQL statements for setting up this test (the preparation phase).
private static void expectException(java.lang.String expectSqlState, java.sql.SQLException sqlException, java.lang.String failMsg)
          This method compares a thrown SQLException's SQLState value to an expected SQLState.
private static java.lang.String getExecQuery(java.lang.String tablePrefix)
           
private static java.lang.String getFetchQuery(java.lang.String tablePrefix)
           
 void go(java.lang.String[] args)
          The actual main bulk of this test.
static void main(java.lang.String[] args)
          Main program, makes this class invocable from the command line
private static java.sql.PreparedStatement prepare(java.sql.Connection conn, java.lang.String query)
           
private static void prepareForTimedQueries(java.sql.Connection conn)
           
private static void prepareTables(java.sql.Connection conn, java.lang.String tablePrefix)
           
private static void printSQLException(java.sql.SQLException e)
           
private static void testInvalidTimeoutValue(java.sql.Connection conn)
           
private static void testRememberTimeoutValue(java.sql.Connection conn)
          Test for DERBY-1692.
private static void testStatementRemembersTimeout(java.sql.PreparedStatement ps)
          Test that a prepared statement remembers its timeout value when executed multiple times.
private static void testStatementRemembersTimeout(java.sql.Statement stmt)
          Test that a statement remembers its timeout value when executed multiple times.
private static void testTimeoutWithExec(java.sql.Connection[] connections)
          Part two of this test.
private static void testTimeoutWithExecuteUpdate(java.sql.Connection conn)
          This tests timeout with executeUpdate call.
private static void testTimeoutWithFetch(java.sql.Connection conn1, java.sql.Connection conn2)
          Part 1 of this test.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TIMEOUT

private static final int TIMEOUT
See Also:
Constant Field Values

CONNECTIONS

private static final int CONNECTIONS
See Also:
Constant Field Values
Constructor Detail

SetQueryTimeoutTest

public SetQueryTimeoutTest()
Method Detail

printSQLException

private static void printSQLException(java.sql.SQLException e)

exec

private static void exec(java.sql.Connection connection,
                         java.lang.String queryString,
                         java.util.Collection ignoreExceptions)
                  throws SetQueryTimeoutTest.TestFailedException
Used for executing the SQL statements for setting up this test (the preparation phase). The queries testing setQueryTimeout() are run by the StatementExecutor class.

Throws:
SetQueryTimeoutTest.TestFailedException

exec

private static void exec(java.sql.Connection connection,
                         java.lang.String queryString)
                  throws SetQueryTimeoutTest.TestFailedException
Throws:
SetQueryTimeoutTest.TestFailedException

dropTables

private static void dropTables(java.sql.Connection conn,
                               java.lang.String tablePrefix)
                        throws SetQueryTimeoutTest.TestFailedException
Throws:
SetQueryTimeoutTest.TestFailedException

prepareTables

private static void prepareTables(java.sql.Connection conn,
                                  java.lang.String tablePrefix)
                           throws SetQueryTimeoutTest.TestFailedException
Throws:
SetQueryTimeoutTest.TestFailedException

delay

public static int delay(int seconds,
                        int value)
                 throws java.sql.SQLException
This is the user-defined function which is called from our queries

Throws:
java.sql.SQLException

prepareForTimedQueries

private static void prepareForTimedQueries(java.sql.Connection conn)
                                    throws SetQueryTimeoutTest.TestFailedException
Throws:
SetQueryTimeoutTest.TestFailedException

getFetchQuery

private static java.lang.String getFetchQuery(java.lang.String tablePrefix)

getExecQuery

private static java.lang.String getExecQuery(java.lang.String tablePrefix)

expectException

private static void expectException(java.lang.String expectSqlState,
                                    java.sql.SQLException sqlException,
                                    java.lang.String failMsg)
                             throws SetQueryTimeoutTest.TestFailedException
This method compares a thrown SQLException's SQLState value to an expected SQLState. If they do not match, a TestFailedException is thrown with the given message string.

Throws:
SetQueryTimeoutTest.TestFailedException

prepare

private static java.sql.PreparedStatement prepare(java.sql.Connection conn,
                                                  java.lang.String query)
                                           throws SetQueryTimeoutTest.TestFailedException
Throws:
SetQueryTimeoutTest.TestFailedException

testTimeoutWithFetch

private static void testTimeoutWithFetch(java.sql.Connection conn1,
                                         java.sql.Connection conn2)
                                  throws SetQueryTimeoutTest.TestFailedException
Part 1 of this test.

Throws:
SetQueryTimeoutTest.TestFailedException

testTimeoutWithExec

private static void testTimeoutWithExec(java.sql.Connection[] connections)
                                 throws SetQueryTimeoutTest.TestFailedException
Part two of this test.

Throws:
SetQueryTimeoutTest.TestFailedException

testInvalidTimeoutValue

private static void testInvalidTimeoutValue(java.sql.Connection conn)
                                     throws SetQueryTimeoutTest.TestFailedException
Throws:
SetQueryTimeoutTest.TestFailedException

testTimeoutWithExecuteUpdate

private static void testTimeoutWithExecuteUpdate(java.sql.Connection conn)
                                          throws SetQueryTimeoutTest.TestFailedException
This tests timeout with executeUpdate call.

Throws:
SetQueryTimeoutTest.TestFailedException

testRememberTimeoutValue

private static void testRememberTimeoutValue(java.sql.Connection conn)
                                      throws SetQueryTimeoutTest.TestFailedException
Test for DERBY-1692.

Throws:
SetQueryTimeoutTest.TestFailedException

testStatementRemembersTimeout

private static void testStatementRemembersTimeout(java.sql.Statement stmt)
                                           throws java.sql.SQLException,
                                                  SetQueryTimeoutTest.TestFailedException
Test that a statement remembers its timeout value when executed multiple times.

Throws:
java.sql.SQLException
SetQueryTimeoutTest.TestFailedException

testStatementRemembersTimeout

private static void testStatementRemembersTimeout(java.sql.PreparedStatement ps)
                                           throws java.sql.SQLException,
                                                  SetQueryTimeoutTest.TestFailedException
Test that a prepared statement remembers its timeout value when executed multiple times.

Throws:
java.sql.SQLException
SetQueryTimeoutTest.TestFailedException

main

public static void main(java.lang.String[] args)
Main program, makes this class invocable from the command line


go

public void go(java.lang.String[] args)
The actual main bulk of this test. Sets up the environment, prepares tables, runs the tests, and shuts down.


Built on Sat 2009-11-28 18:44:18-0800, from revision 885187

Apache Derby V10.6 Internals - Copyright © 2004,2008 The Apache Software Foundation. All Rights Reserved.