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 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.declareGlobalTempTableJavaJDBC30Test
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.lang;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.J2EEDataSource;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Test for declared global temporary tables (introduced in Cloudscape 5.2)
* and pooled connection close and jdbc 3.0 specific features
* The jdbc3.0 specific featuers are holdable cursors, savepoints.
* The rest of the temp table test are in declareGlobalTempTableJavaTest class.
* The reason for a different test class is that the holdability and savepoint
* support is under jdk14 and higher. But we want to be able to run the
* non-holdable tests under all the jdks we support and hence splitting the
* tests into two separate tests.
*/
public class DeclareGlobalTempTableJavaJDBC30Test extends BaseJDBCTestCase {
public DeclareGlobalTempTableJavaJDBC30Test(String name)
{
super(name);
}
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite();
suite.addTest(TestConfiguration.embeddedSuite(
DeclareGlobalTempTableJavaJDBC30Test.class));
suite.addTest(TestConfiguration.clientServerDecorator(
getClientSuite()));
return suite;
}
/**
* Return a suite of tests that are run with client only
*
* @return A suite of tests being run with client only
*/
private static Test getClientSuite() {
BaseTestSuite suite = new BaseTestSuite("Client/Server");
// skip the tests with more than 1 savepoint -
// see (lack of support described in) DERBY-3687
// so, only do the following with network server/client:
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testOnCommitPreserveRowsWithHoldability"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testSavepointRollbackbehaviour7"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testSavepointRollbackbehaviour8"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testSavepointRollbackbehaviour11"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testSavepointRollbackbehaviour12"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testTest4"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testPooledConnectionClosed"));
// following 2 fail with network server; see DERBY-4373
/*
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testOnCommitDeleteRowsWithHoldableOpenCursor"));
suite.addTest(new DeclareGlobalTempTableJavaJDBC30Test(
"testOnCommitDeleteRowsHoldabilityWithPreparedStatements"));
*/
return suite;
}
protected void setUp() throws Exception {
super.setUp();
getConnection().setAutoCommit(false);
}
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Tests that If a Global Temp table(with on commi delete rows) has open
* cursors held on it
* Data should be preserved in at commit time
*
* @throws SQLException
*/
public void testOnCommitDeleteRowsWithHoldableOpenCursor()
throws SQLException {
Statement s1 = getConnection().createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
s1.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit delete rows not logged");
s1.executeUpdate("insert into session.t1 values(11, 1)");
s1.executeUpdate("insert into session.t1 values(12, 2)");
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "2");
//hold cursor open on t1. Commit should preserve the rows
ResultSet rs1 = s1.executeQuery("select * from SESSION.t1");
rs1.next();
//cursor opened
//Temp tables t2 & t3 with one held open cursor on them together. Data
//should be preserved in t2 & t3 at commit time
Statement s2 = getConnection().createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
s2.executeUpdate("declare global temporary table SESSION.t2(" +
"c21 int, c22 int) on commit delete rows not logged");
s2.executeUpdate("insert into session.t2 values(21, 1)");
s2.executeUpdate("insert into session.t2 values(22, 2)");
JDBC.assertSingleValueResultSet(s2.executeQuery(
"select count(*) from SESSION.t2"), "2");
s2.executeUpdate("declare global temporary table SESSION.t3(" +
"c31 int, c32 int) on commit delete rows not logged");
s2.executeUpdate("insert into session.t3 values(31, 1)");
s2.executeUpdate("insert into session.t3 values(32, 2)");
JDBC.assertSingleValueResultSet(s2.executeQuery(
"select count(*) from SESSION.t3") , "2");
//hold cursor open on t2 & t3. Commit should preseve the rows
ResultSet rs23 = s2.executeQuery(
"select * from SESSION.t2, SESSION.t3 where c22=c32");
rs23.next();
//cursor Opened
Statement s3 = getConnection().createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
s3.executeUpdate("declare global temporary table SESSION.t4(" +
"c41 int, c42 int) on commit delete rows not logged");
s3.executeUpdate("insert into session.t4 values(41, 1)");
s3.executeUpdate("insert into session.t4 values(42, 2)");
ResultSet rs4 = s3.executeQuery("select count(*) from SESSION.t4");
JDBC.assertSingleValueResultSet(rs4 , "2");
//hold cursor open on t4 but close it before commit,
//Data should be deleted after commit
rs4 = s3.executeQuery("select * from SESSION.t4");
rs4.next();
rs4.close();
//rs4 closed before committing.
commit();
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "2");
JDBC.assertSingleValueResultSet(s2.executeQuery(
"select count(*) from SESSION.t2") , "2");
JDBC.assertSingleValueResultSet(s2.executeQuery(
"select count(*) from SESSION.t3") , "2");
JDBC.assertSingleValueResultSet(s3.executeQuery(
"select count(*) from SESSION.t4") , "0");
s1.executeUpdate("drop table SESSION.t1");
s2.executeUpdate("drop table SESSION.t2");
s3.executeUpdate("drop table SESSION.t3");
s3.executeUpdate("drop table SESSION.t4");
rs1.close(); rs23.close(); rs4.close();
commit();
s1.close(); s2.close(); s3.close();
}
/**
* Test declared temporary table with ON COMMIT DELETE ROWS and holdable
* cursors on prepared statement
*
* @throws SQLException
*/
public void testOnCommitDeleteRowsHoldabilityWithPreparedStatements()
throws SQLException {
Statement s1 = createStatement();
s1.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit delete rows not logged");
s1.executeUpdate("insert into session.t1 values(11, 1)");
s1.executeUpdate("insert into session.t1 values(12, 2)");
//create a prepared statement with hold cursors over commit
PreparedStatement ps1 = getConnection().prepareStatement(
"select count(*) from SESSION.t1",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
ResultSet rs1 = ps1.executeQuery();
JDBC.assertSingleValueResultSet(rs1 , "2");
PreparedStatement ps2 = getConnection().prepareStatement(
"select * from SESSION.t1", ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
//hold cursor open on t1. Commit should preserve the rows
ResultSet rs11 = ps2.executeQuery();
rs11.next();
//Cursor was Opened.
//Temp table t2 with one held cursor but it is closed before commit.
//Data should be deleted from t2 at commit time
s1.executeUpdate("declare global temporary table SESSION.t2(" +
"c21 int, c22 int) on commit delete rows not logged");
s1.executeUpdate("insert into session.t2 values(21, 1)");
s1.executeUpdate("insert into session.t2 values(22, 2)");
//create a prepared statement with hold cursors over commit
PreparedStatement ps3 = getConnection().prepareStatement(
"select count(*) from SESSION.t2",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
ResultSet rs2 = ps3.executeQuery();
JDBC.assertSingleValueResultSet(rs2 , "2");
PreparedStatement ps4 = getConnection().prepareStatement(
"select * from SESSION.t2", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT );
//hold cursor open on t2 but close it before commit
rs2 = ps4.executeQuery();
rs2.next();
rs2.close();
//commiting Point
commit();
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "2");
//Need to close the held cursor on t1 before t1 can be dropped
rs11.close();
rs1.close();
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t2") , "0");
s1.executeUpdate("drop table SESSION.t1");
s1.executeUpdate("drop table SESSION.t2");
}
/**
* Tests a temporary table with ON COMMIT PRESERVE ROWS and various
* combinations of holdability.
* Temp table t1 with held open cursors on it. Data should be preserved,
* holdability shouldn't matter
*
* @throws SQLException
*/
public void testOnCommitPreserveRowsWithHoldability() throws SQLException {
//create a statement with hold cursors over commit
Statement s1 = getConnection().createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
s1.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
s1.executeUpdate("insert into session.t1 values(11, 1)");
s1.executeUpdate("insert into session.t1 values(12, 2)");
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "2");
//Opening the Cursor
ResultSet rs1 = s1.executeQuery("select * from SESSION.t1");
rs1.next();
//create a statement without hold cursors.
Statement s2 = createStatement();
s1.executeUpdate("declare global temporary table SESSION.t2(" +
"c11 int, c12 int) on commit preserve rows not logged");
s1.executeUpdate("insert into session.t2 values(11, 1)");
s1.executeUpdate("insert into session.t2 values(12, 2)");
JDBC.assertSingleValueResultSet(s2.executeQuery(
"select count(*) from SESSION.t2") , "2");
//Opening The Cursor
ResultSet rs2 = s2.executeQuery("select * from SESSION.t2");
rs2.next();
//commiting point
commit();
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "2");
JDBC.assertSingleValueResultSet(s2.executeQuery(
"select count(*) from SESSION.t2") , "2");
s1.executeUpdate("drop table SESSION.t1");
s2.executeUpdate("drop table SESSION.t2");
}
/**
* Savepoint and Rollback behavior - 1 (3A)
* In the transaction Create savepoint1 and declare temp table t1.
* Create savepoint 2, drop temp table t1, rollback savepoint 2
* select should pass, rollback savepoint 1, select should fail.
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour1() throws SQLException {
Statement s = createStatement();
//Set The First savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
//create a temp table t1
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
PreparedStatement pStmt = prepareStatement(
"insert into SESSION.t1 values (?, ?)");
pStmt.setInt(1, 11);
pStmt.setInt(2, 1);
pStmt.execute();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "1");
//Set The Second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
//drop the temp table t1
s.executeUpdate("drop table SESSION.t1");
assertStatementError("42X05" , s , "select * from SESSION.t1");
//Rollback to the second savepoint - drop table Operation is rolled
//back hence we should have t1
getConnection().rollback(savepoint2);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
//Rollback to the First savepoint - create table Operation is also
//rolled back hence there is no t1
getConnection().rollback(savepoint1);
assertStatementError("42X05" , s , "select * from SESSION.t1");
}
/**
* Savepoint and Rollback behavior - 2 (3B)
* In the transaction Create savepoint1 and declare temp table t1.
* Create savepoint2 and declare temp table t2
* Release savepoint 1 and select from temp table t1 and t2.
* Drop temp table t2(explicit drop), rollback transaction(implicit drop
* of t1) Select from temp table t1 and t2 will fail.
*
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour2() throws SQLException {
Statement s = createStatement();
//Set First savepoint (save point - 1)
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
//Set Second savepoint (save point - 2)
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("declare global temporary table SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged");
//Release First savepoint
getConnection().releaseSavepoint(savepoint1);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
//Drop the Temp Table t2 Explicitly and t1 Implicitly(by rolling back)
s.executeUpdate("drop table SESSION.t2");
rollback();
assertStatementError("42X05" , s , "select * from SESSION.t1");
assertStatementError("42X05" , s , "select * from SESSION.t2");
}
/**
* Savepoint and Rollback behavior - 3 (3C)
* In the transaction Create savepoint1 and declare temp table t1.
* Create savepoint2 and declare temp table t2.
* Release savepoint 1 and select from temp table t1 and t2 should pass.
* Create savepoint3 and rollback savepoint3(should not touch t1 and t2)
* select from temp tables t1 and t2 should pass.
* Rollback transaction and select from temp tables t1 and t2 should fail.
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour3() throws SQLException {
Statement s = createStatement();
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
//set second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("declare global temporary table SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged");
//release the first savepoint
getConnection().releaseSavepoint(savepoint1);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
//set third savepoint
Savepoint savepoint3 = getConnection().setSavepoint();
//rollback to third savepoint - (should not touch t1 and t2)
getConnection().rollback(savepoint3);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
//rollback the entire transaction - this rolls back create statements
//also hence t1 , t2 got deleted
rollback();
assertStatementError("42X05" , s , "select * from SESSION.t1");
assertStatementError("42X05" , s , "select * from SESSION.t2");
}
/**
* Savepoint and Rollback behavior - 4 (3D)
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour4() throws SQLException {
Statement s = createStatement();
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
//set second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("drop table SESSION.t1");
//rollback to second savepoint
getConnection().rollback(savepoint2);
JDBC.assertSingleValueResultSet(s.executeQuery("" +
"select count(*) from SESSION.t1") , "0");
//commit
commit();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
s.executeUpdate("drop table SESSION.t1");
}
/**
* Savepoint and Rollback behavior - 5 - 3E
* Tests the basic definition of savepoint in combination with Rollback.
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour5() throws SQLException {
Statement s = createStatement();
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
//set second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("drop table SESSION.t1");
//rollback to first savepoint
getConnection().rollback(savepoint1);
assertStatementError("42X05" , s , "select * from SESSION.t1");
}
/**
* Savepoint and Rollback behavior - 6 - 3J
* In the transaction: declare temp table t1 with 2 columns and commit.
* Create savepoint1 and drop temp table t1 with 2 columns, declare temp
* table t1 but this time with 3 columns.
* Create savepoint2 and drop temp table t1 with 3 columns, rollback,
* select from temp table t1 here should have 2 columns
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour6() throws SQLException {
String[] s1 = {"C11" , "C12"};
String[] s2 = {"C11" , "C12" , "C13"};
Statement s = createStatement();
// declare temp table t1 with 2 columns
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
s.executeUpdate("insert into SESSION.t1 values(11, 11)");
ResultSet rs1 = s.executeQuery("select * from SESSION.t1");
JDBC.assertColumnNames(rs1 , s1);
//commiting point
getConnection().commit();
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("drop table SESSION.t1");
//declare temp table t1 with 3 columns
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int, c13 int not null) on commit preserve rows " +
"not logged");
s.executeUpdate("insert into SESSION.t1 values(22, 22, 22)");
rs1 = s.executeQuery("select * from SESSION.t1");
JDBC.assertColumnNames(rs1 , s2);
//set second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("drop table SESSION.t1");
//rollback the transaction
rollback();
rs1 = s.executeQuery("select * from SESSION.t1");
JDBC.assertColumnNames(rs1 , s1);
s.executeUpdate("drop table SESSION.t1");
}
/**
* Savepoint and Rollback behavior - 7 - 3K
* tests the savepoint and rollback behaviour with update command
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour7() throws SQLException {
Statement s = createStatement();
//declare temp table t1 & t2, insert few rows and commit
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged " +
"on rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged");
s.executeUpdate("insert into SESSION.t1 values(11, 1)");
s.executeUpdate("insert into session.t2 values(21, 1)");
commit();
//committed - the first transaction has been completed
//In the next transaction, insert couple more rows in t1 & t2
s.executeUpdate("insert into SESSION.t1 values(12, 2)");
s.executeUpdate("insert into SESSION.t2 values(22, 2)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "2");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "2");
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("UPDATE SESSION.t1 SET c12 = 3 where c12>1");
s.executeUpdate("UPDATE SESSION.t2 SET c22 = 3 where c22>2");
//rollback to first savepoint
getConnection().rollback(savepoint1);
//Rollback to savepoint1 and we should loose all the rows in t1 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)
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
commit();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
s.executeUpdate("drop table SESSION.t1");
s.executeUpdate("drop table SESSION.t2");
}
/**
* Savepoint and Rollback behavior - 8 - 3L
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour8() throws SQLException {
Statement s = createStatement();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("insert into SESSION.t1 values(11, 1)");
s.executeUpdate("insert into session.t2 values(21, 1)");
commit();
//insert couple more rows in t1 & t2 and
//Create savepoint1 and update some rows in t1 and inspect the data
s.executeUpdate("insert into SESSION.t1 values(12, 2)");
s.executeUpdate("insert into session.t2 values(22, 2)");
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("UPDATE SESSION.t1 SET c12 = 3 where c12>1");
s.executeUpdate("UPDATE SESSION.t2 SET c22 = 3 where c22>3");
//rollback to first savepoint
getConnection().rollback(savepoint1);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery("select count(*) from SESSION.t1") , "0");
rollback();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
//cleanUp
s.executeUpdate("drop table SESSION.t1");
s.executeUpdate("drop table SESSION.t2");
}
/**
* Savepoint and Rollback behavior - 9 - 3M
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour9() throws SQLException {
Statement s = createStatement();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged " +
"on rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t3(" +
"c31 int, c32 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t4(" +
"c41 int, c42 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("insert into SESSION.t1 values(11, 1)");
s.executeUpdate("insert into SESSION.t2 values(21, 1)");
s.executeUpdate("insert into SESSION.t3 values(31, 1)");
s.executeUpdate("insert into SESSION.t4 values(41, 1)");
commit();
//Beginning of second transaction
//Insert a couple of more rows into t1 , t2 and t3
s.executeUpdate("insert into SESSION.t1 values(12, 2)");
s.executeUpdate("insert into session.t2 values(22, 2)");
s.executeUpdate("insert into session.t3 values(32, 2)");
//set first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("DELETE FROM SESSION.t1 where c12>1");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "1");
//set second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("DELETE FROM SESSION.t2 where c22>1");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "1");
//Release savepoint2 and now savepoint1 should keep track of changes
//made to t1 and t2
getConnection().releaseSavepoint(savepoint2);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "1");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "1");
// Rollback savepoint1 and should see no data in t1 and t2
getConnection().rollback(savepoint1);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
//Should see data in t3 since it was not touched in the savepoint that
//was rolled back
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t3") , "2");
rollback();
//Rolling back the transaction we should see no data in t1 and t2 and t3
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t3") , "0");
//Should see data in t4 since it was not touched in the transaction that was rolled back
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t4") , "1");
s.executeUpdate("drop table SESSION.t1");
s.executeUpdate("drop table SESSION.t2");
s.executeUpdate("drop table SESSION.t3");
s.executeUpdate("drop table SESSION.t4");
}
/**
* Savepoint and Rollback behavior - 10 - 3N
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour10() throws SQLException {
Statement s = createStatement();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t3(" +
"c31 int, c32 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t4(" +
"c41 int, c42 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("insert into SESSION.t1 values(11, 1)");
s.executeUpdate("insert into SESSION.t1 values(12, 2)");
s.executeUpdate("insert into SESSION.t2 values(21, 1)");
s.executeUpdate("insert into SESSION.t2 values(22, 2)");
s.executeUpdate("insert into SESSION.t3 values(31, 1)");
s.executeUpdate("insert into SESSION.t4 values(41, 1)");
commit();
//In the next transaction, insert couple more rows in t3
s.executeUpdate("insert into SESSION.t3 values(31, 2)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t3") , "2");
//Create savepoint1 and delete some rows from t1
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("DELETE FROM SESSION.t1 where c12>1");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "1");
s.executeUpdate("DELETE FROM SESSION.t2 where c22>3");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "2");
//Create savepoint2 and delete some rows from t2
Savepoint savepoint2 = getConnection().setSavepoint();
s.executeUpdate("DELETE FROM SESSION.t2 where c22>1");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "1");
//Rollback the transaction and should see no data in t1 and t2 and t3
rollback();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t3") , "0");
//Should see data in t4 since it was not touched in the transaction
//that was rolled back
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t4") , "1");
s.executeUpdate("drop table SESSION.t1");
s.executeUpdate("drop table SESSION.t2");
s.executeUpdate("drop table SESSION.t3");
s.executeUpdate("drop table SESSION.t4");
}
/**
* Savepoint and Rollback behavior - 11 - 3O
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour11() throws SQLException {
Statement s = createStatement();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
"c21 int, c22 int) on commit preserve rows not logged on " +
"rollback delete rows");
s.executeUpdate("insert into SESSION.t1 values(11, 1)");
s.executeUpdate("insert into SESSION.t2 values(21, 1)");
commit();
s.executeUpdate("insert into SESSION.t1 values(12, 2)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "2");
//set the first savepoint here
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("insert into SESSION.t2 values(22, 2)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "2");
//Rollback savepoint1; expect no data in t2 but t1 should have data
getConnection().rollback(savepoint1);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "2");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
//Commit the transaction; expect no data in t2 but t1 should have data
commit();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "2");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
s.executeUpdate("drop table SESSION.t1");
s.executeUpdate("drop table SESSION.t2");
}
/**
* Savepoint and Rollback behavior - 12 - 3P
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour12() throws SQLException {
Statement s = createStatement();
s.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit preserve rows not logged");
s.executeUpdate("insert into SESSION.t1 values(11, 1)");
s.executeUpdate("insert into SESSION.t1 values(12, 2)");
commit();
//set the first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
s.executeUpdate("insert into SESSION.t1 values(13, 3)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "3");
//release the savepoint - now transaction should keep track of changes
//made to t1
getConnection().releaseSavepoint(savepoint1);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "3");
//Rollback the transaction and should still see no data in t1
rollback();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t1") , "0");
s.executeUpdate("drop table SESSION.t1");
}
/**
* Savepoint and Rollback behavior - 13 - 3Q
* tests the rollback , savepoint behaviour with prepartedStatement
*
* @throws SQLException
*/
public void testSavepointRollbackbehaviour13() throws SQLException {
Statement s = createStatement();
s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
"c21 int, c22 int) not logged on commit preserve rows");
PreparedStatement pStmtInsert = prepareStatement(
"insert into SESSION.t2 values (?, ?)");
pStmtInsert.setInt(1, 21);
pStmtInsert.setInt(2, 1);
pStmtInsert.execute();
pStmtInsert.setInt(1, 22);
pStmtInsert.setInt(2, 2);
pStmtInsert.execute();
pStmtInsert.setInt(1, 23);
pStmtInsert.setInt(2, 2);
pStmtInsert.execute();
PreparedStatement pStmtUpdate = prepareStatement(
"UPDATE SESSION.t2 SET c22 = 3 where c21=?");
pStmtUpdate.setInt(1, 23);
pStmtUpdate.execute();
PreparedStatement pStmtDelete = prepareStatement(
"DELETE FROM SESSION.t2 where c21 = ?");
pStmtDelete.setInt(1, 23);
pStmtDelete.execute();
commit();//committing point
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "2");
//set the first savepoint
Savepoint savepoint1 = getConnection().setSavepoint();
pStmtInsert.setInt(1, 23);
pStmtInsert.setInt(2, 2);
pStmtInsert.execute();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "3");
//set the second savepoint
Savepoint savepoint2 = getConnection().setSavepoint();
pStmtUpdate.setInt(1, 23);
pStmtUpdate.execute();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "3");
//rollback savepoint2 and should loose all the data from t2");
getConnection().rollback(savepoint2);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "0");
//Create savepoint3, insert some rows into t2 using prepared statement
Savepoint savepoint3 = getConnection().setSavepoint();
pStmtInsert.setInt(1, 21);
pStmtInsert.setInt(2, 1);
pStmtInsert.execute();
pStmtInsert.setInt(1, 22);
pStmtInsert.setInt(2, 2);
pStmtInsert.execute();
pStmtInsert.setInt(1, 23);
pStmtInsert.setInt(2, 333);
pStmtInsert.execute();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "3");
//Create savepoint4 and update row inserted in savepoint3 using prepared
//statement and inspect the data in t2
Savepoint savepoint4 = getConnection().setSavepoint();
pStmtUpdate.setInt(1, 23);
pStmtUpdate.execute();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "3");
//release savepoint4
getConnection().releaseSavepoint(savepoint4);
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "3");
pStmtDelete.setInt(1, 23);
pStmtDelete.execute();
//Commit transaction and should see data in t2
commit();
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.t2") , "2");
s.executeUpdate("drop table SESSION.t2");
}
/**
* Test declared temporary table with ON COMMIT DELETE ROWS and holdable
* cursors and temp table as part of subquery
* Temp table t1 with no direct held cursor open on it.
* Data should be deleted from t1 at commit time
*
* @throws SQLException
*/
public void testTest4() throws SQLException {
Statement s1 = getConnection().createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT );
try {
s1.executeUpdate("drop table t1");
}
catch(SQLException e)
{
assertSQLState("42Y55" , e);
}
s1.executeUpdate("create table t1(c11 int, c12 int)");
s1.executeUpdate("declare global temporary table SESSION.t1(" +
"c11 int, c12 int) on commit delete rows not logged");
s1.executeUpdate("insert into session.t1 values(11, 1)");
s1.executeUpdate("insert into session.t1 values(12, 2)");
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "2");
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from t1") , "0");
//Insert into real table using temporary table data on a statement with
//holdability set to true
s1.executeUpdate("INSERT INTO T1 SELECT * FROM SESSION.T1");
commit();
//Temp table t1 will have no data after commit
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from SESSION.t1") , "0");
//Physical table t1 will have 2 rows after commit
JDBC.assertSingleValueResultSet(s1.executeQuery(
"select count(*) from t1") , "2");
s1.executeUpdate("drop table SESSION.t1");
s1.executeUpdate("drop table t1");
}
/**
* Temporary tables declared in a pooled connection should get dropped
* when that pooled connection is closed.
*
* @throws SQLException
*/
public void testPooledConnectionClosed() throws SQLException {
ConnectionPoolDataSource dscsp =
J2EEDataSource.getConnectionPoolDataSource();
//In the first connection handle to the pooled connection, create
//physical session schema, create table t1 in it
PooledConnection pc = dscsp.getPooledConnection();
Connection pcon = pc.getConnection();
Statement s = pcon.createStatement();
try {
s.executeUpdate("CREATE schema SESSION");
}
catch(SQLException e)
{
assertSQLState("X0Y68" , e);
}
s.executeUpdate("CREATE TABLE SESSION.tx(c21 int)");
s.executeUpdate("insert into SESSION.tx values(11)");
s.executeUpdate("insert into SESSION.tx values(12)");
s.executeUpdate("insert into SESSION.tx values(13)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.tx") , "3");
//Declare temp table with same name as physical table in SESSION schema
s.executeUpdate("declare global temporary table SESSION.tx(" +
"c11 int, c12 int) on commit preserve rows not logged");
s.executeUpdate("insert into SESSION.tx values(11,1)");
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.tx") , "1");
commit();
//Now close the connection handle to the pooled connection
getConnection().close();
//Do another getConnection() to get a new connection handle to the
//pooled connection
s = getConnection().createStatement();
//In this new handle, a select * from SESSION.tx should be looking at
//the physical session table
JDBC.assertSingleValueResultSet(s.executeQuery(
"select count(*) from SESSION.tx") , "3");
s.executeUpdate("drop table SESSION.tx");
}
/**
* Test that we don't get an NPE when re-using a PreparedStatement
* on a temp table declared and then rolled back. See DERBY-6189.
*/
public void test_derby_6189() throws Exception
{
Connection conn = getConnection();
conn.prepareStatement
( "DECLARE GLOBAL TEMPORARY TABLE SESSION.t6189( c21 int, c22 int) not logged on commit preserve rows" )
.execute();
PreparedStatement pStmtInsert = conn.prepareStatement( "insert into SESSION.t6189 values (23, 1)" );
pStmtInsert.execute();
conn.rollback();
assertStatementError("42X05", pStmtInsert);
}
}
|