1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.ResultSetMetaData ; 28 import java.sql.Savepoint ; 29 import java.sql.Statement ; 30 import java.sql.SQLException ; 31 32 import javax.sql.ConnectionPoolDataSource ; 33 import javax.sql.PooledConnection ; 34 35 import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource; 36 37 import org.apache.derby.tools.ij; 38 import org.apache.derby.tools.JDBCDisplayUtil; 39 import org.apache.derbyTesting.functionTests.util.TestUtil; 40 49 50 51 public class declareGlobalTempTableJavaJDBC30 { 52 53 static private boolean isDerbyNet = false; 54 55 58 public static void main(String [] args) { 59 boolean passed = true; 60 61 Connection con = null; 62 Statement s = null; 63 64 65 try { 66 System.out.println("Test declaredGlobalTempTableJava starting"); 67 68 69 72 ij.getPropertyArg(args); 73 con = ij.startJBMS(); 74 isDerbyNet = TestUtil.isNetFramework(); 75 76 con.setAutoCommit(false); 77 s = con.createStatement(); 78 79 80 81 passed = testHoldableCursorsAndSavepoints(con, s) && passed; 82 83 84 passed = testPooledConnectionClose() && passed; 85 86 con.close(); 87 88 } catch (Throwable e) { 89 System.out.println("FAIL -- unexpected exception "+e); 90 JDBCDisplayUtil.ShowException(System.out, e); 91 e.printStackTrace(); 92 passed = false; 93 } 94 95 if (passed) 96 System.out.println("PASS"); 97 98 System.out.println("Test declaredGlobalTempTable finished"); 99 } 100 101 112 113 static boolean testHoldableCursorsAndSavepoints(Connection con, Statement s) 114 throws SQLException { 115 boolean passed = true; 116 117 try 118 { 119 System.out.println("TEST1 : Test declared temporary table with ON COMMIT DELETE ROWS and holdable cursors"); 120 121 System.out.println("Temp table t1 with held open cursors on it. Data should be preserved in t1 at commit time"); 122 Statement s1 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 124 ResultSet.HOLD_CURSORS_OVER_COMMIT ); 125 s1.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit delete rows not logged"); 126 s1.executeUpdate("insert into session.t1 values(11, 1)"); 127 s1.executeUpdate("insert into session.t1 values(12, 2)"); 128 ResultSet rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 130 131 rs1 = s1.executeQuery("select * from SESSION.t1"); rs1.next(); 133 134 System.out.println("Temp tables t2 & t3 with one held open cursor on them together. Data should be preserved in t2 & t3 at commit time"); 135 Statement s2 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 136 ResultSet.HOLD_CURSORS_OVER_COMMIT ); 137 s2.executeUpdate("declare global temporary table SESSION.t2(c21 int, c22 int) on commit delete rows not logged"); 138 s2.executeUpdate("insert into session.t2 values(21, 1)"); 139 s2.executeUpdate("insert into session.t2 values(22, 2)"); 140 ResultSet rs23 = s2.executeQuery("select count(*) from SESSION.t2"); dumpRS(rs23); 142 143 s2.executeUpdate("declare global temporary table SESSION.t3(c31 int, c32 int) on commit delete rows not logged"); 144 s2.executeUpdate("insert into session.t3 values(31, 1)"); 145 s2.executeUpdate("insert into session.t3 values(32, 2)"); 146 rs23 = s2.executeQuery("select count(*) from SESSION.t3"); dumpRS(rs23); 148 149 rs23 = s2.executeQuery("select * from SESSION.t2, SESSION.t3 where c22=c32"); rs23.next(); 151 152 System.out.println("Temp table t4 with one held cursor but it is closed before commit. Data should be deleted from t4 at commit time"); 153 Statement s3 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 154 ResultSet.HOLD_CURSORS_OVER_COMMIT ); 155 s3.executeUpdate("declare global temporary table SESSION.t4(c41 int, c42 int) on commit delete rows not logged"); 156 s3.executeUpdate("insert into session.t4 values(41, 1)"); 157 s3.executeUpdate("insert into session.t4 values(42, 2)"); 158 ResultSet rs4 = s3.executeQuery("select count(*) from SESSION.t4"); dumpRS(rs4); 160 161 rs4 = s3.executeQuery("select * from SESSION.t4"); rs4.next(); 163 rs4.close(); 164 165 con.commit(); 166 167 System.out.println("After commit, verify all the 4 tables"); 168 169 System.out.println("Temp table t1 will have the data intact after commit"); 170 rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 172 173 System.out.println("Temp table t2 will have the data intact after commit"); 174 rs23 = s2.executeQuery("select count(*) from SESSION.t2"); dumpRS(rs23); 176 177 System.out.println("Temp table t3 will have the data intact after commit"); 178 rs23 = s2.executeQuery("select count(*) from SESSION.t3"); dumpRS(rs23); 180 181 System.out.println("Temp table t4 will have no data after commit"); 182 rs4 = s3.executeQuery("select count(*) from SESSION.t4"); dumpRS(rs4); 184 185 s.executeUpdate("drop table SESSION.t1"); 186 s.executeUpdate("drop table SESSION.t2"); 187 s.executeUpdate("drop table SESSION.t3"); 188 s.executeUpdate("drop table SESSION.t4"); 189 190 con.commit(); 191 System.out.println("TEST1 PASSED"); 192 } catch (Throwable e) 193 { 194 System.out.println("Unexpected message: "+ e.getMessage()); 195 e.printStackTrace(System.out); 196 con.rollback(); 197 passed = false; System.out.println("TEST1 FAILED"); 199 } 200 201 try 202 { 203 System.out.println("TEST1a : Test declared temporary table with ON COMMIT DELETE ROWS and holdable cursors on prepared statement"); 204 205 System.out.println("Temp table t1 with held open cursors on it. Data should be preserved in t1 at commit time"); 206 Statement s1 = con.createStatement(); 207 s1.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit delete rows not logged"); 208 s1.executeUpdate("insert into session.t1 values(11, 1)"); 209 s1.executeUpdate("insert into session.t1 values(12, 2)"); 210 211 PreparedStatement ps1 = con.prepareStatement("select count(*) from SESSION.t1", 213 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT ); 214 ResultSet rs1 = ps1.executeQuery(); dumpRS(rs1); 216 217 PreparedStatement ps2 = con.prepareStatement("select * from SESSION.t1", 218 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT ); 219 ResultSet rs11 = ps2.executeQuery(); rs11.next(); 222 System.out.println("Temp table t2 with one held cursor but it is closed before commit. Data should be deleted from t2 at commit time"); 223 s1.executeUpdate("declare global temporary table SESSION.t2(c21 int, c22 int) on commit delete rows not logged"); 224 s1.executeUpdate("insert into session.t2 values(21, 1)"); 225 s1.executeUpdate("insert into session.t2 values(22, 2)"); 226 227 PreparedStatement ps3 = con.prepareStatement("select count(*) from SESSION.t2", 229 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT ); 230 ResultSet rs2 = ps3.executeQuery(); dumpRS(rs2); 232 233 PreparedStatement ps4 = con.prepareStatement("select * from SESSION.t2", 234 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT ); 235 rs2 = ps4.executeQuery(); rs2.next(); 237 rs2.close(); 238 239 con.commit(); 240 241 System.out.println("After commit, verify both the tables"); 242 243 System.out.println("Temp table t1 will have the data intact after commit"); 244 rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 246 rs11.close(); 248 249 System.out.println("Temp table t2 will have no data after commit"); 250 rs2 = s1.executeQuery("select count(*) from SESSION.t2"); dumpRS(rs2); 252 253 s.executeUpdate("drop table SESSION.t1"); 254 s.executeUpdate("drop table SESSION.t2"); 255 256 con.commit(); 257 System.out.println("TEST1a PASSED"); 258 } catch (Throwable e) 259 { 260 System.out.println("Unexpected message: "+ e.getMessage()); 261 con.rollback(); 262 passed = false; System.out.println("TEST1a FAILED"); 264 } 265 266 try 267 { 268 System.out.println("TEST2 : Declare a temporary table with ON COMMIT PRESERVE ROWS and various combinations of holdability"); 269 270 System.out.println("Temp table t1 with held open cursors on it. Data should be preserved, holdability shouldn't matter"); 271 Statement s1 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 273 ResultSet.HOLD_CURSORS_OVER_COMMIT ); 274 s1.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 275 s1.executeUpdate("insert into session.t1 values(11, 1)"); 276 s1.executeUpdate("insert into session.t1 values(12, 2)"); 277 ResultSet rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 279 280 rs1 = s1.executeQuery("select * from SESSION.t1"); rs1.next(); 282 283 con.commit(); 284 285 System.out.println("After commit, verify the table"); 286 287 System.out.println("Temp table t1 will have data after commit"); 288 rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 290 291 s.executeUpdate("drop table SESSION.t1"); 292 con.commit(); 293 System.out.println("TEST2 PASSED"); 294 } catch (Throwable e) 295 { 296 System.out.println("Unexpected message: "+ e.getMessage()); 297 con.rollback(); 298 passed = false; System.out.println("TEST2 FAILED"); 300 } 301 302 try 303 { 304 System.out.println("TEST3A : Savepoint and Rollback behavior"); 305 306 System.out.println(" In the transaction:"); 307 System.out.println(" Create savepoint1 and declare temp table t1"); 308 Savepoint savepoint1 = con.setSavepoint(); 309 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 310 PreparedStatement pStmt = con.prepareStatement("insert into SESSION.t1 values (?, ?)"); 311 pStmt.setInt(1, 11); 312 pStmt.setInt(2, 1); 313 pStmt.execute(); 314 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 315 dumpRS(rs1); 316 317 System.out.println(" Create savepoint 2, drop temp table t1, rollback savepoint 2"); 318 Savepoint savepoint2 = con.setSavepoint(); 319 s.executeUpdate("drop table SESSION.t1"); 320 try { 321 rs1 = s.executeQuery("select * from SESSION.t1"); 322 } catch (Throwable e) 323 { 324 System.out.println("Expected message: "+ e.getMessage()); 325 } 326 con.rollback(savepoint2); 327 328 System.out.println(" select should pass, rollback savepoint 1, select should fail"); 329 rs1 = s.executeQuery("select * from SESSION.t1"); 330 dumpRS(rs1); 331 con.rollback(savepoint1); 332 rs1 = s.executeQuery("select * from SESSION.t1"); 333 334 passed = false; System.out.println("TEST3A FAILED"); 336 } catch (Throwable e) 337 { 338 System.out.println("Expected message: "+ e.getMessage()); 339 con.commit(); 340 System.out.println("TEST3A PASSED"); 341 } 342 343 try 344 { 345 System.out.println("TEST3B : Savepoint and Rollback behavior"); 346 347 System.out.println(" In the transaction:"); 348 System.out.println(" Create savepoint1 and declare temp table t1"); 349 Savepoint savepoint1 = con.setSavepoint(); 350 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 351 352 System.out.println(" Create savepoint2 and declare temp table t2"); 353 Savepoint savepoint2 = con.setSavepoint(); 354 s.executeUpdate("declare global temporary table SESSION.t2(c21 int, c22 int) on commit preserve rows not logged"); 355 356 System.out.println(" Release savepoint 1 and select from temp table t1 & t2"); 357 con.releaseSavepoint(savepoint1); 358 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 359 dumpRS(rs1); 360 rs1 = s.executeQuery("select * from SESSION.t2"); 361 dumpRS(rs1); 362 363 System.out.println(" Drop temp table t2(explicit drop), rollback transaction(implicit drop of t1)"); 364 s.executeUpdate("drop table SESSION.t2"); 365 con.rollback(); 366 367 System.out.println(" Select from temp table t1 and t2 will fail"); 368 try { 369 rs1 = s.executeQuery("select * from SESSION.t1"); 370 } catch (Throwable e) 371 { 372 System.out.println("Expected message: "+ e.getMessage()); 373 } 374 try { 375 rs1 = s.executeQuery("select * from SESSION.t2"); 376 } catch (Throwable e) 377 { 378 System.out.println("Expected message: "+ e.getMessage()); 379 } 380 con.commit(); 381 System.out.println("TEST3B PASSED"); 382 } catch (Throwable e) 383 { 384 System.out.println("Unexpected message: "+ e.getMessage()); 385 con.rollback(); 386 passed = false; System.out.println("TEST3B FAILED"); 388 } 389 390 try 391 { 392 System.out.println("TEST3C : Savepoint and Rollback behavior"); 393 394 System.out.println(" In the transaction:"); 395 System.out.println(" Create savepoint1 and declare temp table t1"); 396 Savepoint savepoint1 = con.setSavepoint(); 397 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 398 399 System.out.println(" Create savepoint2 and declare temp table t2"); 400 Savepoint savepoint2 = con.setSavepoint(); 401 s.executeUpdate("declare global temporary table SESSION.t2(c21 int, c22 int) on commit preserve rows not logged"); 402 403 System.out.println(" Release savepoint 1 and select from temp table t1 and t2"); 404 con.releaseSavepoint(savepoint1); 405 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 406 dumpRS(rs1); 407 rs1 = s.executeQuery("select * from SESSION.t2"); 408 dumpRS(rs1); 409 410 System.out.println(" Create savepoint3 and rollback savepoint3(should not touch t1 and t2)"); 411 Savepoint savepoint3 = con.setSavepoint(); 412 con.rollback(savepoint3); 413 414 System.out.println(" select from temp tables t1 and t2 should pass"); 415 rs1 = s.executeQuery("select * from SESSION.t1"); 416 dumpRS(rs1); 417 rs1 = s.executeQuery("select * from SESSION.t2"); 418 dumpRS(rs1); 419 420 System.out.println(" Rollback transaction and select from temp tables t1 and t2 should fail"); 421 con.rollback(); 422 try { 423 rs1 = s.executeQuery("select * from SESSION.t1"); 424 } catch (Throwable e) 425 { 426 System.out.println("Expected message: "+ e.getMessage()); 427 } 428 try { 429 rs1 = s.executeQuery("select * from SESSION.t2"); 430 } catch (Throwable e) 431 { 432 System.out.println("Expected message: "+ e.getMessage()); 433 } 434 435 con.commit(); 436 System.out.println("TEST3C PASSED"); 437 } catch (Throwable e) 438 { 439 System.out.println("Unexpected message: "+ e.getMessage()); 440 con.rollback(); 441 passed = false; System.out.println("TEST3C FAILED"); 443 } 444 445 try 446 { 447 System.out.println("TEST3D : Savepoint and Rollback behavior"); 448 449 System.out.println(" In the transaction:"); 450 System.out.println(" Create savepoint1 and declare temp table t1"); 451 Savepoint savepoint1 = con.setSavepoint(); 452 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 453 454 System.out.println(" Create savepoint2 and drop temp table t1"); 455 Savepoint savepoint2 = con.setSavepoint(); 456 s.executeUpdate("drop table SESSION.t1"); 457 458 System.out.println(" Rollback savepoint2 and select temp table t1"); 459 con.rollback(savepoint2); 460 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 461 dumpRS(rs1); 462 463 System.out.println(" Commit transaction and select temp table t1"); 464 con.commit(); 465 rs1 = s.executeQuery("select * from SESSION.t1"); 466 dumpRS(rs1); 467 468 s.executeUpdate("drop table SESSION.t1"); 469 con.commit(); 470 System.out.println("TEST3D PASSED"); 471 } catch (Throwable e) 472 { 473 System.out.println("Unexpected message: "+ e.getMessage()); 474 con.rollback(); 475 passed = false; System.out.println("TEST3D FAILED"); 477 } 478 479 try 480 { 481 System.out.println("TEST3E : Savepoint and Rollback behavior"); 482 483 System.out.println(" In the transaction:"); 484 System.out.println(" Create savepoint1 and declare temp table t1"); 485 Savepoint savepoint1 = con.setSavepoint(); 486 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 487 488 System.out.println(" Create savepoint2 and drop temp table t1"); 489 Savepoint savepoint2 = con.setSavepoint(); 490 s.executeUpdate("drop table SESSION.t1"); 491 492 System.out.println(" Rollback savepoint 1 and select from temp table t1 should fail"); 493 con.rollback(savepoint1); 494 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 495 496 con.rollback(); 497 passed = false; System.out.println("TEST3E FAILED"); 499 } catch (Throwable e) 500 { 501 System.out.println("Expected message: "+ e.getMessage()); 502 con.commit(); 503 System.out.println("TEST3E PASSED"); 504 } 505 506 try 507 { 508 System.out.println("TEST3F : Savepoint and Rollback behavior"); 509 510 System.out.println(" In the transaction:"); 511 System.out.println(" declare temp table t1 and drop temp table t1"); 512 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 513 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 514 dumpRS(rs1); 515 s.executeUpdate("drop table SESSION.t1"); 516 System.out.println(" rollback, select on t1 should fail"); 517 con.rollback(); 518 rs1 = s.executeQuery("select * from SESSION.t1"); 519 520 con.rollback(); 521 passed = false; System.out.println("TEST3F FAILED"); 523 } catch (Throwable e) 524 { 525 System.out.println("Expected message: "+ e.getMessage()); 526 con.commit(); 527 System.out.println("TEST3F PASSED"); 528 } 529 530 try 531 { 532 System.out.println("TEST3G : Savepoint and Rollback behavior"); 533 534 System.out.println(" In the transaction:"); 535 System.out.println(" declare temp table t1 and commit"); 536 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 537 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 538 dumpRS(rs1); 539 con.commit(); 540 System.out.println(" In the transaction:"); 541 System.out.println(" drop temp table t1 and rollback, select on t1 should pass"); 542 s.executeUpdate("drop table SESSION.t1"); 543 con.rollback(); 544 rs1 = s.executeQuery("select * from SESSION.t1"); 545 dumpRS(rs1); 546 547 s.executeUpdate("drop table SESSION.t1"); 548 con.commit(); 549 System.out.println("TEST3G PASSED"); 550 } catch (Throwable e) 551 { 552 System.out.println("Unexpected message: "+ e.getMessage()); 553 con.rollback(); 554 passed = false; System.out.println("TEST3G FAILED"); 556 } 557 558 try 559 { 560 System.out.println("TEST3H : Savepoint and commit behavior"); 561 562 System.out.println(" In the transaction:"); 563 System.out.println(" declare temp table t1 and commit"); 564 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 565 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 566 dumpRS(rs1); 567 con.commit(); 568 System.out.println(" In the transaction:"); 569 System.out.println(" drop temp table t1 and commit, select on t1 should fail"); 570 s.executeUpdate("drop table SESSION.t1"); 571 con.commit(); 572 rs1 = s.executeQuery("select * from SESSION.t1"); 573 574 con.rollback(); 575 passed = false; System.out.println("TEST3H FAILED"); 577 } catch (Throwable e) 578 { 579 System.out.println("Expected message: "+ e.getMessage()); 580 con.commit(); 581 System.out.println("TEST3H PASSED"); 582 } 583 584 try 585 { 586 System.out.println("TEST3I : Savepoint and Rollback behavior"); 587 588 System.out.println(" In the transaction:"); 589 System.out.println(" declare temp table t1 and rollback"); 590 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 591 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 592 dumpRS(rs1); 593 con.rollback(); 594 rs1 = s.executeQuery("select * from SESSION.t1"); 595 596 con.rollback(); 597 passed = false; System.out.println("TEST3I FAILED"); 599 } catch (Throwable e) 600 { 601 System.out.println("Expected message: "+ e.getMessage()); 602 con.commit(); 603 System.out.println("TEST3I PASSED"); 604 } 605 606 try 607 { 608 System.out.println("TEST3J : Savepoint and Rollback behavior"); 609 610 System.out.println(" In the transaction:"); 611 System.out.println(" declare temp table t1 with 2 columns and commit"); 612 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 613 s.executeUpdate("insert into SESSION.t1 values(11, 11)"); 614 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 615 dumpRS(rs1); 616 con.commit(); 617 System.out.println(" Create savepoint1 and drop temp table t1 with 2 columns"); 618 Savepoint savepoint1 = con.setSavepoint(); 619 s.executeUpdate("drop table SESSION.t1"); 620 System.out.println(" declare temp table t1 but this time with 3 columns"); 621 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int, c13 int not null) on commit preserve rows not logged"); 622 s.executeUpdate("insert into SESSION.t1 values(22, 22, 22)"); 623 rs1 = s.executeQuery("select * from SESSION.t1"); 624 dumpRS(rs1); 625 System.out.println(" Create savepoint2 and drop temp table t1 with 3 columns"); 626 Savepoint savepoint2 = con.setSavepoint(); 627 s.executeUpdate("drop table SESSION.t1"); 628 con.rollback(); 629 System.out.println(" select from temp table t1 here should have 2 columns"); 630 rs1 = s.executeQuery("select * from SESSION.t1"); 631 dumpRS(rs1); 632 s.executeUpdate("drop table SESSION.t1"); 633 634 con.commit(); 635 System.out.println("TEST3J PASSED"); 636 } catch (Throwable e) 637 { 638 System.out.println("Unexpected message: "+ e.getMessage()); 639 con.rollback(); 640 passed = false; System.out.println("TEST3J FAILED"); 642 } 643 644 try 645 { 646 System.out.println("TEST3K : Savepoint and Rollback behavior"); 647 648 System.out.println(" In the transaction:"); 649 System.out.println(" declare temp table t1 & t2, insert few rows and commit"); 650 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged on rollback delete rows"); 651 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(c21 int, c22 int) on commit preserve rows not logged"); 652 s.executeUpdate("insert into SESSION.t1 values(11, 1)"); 653 s.executeUpdate("insert into session.t2 values(21, 1)"); 654 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 655 dumpRS(rs1); 656 rs1 = s.executeQuery("select * from SESSION.t2"); 657 dumpRS(rs1); 658 con.commit(); 659 660 System.out.println(" In the next transaction, insert couple more rows in t1 & t2 and "); 661 s.executeUpdate("insert into SESSION.t1 values(12, 2)"); 662 s.executeUpdate("insert into SESSION.t2 values(22, 2)"); 663 rs1 = s.executeQuery("select * from SESSION.t1"); 664 dumpRS(rs1); 665 rs1 = s.executeQuery("select * from SESSION.t2"); 666 dumpRS(rs1); 667 668 System.out.println(" Create savepoint1 and update some rows in t1 and inspect the data"); 669 Savepoint savepoint1 = con.setSavepoint(); 670 s.executeUpdate("UPDATE SESSION.t1 SET c12 = 3 where c12>1"); 671 rs1 = s.executeQuery("select * from SESSION.t1"); 672 dumpRS(rs1); 673 674 System.out.println(" update t2 with where clause such that no rows get modified in t2 and inspect the data"); 675 s.executeUpdate("UPDATE SESSION.t2 SET c22 = 3 where c22>2"); 676 rs1 = s.executeQuery("select * from SESSION.t2"); 677 dumpRS(rs1); 678 679 System.out.println(" Rollback to savepoint1 and we should loose all the rows in t1"); 680 con.rollback(savepoint1); 681 rs1 = s.executeQuery("select * from SESSION.t1"); 682 dumpRS(rs1); 683 System.out.println(" temp table t2 should also have no rows because attempt was made to modify it (even though nothing actually got modified in t2 in the savepoint)"); 684 rs1 = s.executeQuery("select * from SESSION.t2"); 685 dumpRS(rs1); 686 687 System.out.println(" Commit the transaction and should see no data in t1 and t2"); 688 con.commit(); 689 rs1 = s.executeQuery("select * from SESSION.t1"); 690 dumpRS(rs1); 691 rs1 = s.executeQuery("select * from SESSION.t2"); 692 dumpRS(rs1); 693 694 s.executeUpdate("drop table SESSION.t1"); 695 s.executeUpdate("drop table SESSION.t2"); 696 con.commit(); 697 System.out.println("TEST3K PASSED"); 698 } catch (Throwable e) 699 { 700 System.out.println("Unexpected message: "+ e.getMessage()); 701 con.rollback(); 702 passed = false; System.out.println("TEST3K FAILED"); 704 } 705 706 try 707 { 708 System.out.println("TEST3L : Savepoint and Rollback behavior"); 709 710 System.out.println(" In the transaction:"); 711 System.out.println(" declare temp table t1 & t2, insert few rows and commit"); 712 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged on rollback delete rows"); 713 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(c21 int, c22 int) on commit preserve rows not logged on rollback delete rows"); 714 s.executeUpdate("insert into SESSION.t1 values(11, 1)"); 715 s.executeUpdate("insert into session.t2 values(21, 1)"); 716 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 717 dumpRS(rs1); 718 rs1 = s.executeQuery("select * from SESSION.t2"); 719 dumpRS(rs1); 720 con.commit(); 721 722 System.out.println(" In the next transaction, insert couple more rows in t1 & t2 and "); 723 s.executeUpdate("insert into SESSION.t1 values(12, 2)"); 724 s.executeUpdate("insert into session.t2 values(22, 2)"); 725 rs1 = s.executeQuery("select * from SESSION.t1"); 726 dumpRS(rs1); 727 rs1 = s.executeQuery("select * from SESSION.t2"); 728 dumpRS(rs1); 729 730 System.out.println(" Create savepoint1 and update some rows in t1 and inspect the data"); 731 Savepoint savepoint1 = con.setSavepoint(); 732 s.executeUpdate("UPDATE SESSION.t1 SET c12 = 3 where c12>1"); 733 rs1 = s.executeQuery("select * from SESSION.t1"); 734 dumpRS(rs1); 735 736 System.out.println(" update t2 with where clause such that no rows get modified in t2 and inspect the data"); 737 s.executeUpdate("UPDATE SESSION.t2 SET c22 = 3 where c22>3"); 738 rs1 = s.executeQuery("select * from SESSION.t2"); 739 dumpRS(rs1); 740 741 System.out.println(" Rollback to savepoint1 and we should loose all the rows in t1"); 742 con.rollback(savepoint1); 743 rs1 = s.executeQuery("select * from SESSION.t1"); 744 dumpRS(rs1); 745 System.out.println(" temp table t2 should also have no rows because attempt was made to modfiy it (even though nothing actually got modified in t2 in the savepoint)"); 746 rs1 = s.executeQuery("select * from SESSION.t2"); 747 dumpRS(rs1); 748 749 System.out.println(" Rollback the transaction and should see no data in t1 and t2"); 750 con.rollback(); 751 rs1 = s.executeQuery("select * from SESSION.t1"); 752 dumpRS(rs1); 753 rs1 = s.executeQuery("select * from SESSION.t2"); 754 dumpRS(rs1); 755 756 s.executeUpdate("drop table SESSION.t1"); 757 s.executeUpdate("drop table SESSION.t2"); 758 con.commit(); 759 System.out.println("TEST3L PASSED"); 760 } catch (Throwable e) 761 { 762 System.out.println("Unexpected message: "+ e.getMessage()); 763 con.rollback(); 764 passed = false; System.out.println("TEST3L FAILED"); 766 } 767 768 try 769 { 770 System.out.println("TEST3M : Savepoint and Rollback behavior"); 771 772 System.out.println(" In the transaction:"); 773 System.out.println(" declare temp table t1 & t2 & t3 & t4, insert few rows and commit"); 774 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged on rollback delete rows"); 775 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(c21 int, c22 int) on commit preserve rows not logged on rollback delete rows"); 776 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t3(c31 int, c32 int) on commit preserve rows not logged on rollback delete rows"); 777 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t4(c41 int, c42 int) on commit preserve rows not logged on rollback delete rows"); 778 s.executeUpdate("insert into SESSION.t1 values(11, 1)"); 779 s.executeUpdate("insert into SESSION.t2 values(21, 1)"); 780 s.executeUpdate("insert into SESSION.t3 values(31, 1)"); 781 s.executeUpdate("insert into SESSION.t4 values(41, 1)"); 782 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 783 dumpRS(rs1); 784 rs1 = s.executeQuery("select * from SESSION.t2"); 785 dumpRS(rs1); 786 rs1 = s.executeQuery("select * from SESSION.t3"); 787 dumpRS(rs1); 788 rs1 = s.executeQuery("select * from SESSION.t4"); 789 dumpRS(rs1); 790 con.commit(); 791 792 System.out.println(" In the next transaction, insert couple more rows in t1 & t2 & t3 and "); 793 s.executeUpdate("insert into SESSION.t1 values(12, 2)"); 794 s.executeUpdate("insert into session.t2 values(22, 2)"); 795 s.executeUpdate("insert into session.t3 values(32, 2)"); 796 rs1 = s.executeQuery("select * from SESSION.t1"); 797 dumpRS(rs1); 798 rs1 = s.executeQuery("select * from SESSION.t2"); 799 dumpRS(rs1); 800 rs1 = s.executeQuery("select * from SESSION.t3"); 801 dumpRS(rs1); 802 803 System.out.println(" Create savepoint1 and delete some rows from t1 and inspect the data in t1"); 804 Savepoint savepoint1 = con.setSavepoint(); 805 s.executeUpdate("DELETE FROM SESSION.t1 where c12>1"); 806 rs1 = s.executeQuery("select * from SESSION.t1"); 807 dumpRS(rs1); 808 809 System.out.println(" Create savepoint2 and delete some rows from t2 this time and inspect the data in t2"); 810 Savepoint savepoint2 = con.setSavepoint(); 811 s.executeUpdate("DELETE FROM SESSION.t2 where c22>1"); 812 rs1 = s.executeQuery("select * from SESSION.t2"); 813 dumpRS(rs1); 814 815 System.out.println(" Release savepoint2 and now savepoint1 should keep track of changes made to t1 and t2, inspect the data in t1 & t2"); 816 con.releaseSavepoint(savepoint2); 817 rs1 = s.executeQuery("select * from SESSION.t1"); 818 dumpRS(rs1); 819 rs1 = s.executeQuery("select * from SESSION.t2"); 820 dumpRS(rs1); 821 822 System.out.println(" Rollback savepoint1 and should see no data in t1 and t2, inspect the data"); 823 con.rollback(savepoint1); 824 rs1 = s.executeQuery("select * from SESSION.t1"); 825 dumpRS(rs1); 826 rs1 = s.executeQuery("select * from SESSION.t2"); 827 dumpRS(rs1); 828 829 System.out.println(" Should see data in t3 since it was not touched in the savepoint that was rolled back"); 830 rs1 = s.executeQuery("select * from SESSION.t3"); 831 dumpRS(rs1); 832 833 System.out.println(" Rollback the transaction and should see no data in t1 and t2 and t3"); 834 con.rollback(); 835 rs1 = s.executeQuery("select * from SESSION.t1"); 836 dumpRS(rs1); 837 rs1 = s.executeQuery("select * from SESSION.t2"); 838 dumpRS(rs1); 839 rs1 = s.executeQuery("select * from SESSION.t3"); 840 dumpRS(rs1); 841 842 System.out.println(" Should see data in t4 since it was not touched in the transaction that was rolled back"); 843 rs1 = s.executeQuery("select * from SESSION.t4"); 844 dumpRS(rs1); 845 846 s.executeUpdate("drop table SESSION.t1"); 847 s.executeUpdate("drop table SESSION.t2"); 848 s.executeUpdate("drop table SESSION.t3"); 849 s.executeUpdate("drop table SESSION.t4"); 850 con.commit(); 851 System.out.println("TEST3M PASSED"); 852 } catch (Throwable e) 853 { 854 System.out.println("Unexpected message: "+ e.getMessage()); 855 con.rollback(); 856 passed = false; System.out.println("TEST3M FAILED"); 858 } 859 860 try 861 { 862 System.out.println("TEST3N : Savepoint and Rollback behavior"); 863 864 System.out.println(" In the transaction:"); 865 System.out.println(" declare temp table t1 & t2 & t3 & t4, insert few rows and commit"); 866 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged on rollback delete rows"); 867 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(c21 int, c22 int) on commit preserve rows not logged on rollback delete rows"); 868 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t3(c31 int, c32 int) on commit preserve rows not logged on rollback delete rows"); 869 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t4(c41 int, c42 int) on commit preserve rows not logged on rollback delete rows"); 870 s.executeUpdate("insert into SESSION.t1 values(11, 1)"); 871 s.executeUpdate("insert into SESSION.t1 values(12, 2)"); 872 s.executeUpdate("insert into SESSION.t2 values(21, 1)"); 873 s.executeUpdate("insert into SESSION.t2 values(22, 2)"); 874 s.executeUpdate("insert into SESSION.t3 values(31, 1)"); 875 s.executeUpdate("insert into SESSION.t4 values(41, 1)"); 876 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 877 dumpRS(rs1); 878 rs1 = s.executeQuery("select * from SESSION.t2"); 879 dumpRS(rs1); 880 rs1 = s.executeQuery("select * from SESSION.t3"); 881 dumpRS(rs1); 882 rs1 = s.executeQuery("select * from SESSION.t4"); 883 dumpRS(rs1); 884 con.commit(); 885 886 System.out.println(" In the next transaction, insert couple more rows in t3 "); 887 s.executeUpdate("insert into SESSION.t3 values(31, 2)"); 888 rs1 = s.executeQuery("select * from SESSION.t3"); 889 dumpRS(rs1); 890 891 System.out.println(" Create savepoint1 and delete some rows from t1 and inspect the data in t1"); 892 Savepoint savepoint1 = con.setSavepoint(); 893 s.executeUpdate("DELETE FROM SESSION.t1 where c12>1"); 894 rs1 = s.executeQuery("select * from SESSION.t1"); 895 dumpRS(rs1); 896 897 System.out.println(" delete from t2 with where clause such that no rows are deleted from t2 and inspect the data in t2"); 898 s.executeUpdate("DELETE FROM SESSION.t2 where c22>3"); 899 rs1 = s.executeQuery("select * from SESSION.t2"); 900 dumpRS(rs1); 901 902 System.out.println(" Create savepoint2 and delete some rows from t2 this time and inspect the data in t2"); 903 Savepoint savepoint2 = con.setSavepoint(); 904 s.executeUpdate("DELETE FROM SESSION.t2 where c22>1"); 905 rs1 = s.executeQuery("select * from SESSION.t2"); 906 dumpRS(rs1); 907 908 System.out.println(" Rollback the transaction and should see no data in t1 and t2 and t3"); 909 con.rollback(); 910 rs1 = s.executeQuery("select * from SESSION.t1"); 911 dumpRS(rs1); 912 rs1 = s.executeQuery("select * from SESSION.t2"); 913 dumpRS(rs1); 914 rs1 = s.executeQuery("select * from SESSION.t3"); 915 dumpRS(rs1); 916 917 System.out.println(" Should see data in t4 since it was not touched in the transaction that was rolled back"); 918 rs1 = s.executeQuery("select * from SESSION.t4"); 919 dumpRS(rs1); 920 921 s.executeUpdate("drop table SESSION.t1"); 922 s.executeUpdate("drop table SESSION.t2"); 923 s.executeUpdate("drop table SESSION.t3"); 924 s.executeUpdate("drop table SESSION.t4"); 925 con.commit(); 926 System.out.println("TEST3N PASSED"); 927 } catch (Throwable e) 928 { 929 System.out.println("Unexpected message: "+ e.getMessage()); 930 con.rollback(); 931 passed = false; System.out.println("TEST3N FAILED"); 933 } 934 935 try 936 { 937 System.out.println("TEST3O : Savepoint and Rollback behavior"); 938 939 System.out.println(" In the transaction:"); 940 System.out.println(" declare temp table t1 & t2, insert few rows and commit"); 941 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged on rollback delete rows"); 942 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(c21 int, c22 int) on commit preserve rows not logged on rollback delete rows"); 943 s.executeUpdate("insert into SESSION.t1 values(11, 1)"); 944 s.executeUpdate("insert into SESSION.t2 values(21, 1)"); 945 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 946 dumpRS(rs1); 947 rs1 = s.executeQuery("select * from SESSION.t2"); 948 dumpRS(rs1); 949 con.commit(); 950 951 System.out.println(" In the next transaction, insert couple more rows in t1 "); 952 s.executeUpdate("insert into SESSION.t1 values(12, 2)"); 953 rs1 = s.executeQuery("select * from SESSION.t1"); 954 dumpRS(rs1); 955 956 System.out.println(" Create savepoint1 and insert one row in t2 and inspect the data in t2"); 957 Savepoint savepoint1 = con.setSavepoint(); 958 s.executeUpdate("insert into SESSION.t2 values(22, 2)"); 959 rs1 = s.executeQuery("select * from SESSION.t2"); 960 dumpRS(rs1); 961 962 System.out.println(" Rollback savepoint1 and should see no data in t2 but t1 should have data, inspect the data"); 963 con.rollback(savepoint1); 964 rs1 = s.executeQuery("select * from SESSION.t1"); 965 dumpRS(rs1); 966 rs1 = s.executeQuery("select * from SESSION.t2"); 967 dumpRS(rs1); 968 969 System.out.println(" Commit the transaction and should see no data in t2 but t1 should have data"); 970 con.commit(); 971 rs1 = s.executeQuery("select * from SESSION.t1"); 972 dumpRS(rs1); 973 rs1 = s.executeQuery("select * from SESSION.t2"); 974 dumpRS(rs1); 975 976 s.executeUpdate("drop table SESSION.t1"); 977 s.executeUpdate("drop table SESSION.t2"); 978 con.commit(); 979 System.out.println("TEST3O PASSED"); 980 } catch (Throwable e) 981 { 982 System.out.println("Unexpected message: "+ e.getMessage()); 983 con.rollback(); 984 passed = false; System.out.println("TEST3O FAILED"); 986 } 987 988 try 989 { 990 System.out.println("TEST3P : Savepoint and Rollback behavior"); 991 992 System.out.println(" In the transaction:"); 993 System.out.println(" declare temp table t1, insert few rows and commit"); 994 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 995 s.executeUpdate("insert into SESSION.t1 values(11, 1)"); 996 s.executeUpdate("insert into SESSION.t1 values(12, 2)"); 997 con.commit(); 998 999 System.out.println(" In the transaction:"); 1000 System.out.println(" Create savepoint1 and insert some rows into t1 and inspect the data in t1"); 1001 Savepoint savepoint1 = con.setSavepoint(); 1002 s.executeUpdate("insert into SESSION.t1 values(13, 3)"); 1003 1004 System.out.println(" Release savepoint1 and now transaction should keep track of changes made to t1, inspect the data in t1"); 1005 con.releaseSavepoint(savepoint1); 1006 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); 1007 dumpRS(rs1); 1008 1009 System.out.println(" Rollback the transaction and should still see no data in t1"); 1010 con.rollback(); 1011 rs1 = s.executeQuery("select * from SESSION.t1"); 1012 dumpRS(rs1); 1013 1014 s.executeUpdate("drop table SESSION.t1"); 1015 con.commit(); 1016 System.out.println("TEST3P PASSED"); 1017 } catch (Throwable e) 1018 { 1019 System.out.println("Unexpected message: "+ e.getMessage()); 1020 con.rollback(); 1021 passed = false; System.out.println("TEST3P FAILED"); 1023 } 1024 1025 try 1026 { 1027 System.out.println("TEST3Q : Prepared statement test - DML and rollback behavior"); 1028 System.out.println(" In the transaction:"); 1029 System.out.println(" Declare temp table t2, insert / update / delete data using various prepared statements and commit"); 1030 s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(c21 int, c22 int) not logged on commit preserve rows"); 1031 PreparedStatement pStmtInsert = con.prepareStatement("insert into SESSION.t2 values (?, ?)"); 1032 pStmtInsert.setInt(1, 21); 1033 pStmtInsert.setInt(2, 1); 1034 pStmtInsert.execute(); 1035 pStmtInsert.setInt(1, 22); 1036 pStmtInsert.setInt(2, 2); 1037 pStmtInsert.execute(); 1038 pStmtInsert.setInt(1, 23); 1039 pStmtInsert.setInt(2, 2); 1040 pStmtInsert.execute(); 1041 PreparedStatement pStmtUpdate = con.prepareStatement("UPDATE SESSION.t2 SET c22 = 3 where c21=?"); 1042 pStmtUpdate.setInt(1, 23); 1043 pStmtUpdate.execute(); 1044 PreparedStatement pStmtDelete = con.prepareStatement("DELETE FROM SESSION.t2 where c21 = ?"); 1045 pStmtDelete.setInt(1, 23); 1046 pStmtDelete.execute(); 1047 1048 con.commit(); 1049 ResultSet rs1 = s.executeQuery("select * from SESSION.t2"); 1050 dumpRS(rs1); 1051 1052 System.out.println(" In the next transaction:"); 1053 System.out.println(" Create savepoint1 and insert some rows into t2 using prepared statement and inspect the data in t2"); 1054 Savepoint savepoint1 = con.setSavepoint(); 1055 pStmtInsert.setInt(1, 23); 1056 pStmtInsert.setInt(2, 2); 1057 pStmtInsert.execute(); 1058 rs1 = s.executeQuery("select * from SESSION.t2"); 1059 dumpRS(rs1); 1060 1061 System.out.println(" Create savepoint2 and update row inserted in savepoint1 using prepared statement and inspect the data in t2"); 1062 Savepoint savepoint2 = con.setSavepoint(); 1063 pStmtUpdate.setInt(1, 23); 1064 pStmtUpdate.execute(); 1065 rs1 = s.executeQuery("select * from SESSION.t2"); 1066 dumpRS(rs1); 1067 1068 System.out.println(" rollback savepoint2 and should loose all the data from t2"); 1069 con.rollback(savepoint2); 1070 rs1 = s.executeQuery("select * from SESSION.t2"); 1071 dumpRS(rs1); 1072 1073 System.out.println(" Create savepoint3 and insert some rows into t2 using prepared statement and inspect the data in t2"); 1074 Savepoint savepoint3 = con.setSavepoint(); 1075 pStmtInsert.setInt(1, 21); 1076 pStmtInsert.setInt(2, 1); 1077 pStmtInsert.execute(); 1078 pStmtInsert.setInt(1, 22); 1079 pStmtInsert.setInt(2, 2); 1080 pStmtInsert.execute(); 1081 pStmtInsert.setInt(1, 23); 1082 pStmtInsert.setInt(2, 333); 1083 pStmtInsert.execute(); 1084 rs1 = s.executeQuery("select * from SESSION.t2"); 1085 dumpRS(rs1); 1086 1087 System.out.println(" Create savepoint4 and update row inserted in savepoint3 using prepared statement and inspect the data in t2"); 1088 Savepoint savepoint4 = con.setSavepoint(); 1089 pStmtUpdate.setInt(1, 23); 1090 pStmtUpdate.execute(); 1091 rs1 = s.executeQuery("select * from SESSION.t2"); 1092 dumpRS(rs1); 1093 1094 System.out.println(" Release savepoint4 and inspect the data in t2, then delete a row from t2"); 1095 con.releaseSavepoint(savepoint4); 1096 rs1 = s.executeQuery("select * from SESSION.t2"); 1097 dumpRS(rs1); 1098 pStmtDelete.setInt(1, 23); 1099 pStmtDelete.execute(); 1100 1101 System.out.println(" Commit transaction and should see data data in t2"); 1102 con.commit(); 1103 rs1 = s.executeQuery("select * from SESSION.t2"); 1104 dumpRS(rs1); 1105 1106 s.executeUpdate("drop table SESSION.t2"); 1107 con.commit(); 1108 System.out.println("TEST3Q PASSED"); 1109 } catch (Throwable e) 1110 { 1111 System.out.println("FAIL " + e.getMessage()); 1112 e.printStackTrace(System.out); 1113 con.rollback(); 1114 passed = false; System.out.println("TEST3Q FAILED"); 1116 } 1117 1118 try 1119 { 1120 System.out.println("TEST4 : Test declared temporary table with ON COMMIT DELETE ROWS and holdable cursors and temp table as part of subquery"); 1121 1122 System.out.println("Temp table t1 with no direct held cursor open on it. Data should be deleted from t1 at commit time"); 1123 Statement s1 = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, 1124 ResultSet.HOLD_CURSORS_OVER_COMMIT ); 1125 s1.executeUpdate("create table t1(c11 int, c12 int)"); 1126 s1.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit delete rows not logged"); 1127 s1.executeUpdate("insert into session.t1 values(11, 1)"); 1128 s1.executeUpdate("insert into session.t1 values(12, 2)"); 1129 ResultSet rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 1131 rs1 = s1.executeQuery("select count(*) from t1"); dumpRS(rs1); 1133 System.out.println("Insert into real table using temporary table data on a statement with holdability set to true"); 1134 s1.executeUpdate("INSERT INTO T1 SELECT * FROM SESSION.T1"); 1135 con.commit(); 1136 1137 System.out.println("After commit, verify both the tables"); 1138 1139 System.out.println("Temp table t1 will have no data after commit"); 1140 rs1 = s1.executeQuery("select count(*) from SESSION.t1"); dumpRS(rs1); 1142 1143 System.out.println("Physical table t1 will have 2 rows after commit"); 1144 rs1 = s1.executeQuery("select count(*) from t1"); dumpRS(rs1); 1146 1147 s.executeUpdate("drop table SESSION.t1"); 1148 s.executeUpdate("drop table t1"); 1149 1150 con.commit(); 1151 System.out.println("TEST4 PASSED"); 1152 } catch (Throwable e) 1153 { 1154 System.out.println("Unexpected message: "+ e.getMessage()); 1155 con.rollback(); 1156 passed = false; System.out.println("TEST4 FAILED"); 1158 } 1159 1160 return passed; 1161 } 1162 1163 1174 static boolean testPooledConnectionClose() 1175 throws SQLException { 1176 boolean passed = true; 1177 Connection con1 = null, con2 = null; 1178 1179 try 1180 { 1181 System.out.println("TEST5 : Temporary tables declared in a pooled connection should get dropped when that pooled connection is closed"); 1182 ConnectionPoolDataSource dsp; 1183 if (isDerbyNet) { 1184 1195 System.out.println("test will not build without universal driver"); 1196 return passed; 1197 1198 } else { 1199 EmbeddedConnectionPoolDataSource dscsp = new EmbeddedConnectionPoolDataSource(); 1200 dscsp.setDatabaseName("wombat"); 1201 dsp = dscsp; 1203 } 1204 1205 PooledConnection pc = dsp.getPooledConnection(); 1206 con1 = pc.getConnection(); 1207 con1.setAutoCommit(false); 1208 Statement s = con1.createStatement(); 1209 1210 System.out.println(" In the first connection handle to the pooled connection, create physical session schema, create table t1 in it"); 1211 System.out.println(" Insert some rows in physical SESSION.t1 table. Inspect the data."); 1212 s.executeUpdate("CREATE schema SESSION"); 1213 s.executeUpdate("CREATE TABLE SESSION.t1(c21 int)"); 1214 s.executeUpdate("insert into SESSION.t1 values(11)"); 1215 s.executeUpdate("insert into SESSION.t1 values(12)"); 1216 s.executeUpdate("insert into SESSION.t1 values(13)"); 1217 ResultSet rs1 = s.executeQuery("select * from SESSION.t1"); dumpRS(rs1); 1219 1220 System.out.println(" Next declare a temp table with same name as physical table in SESSION schema."); 1221 System.out.println(" Insert some rows in temporary table SESSION.t1. Inspect the data"); 1222 s.executeUpdate("declare global temporary table SESSION.t1(c11 int, c12 int) on commit preserve rows not logged"); 1223 s.executeUpdate("insert into SESSION.t1 values(11,1)"); 1224 rs1 = s.executeQuery("select * from SESSION.t1"); dumpRS(rs1); 1226 System.out.println(" Now close the connection handle to the pooled connection"); 1227 con1.commit(); 1228 con1.close(); 1229 con1=null; 1230 1231 System.out.println(" Do another getConnection() to get a new connection handle to the pooled connection"); 1232 con2 = pc.getConnection(); 1233 s = con2.createStatement(); 1234 System.out.println(" In this new handle, a select * from SESSION.t1 should be looking at the physical session table"); 1235 rs1 = s.executeQuery("select * from SESSION.t1"); 1236 dumpRS(rs1); 1237 1238 s.executeUpdate("DROP TABLE SESSION.t1"); 1239 if (isDerbyNet) 1240 s.executeUpdate("DROP TABLE SESSION.t1"); 1241 1242 s.executeUpdate("DROP schema SESSION restrict"); 1243 con2.commit(); 1244 con2.close(); 1245 System.out.println("TEST5 PASSED"); 1246 } catch (Throwable e) 1247 { 1248 System.out.println("Unexpected message: "+ e.getMessage()); 1249 if (con1 != null) con1.rollback(); 1250 if (con2 != null) con2.rollback(); 1251 passed = false; System.out.println("TEST5 FAILED"); 1253 } 1254 1255 return passed; 1256 } 1257 1258 static private void dumpExpectedSQLExceptions (SQLException se) { 1259 System.out.println("PASS -- expected exception"); 1260 while (se != null) 1261 { 1262 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 1263 se = se.getNextException(); 1264 } 1265 } 1266 1267 static private void dumpSQLExceptions (SQLException se) { 1268 System.out.println("FAIL -- unexpected exception"); 1269 while (se != null) { 1270 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 1271 se.printStackTrace(System.out); 1272 se = se.getNextException(); 1273 } 1274 } 1275 1276 private static void dumpRS(ResultSet s) throws SQLException 1278 { 1279 if (s == null) 1280 { 1281 System.out.println("<NULL>"); 1282 return; 1283 } 1284 1285 ResultSetMetaData rsmd = s.getMetaData(); 1286 1287 int numCols = rsmd.getColumnCount(); 1289 1290 if (numCols <= 0) 1291 { 1292 System.out.println("(no columns!)"); 1293 return; 1294 } 1295 1296 StringBuffer heading = new StringBuffer ("\t "); 1297 StringBuffer underline = new StringBuffer ("\t "); 1298 1299 int len; 1300 for (int i=1; i<=numCols; i++) 1302 { 1303 if (i > 1) 1304 { 1305 heading.append(","); 1306 underline.append(" "); 1307 } 1308 len = heading.length(); 1309 heading.append(rsmd.getColumnLabel(i)); 1310 len = heading.length() - len; 1311 for (int j = len; j > 0; j--) 1312 { 1313 underline.append("-"); 1314 } 1315 } 1316 System.out.println(heading.toString()); 1317 System.out.println(underline.toString()); 1318 1319 1320 StringBuffer row = new StringBuffer (); 1321 while (s.next()) 1323 { 1324 row.append("\t{"); 1325 for (int i=1; i<=numCols; i++) 1328 { 1329 if (i > 1) row.append(","); 1330 row.append(s.getString(i)); 1331 } 1332 row.append("}\n"); 1333 } 1334 System.out.println(row.toString()); 1335 s.close(); 1336 } 1337} 1338 | Popular Tags |