1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.SetQueryTimeoutTest
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.jdbcapi;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.Collections;
import org.apache.derby.tools.ij;
/**
* 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).
*/
public class SetQueryTimeoutTest
{
private static final int TIMEOUT = 1; // In seconds
private static final int CONNECTIONS = 100;
private static void printSQLException(SQLException e)
{
while (e != null)
{
e.printStackTrace();
e = e.getNextException();
}
}
/**
* This Exception class is used for getting fail-fast behaviour in
* this test. There is no point in wasting cycles running a test to
* the end when we know that it has failed.
*
* In order to enable chaining of exceptions in J2ME, this class defines
* its own "cause", duplicating existing functionality in J2SE.
*/
private static class TestFailedException
extends
Exception
{
private Throwable cause;
public TestFailedException(Throwable t)
{
super();
cause = t;
}
public TestFailedException(String message)
{
super(message);
cause = null;
}
public TestFailedException(String message, Throwable t)
{
super(message);
cause = t;
}
public String toString()
{
if (cause != null) {
return super.toString() + ": " + cause.toString();
} else {
return super.toString();
}
}
public void printStackTrace()
{
super.printStackTrace();
if (cause != null) {
if (cause instanceof SQLException) {
SetQueryTimeoutTest.printSQLException((SQLException)cause);
} else {
cause.printStackTrace();
}
}
}
}
/**
* Used for executing the SQL statements for setting up this test
* (the preparation phase). The queries testing setQueryTimeout()
* are run by the StatementExecutor class.
*/
private static void exec(Connection connection,
String queryString,
Collection ignoreExceptions)
throws
TestFailedException
{
Statement statement = null;
try {
statement = connection.createStatement();
statement.execute(queryString);
} catch (SQLException e) {
String sqlState = e.getSQLState();
if (!ignoreExceptions.contains(sqlState)) {
throw new TestFailedException(e); // See finally block below
}
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException ee) {
// This will discard an exception possibly thrown above :-(
// But we don't worry too much about this, since:
// 1. This is just a test
// 2. We don't expect close() to throw
// 3. If it does, this will be inspected by a developer
throw new TestFailedException(ee);
}
}
}
}
// Convenience method
private static void exec(Connection connection,
String queryString)
throws
TestFailedException
{
exec(connection, queryString, Collections.EMPTY_SET);
}
private static void dropTables(Connection conn, String tablePrefix)
throws
TestFailedException
{
HashSet<String> ignore = new HashSet<String>();
ignore.add("42Y55");
exec(conn, "drop table " + tablePrefix + "_orig", ignore);
exec(conn, "drop table " + tablePrefix + "_copy", ignore);
}
private static void prepareTables(Connection conn, String tablePrefix)
throws
TestFailedException
{
System.out.println("Initializing tables with prefix " + tablePrefix);
dropTables(conn, tablePrefix);
exec(conn,
"create table " + tablePrefix + "_orig (a int)");
exec(conn,
"create table " + tablePrefix + "_copy (a int)");
exec(conn,
"insert into "
+ tablePrefix + "_orig"
+ " values(0),(1),(2),(3),(4),(5),(6)");
}
/**
* 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
{
System.out.println("Preparing for testing queries with timeout");
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new TestFailedException("Should not happen", e);
}
try {
exec(conn, "DROP FUNCTION DELAY");
} catch (Exception e) {
// Ignore
}
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");
}
private static String getFetchQuery(String tablePrefix)
{
/**
* 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 + "_copy select a from "
+ tablePrefix + "_orig where DELAY(1,1)=1";
}
public static class StatementExecutor
extends
Thread
{
private PreparedStatement statement;
private boolean doFetch;
private int timeout;
private SQLException sqlException;
private String name;
private long highestRunTime;
public StatementExecutor(PreparedStatement statement,
boolean doFetch,
int timeout)
{
this.statement = statement;
this.doFetch = doFetch;
this.timeout = timeout;
highestRunTime = 0;
sqlException = null;
if (timeout > 0) {
try {
statement.setQueryTimeout(timeout);
} catch (SQLException e) {
sqlException = e;
}
}
}
private void setHighestRunTime(long runTime)
{
synchronized (this) {
highestRunTime = runTime;
}
}
public long getHighestRunTime()
{
synchronized (this) {
return highestRunTime;
}
}
private boolean fetchRow(ResultSet resultSet)
throws
SQLException
{
long startTime = System.currentTimeMillis();
boolean hasNext = resultSet.next();
long endTime = System.currentTimeMillis();
long runTime = endTime - startTime;
if (runTime > highestRunTime) setHighestRunTime(runTime);
return hasNext;
}
public void run()
{
if (sqlException != null)
return;
ResultSet resultSet = null;
try {
if (doFetch) {
long startTime = System.currentTimeMillis();
resultSet = statement.executeQuery();
long endTime = System.currentTimeMillis();
setHighestRunTime(endTime - startTime);
while (fetchRow(resultSet)) {
yield();
}
} else {
long startTime = System.currentTimeMillis();
statement.execute();
long endTime = System.currentTimeMillis();
setHighestRunTime(endTime - startTime);
}
} catch (SQLException e) {
synchronized (this) {
sqlException = e;
}
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException ex) {
if (sqlException != null) {
System.err.println("Discarding previous exception");
sqlException.printStackTrace();
}
sqlException = ex;
}
}
}
}
public SQLException getSQLException()
{
synchronized (this) {
return sqlException;
}
}
}
/**
* 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.
*/
private static void expectException(String expectSqlState,
SQLException sqlException,
String failMsg)
throws
TestFailedException
{
if (sqlException == null) {
throw new TestFailedException(failMsg);
} else {
String sqlState = sqlException.getSQLState();
if (!expectSqlState.equals(sqlState)) {
throw new TestFailedException(sqlException);
}
}
}
// A convenience method which wraps a SQLException
private static PreparedStatement prepare(Connection conn, String query)
throws
TestFailedException
{
try {
return conn.prepareStatement(query);
} catch (SQLException e) {
throw new TestFailedException(e);
}
}
/**
* Part 1 of this test.
*/
private static void testTimeoutWithFetch(Connection conn1,
Connection conn2)
throws
TestFailedException
{
System.out.println("Testing timeout with fetch operations");
try {
conn1.setAutoCommit(false);
conn2.setAutoCommit(false);
} catch (SQLException e) {
throw new TestFailedException("Should not happen", e);
}
// The idea with these 4 statements is as follows:
// A - should time out
// B - different stmt on the same connection; should NOT time out
// C - different stmt on different connection; should NOT time out
// D - here just to create equal contention on conn1 and conn2
PreparedStatement statementA = prepare(conn1, getFetchQuery("t"));
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);
statementExecutor[1] = new StatementExecutor(statementB, true, 0);
statementExecutor[2] = new StatementExecutor(statementC, true, 0);
statementExecutor[3] = new StatementExecutor(statementD, true, 0);
for (int i = 3; i >= 0; --i) {
statementExecutor[i].start();
}
for (int i = 0; i < 4; ++i) {
try {
statementExecutor[i].join();
} catch (InterruptedException e) {
throw new TestFailedException("Should never happen", e);
}
}
/**
* Actually, there is no guarantee that setting a query timeout
* for a statement will actually cause a timeout, even if execution
* of the statement takes longer than the specified timeout.
*
* However, these queries execute significantly longer than the
* specified query timeout. Also, the cancellation mechanism
* implemented should be quite responsive. In sum, we expect
* the statement to always time out.
*
* If it does not time out, however, we print the highest
* execution time for the query, as an assistance in determining
* why it failed. Compare the number to the TIMEOUT constant
* in this class (note that the TIMEOUT constant is in seconds,
* while the execution time is in milliseconds).
*/
expectException("XCL52",
statementExecutor[0].getSQLException(),
"fetch did not time out. Highest execution time: "
+ statementExecutor[0].getHighestRunTime() + " ms");
System.out.println("Statement 0 timed out");
for (int i = 1; i < 4; ++i) {
SQLException sqlException = statementExecutor[i].getSQLException();
if (sqlException != null) {
throw new TestFailedException("Unexpected exception in " + i,
sqlException);
}
System.out.println("Statement " + i + " completed");
}
try {
statementA.close();
statementB.close();
statementC.close();
statementD.close();
conn1.commit();
conn2.commit();
} catch (SQLException e) {
throw new TestFailedException(e);
}
}
/**
* Part two of this test.
*/
private static void testTimeoutWithExec(Connection[] connections)
throws
TestFailedException
{
System.out.println("Testing timeout with an execute operation");
for (int i = 0; i < connections.length; ++i) {
try {
connections[i].setAutoCommit(true);
} catch (SQLException e) {
throw new TestFailedException("Should not happen", e);
}
}
PreparedStatement statements[] = new PreparedStatement[connections.length];
for (int i = 0; i < statements.length; ++i) {
statements[i] = prepare(connections[i], getExecQuery("t"));
}
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);
}
}
/**
* Actually, there is no guarantee that setting a query timeout
* for a statement will actually cause a timeout, even if execution
* of the statement takes longer than the specified timeout.
*
* However, these queries execute significantly longer than the
* specified query timeout. Also, the cancellation mechanism
* implemented should be quite responsive. In sum, we expect
* the statement to always time out.
*
* If it does not time out, however, we print the highest
* execution time for the query, as an assistance in determining
* why it failed. Compare the number to the TIMEOUT constant
* in this class (note that the TIMEOUT constant is in seconds,
* while the execution time is in milliseconds).
*/
for (int i = 0; i < executors.length; ++i) {
int timeout =
(i % 2 == 0)
? TIMEOUT
: 0;
if (timeout > 0) {
expectException("XCL52",
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("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);
}
}
}
private static void testInvalidTimeoutValue(Connection conn)
throws
TestFailedException
{
System.out.println("Testing setting a negative timeout value");
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new TestFailedException("Should not happen", e);
}
// Create statement
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement("select * from sys.systables");
} catch (SQLException e) {
throw new TestFailedException("Should not happen", e);
}
// Set (invalid) timeout value - expect exception
try {
stmt.setQueryTimeout(-1);
} catch (SQLException e) {
expectException("XJ074", e,
"negative timeout value should give exception");
}
System.out.println("Negative timeout value caused exception, as expected");
// Execute the statement and fetch result
ResultSet rs = null;
try {
rs = stmt.executeQuery();
System.out.println("Execute returned a ResultSet");
rs.close();
} catch (SQLException e) {
throw new TestFailedException("Should not happen", e);
} finally {
try {
stmt.close();
} catch (SQLException e) {
// This will discard an exception possibly thrown above :-(
// But we don't worry too much about this, since:
// 1. This is just a test
// 2. We don't expect close() to throw
// 3. If it does, this will be inspected by a developer
throw new TestFailedException("close should not throw", e);
}
}
}
/** This tests timeout with executeUpdate call. */
private static void testTimeoutWithExecuteUpdate(Connection conn)
throws TestFailedException
{
System.out.println("Testing timeout with executeUpdate call.");
try{
Statement stmt = conn.createStatement();
stmt.setQueryTimeout(TIMEOUT);
stmt.executeUpdate(getExecQuery("t"));
} catch (SQLException sqle) {
expectException("XCL52", sqle, "Should have timed out.");
}
}
/** Test for DERBY-1692. */
private static void testRememberTimeoutValue(Connection conn)
throws TestFailedException
{
String sql = getFetchQuery("t");
try {
Statement stmt = conn.createStatement();
testStatementRemembersTimeout(stmt);
PreparedStatement ps = conn.prepareStatement(sql);
testStatementRemembersTimeout(ps);
CallableStatement cs = conn.prepareCall(sql);
testStatementRemembersTimeout(cs);
} catch (SQLException sqle) {
throw new TestFailedException("Should not happen", sqle);
}
}
/** Test that a statement remembers its timeout value when executed
* multiple times. */
private static void testStatementRemembersTimeout(Statement stmt)
throws SQLException, TestFailedException
{
System.out.println("Testing that Statement remembers timeout.");
stmt.setQueryTimeout(1);
long runTime=0;
for (int i = 0; i < 3; i++) {
try {
long startTime = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery(getFetchQuery("t"));
while (rs.next());
long endTime = System.currentTimeMillis();
runTime = endTime - startTime;
throw new TestFailedException("Should have timed out, for " +
"statement, iteration: " +i+ ", took (millis): "+runTime);
} catch (SQLException sqle) {
expectException("XCL52", sqle, "Should have timed out, got " +
"unexpected exception, for statement, iteration: " + i +
", time taken (millis): " + runTime);
}
}
stmt.close();
}
/** Test that a prepared statement remembers its timeout value when
* executed multiple times. */
private static void testStatementRemembersTimeout(PreparedStatement ps)
throws SQLException, TestFailedException
{
String name = (ps instanceof CallableStatement) ?
"CallableStatement" : "PreparedStatement";
System.out.println("Testing that " + name + " remembers timeout.");
ps.setQueryTimeout(1);
for (int i = 0; i < 3; i++) {
long runTime=0;
try {
long startTime = System.currentTimeMillis();
ResultSet rs = ps.executeQuery();
while (rs.next());
long endTime = System.currentTimeMillis();
runTime = endTime - startTime;
throw new TestFailedException(
"Should have timed out, for " + name + ", on iteration "
+ i + ", runtime(millis): " + runTime);
} catch (SQLException sqle) {
expectException("XCL52", sqle, "Should have timed out, " +
"got unexpected exception, for " + name + ", on iteration "
+ i + ", runtime(millis): " + runTime);
}
}
ps.close();
}
/**
* Main program, makes this class invocable from the command line
*/
public static void main(String[] args)
{
new SetQueryTimeoutTest().go(args);
}
/**
* The actual main bulk of this test.
* Sets up the environment, prepares tables,
* runs the tests, and shuts down.
*/
public void go(String[] args)
{
System.out.println("Test SetQueryTimeoutTest starting");
Connection[] connections = new Connection[CONNECTIONS];
try {
// Load the JDBC Driver class
// use the ij utility to read the property file and
// create connections
ij.getPropertyArg(args);
for (int i = 0; i < connections.length; ++i) {
connections[i] = ij.startJBMS();
}
System.out.println("Got connections");
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]);
testRememberTimeoutValue(connections[0]);
testTimeoutWithExecuteUpdate(connections[0]);
System.out.println("Test SetQueryTimeoutTest PASSED");
} catch (Throwable e) {
System.out.println("Test SetQueryTimeoutTest FAILED");
e.printStackTrace();
} finally {
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");
}
}
}
|