1 21 22 package org.opensubsystems.core.persist.db.driver; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 import junit.extensions.TestSetup; 31 import junit.framework.Test; 32 import junit.framework.TestSuite; 33 34 import org.opensubsystems.core.error.OSSException; 35 import org.opensubsystems.core.persist.db.Database; 36 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 37 import org.opensubsystems.core.persist.db.DatabaseImpl; 38 import org.opensubsystems.core.persist.db.DatabaseTest; 39 import org.opensubsystems.core.persist.db.DatabaseTestSetup; 40 import org.opensubsystems.core.persist.db.DatabaseTestSuite; 41 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 42 import org.opensubsystems.core.util.DatabaseUtils; 43 import org.opensubsystems.core.util.Log; 44 45 53 public final class TransactionTest 54 { 55 57 60 private TransactionTest( 61 ) 62 { 63 } 65 66 68 74 public static Test suite( 75 ) 76 { 77 TestSuite suite = new DatabaseTestSuite("TransactionTest"); 78 suite.addTestSuite(TransactionTestInternal.class); 79 TestSetup wrapper = new DatabaseTestSetup(suite); 80 81 return wrapper; 82 } 83 84 89 public static class TransactionTestInternal extends DatabaseTest 90 { 91 93 96 private static Logger s_logger = Log.getInstance(TransactionTestInternal.class); 97 98 101 static 102 { Database dbDatabase; 104 try 105 { dbDatabase = DatabaseImpl.getInstance(); 107 dbDatabase.add(DatabaseTestSchema.class); 108 } 109 catch (OSSException bfeExc) 110 { 111 throw new RuntimeException ("Unexpected exception.", bfeExc); 112 } 113 } 114 119 public TransactionTestInternal( 120 String strTestName 121 ) 122 { 123 super(strTestName); 124 } 125 126 134 public void testRollbackUniqueWithJTA( 135 ) throws Throwable 136 { 137 final String INSERT_VALUE = "insert into ROLLBACK_TEST (TEST_COLUMN) values (?)"; 138 final String DELETE_ALL = "delete from ROLLBACK_TEST"; 139 final String UPDATE_VALUE 140 = "update ROLLBACK_TEST set TEST_COLUMN = ? where TEST_COLUMN = ?"; 141 final String VALUE_TEST_1 = "value one 1"; 142 final String VALUE_TEST_2 = "value two 2"; 143 144 PreparedStatement insertStatement = null; 145 int iUpdateCount; 146 try 147 { 148 m_transaction.begin(); 149 150 try 151 { 152 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 153 insertStatement.setString(1, VALUE_TEST_1); 154 155 iUpdateCount = DatabaseUtils.executeUpdateAndClose(insertStatement); 156 157 m_transaction.commit(); 158 } 159 catch (Throwable throwable) 160 { 161 m_transaction.rollback(); 162 throw throwable; 163 } 164 finally 165 { 166 DatabaseUtils.closeStatement(insertStatement); 167 insertStatement = null; 168 } 169 170 assertEquals("Exactly one record have been inserted.", 171 iUpdateCount, 1); 172 173 m_transaction.begin(); 174 175 try 176 { 177 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 178 insertStatement.setString(1, VALUE_TEST_2); 179 180 iUpdateCount = DatabaseUtils.executeUpdateAndClose(insertStatement); 181 182 m_transaction.commit(); 183 } 184 catch (Throwable throwable) 185 { 186 m_transaction.rollback(); 187 throw throwable; 188 } 189 finally 190 { 191 DatabaseUtils.closeStatement(insertStatement); 192 insertStatement = null; 193 } 194 195 PreparedStatement updateStatement = null; 197 198 m_transaction.begin(); 199 try 200 { 201 updateStatement = m_connection.prepareStatement(UPDATE_VALUE); 202 updateStatement.setString(1, VALUE_TEST_1); 203 updateStatement.setString(2, VALUE_TEST_2); 204 iUpdateCount = updateStatement.executeUpdate(); 205 206 m_transaction.commit(); 207 208 fail("Updating the unique column with the same value" + 209 " didn't generated exception."); 210 } 211 catch (Throwable throwable) 212 { 213 s_logger.log(Level.FINEST, 214 "Update of unique column with the same value" + 215 " generated exception as expected.", 216 throwable); 217 m_transaction.rollback(); 218 } 220 finally 221 { 222 DatabaseUtils.closeStatement(updateStatement); 223 updateStatement = null; 224 } 225 } 226 finally 227 { 228 PreparedStatement deleteStatement = null; 229 230 m_transaction.begin(); 231 try 232 { 233 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 234 235 DatabaseUtils.executeUpdateAndClose(deleteStatement); 236 237 m_transaction.commit(); 238 } 239 catch (Throwable throwable) 240 { 241 m_transaction.rollback(); 242 throw throwable; 243 } 244 finally 245 { 246 DatabaseUtils.closeStatement(deleteStatement); 247 deleteStatement = null; 248 } 249 } 250 } 251 252 260 public void testRollbackUniqueWithJDBC( 261 ) throws Throwable 262 { 263 final String INSERT_VALUE = "insert into ROLLBACK_TEST (TEST_COLUMN) values (?)"; 264 final String DELETE_ALL = "delete from ROLLBACK_TEST"; 265 final String UPDATE_VALUE 267 = "update ROLLBACK_TEST set TEST_COLUMN = ? where TEST_COLUMN = ?"; 268 final String VALUE_TEST_1 = "value one 1"; 269 final String VALUE_TEST_2 = "value two 2"; 270 271 PreparedStatement insertStatement = null; 272 int iUpdateCount; 273 try 274 { 275 m_transaction.begin(); 276 try 277 { 278 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 279 insertStatement.setString(1, VALUE_TEST_1); 280 281 iUpdateCount = DatabaseUtils.executeUpdateAndClose(insertStatement); 282 283 m_transaction.commit(); 284 } 285 catch (Throwable throwable) 286 { 287 m_transaction.rollback(); 288 throw throwable; 289 } 290 finally 291 { 292 DatabaseUtils.closeStatement(insertStatement); 293 insertStatement = null; 294 } 295 296 assertEquals("Exactly one record have been inserted.", 297 iUpdateCount, 1); 298 299 m_transaction.begin(); 300 try 301 { 302 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 303 insertStatement.setString(1, VALUE_TEST_2); 304 305 iUpdateCount = DatabaseUtils.executeUpdateAndClose(insertStatement); 306 307 m_transaction.commit(); 308 } 309 catch (Throwable throwable) 310 { 311 m_transaction.rollback(); 312 throw throwable; 313 } 314 finally 315 { 316 DatabaseUtils.closeStatement(insertStatement); 317 insertStatement = null; 318 } 319 320 PreparedStatement updateStatement = null; 322 323 boolean bOriginalAutoCommit = m_connection.getAutoCommit(); 326 try 327 { 328 if (bOriginalAutoCommit) 329 { 330 m_connection.setAutoCommit(false); 331 } 332 try 333 { 334 updateStatement = m_connection.prepareStatement(UPDATE_VALUE); 335 updateStatement.setString(1, VALUE_TEST_1); 336 updateStatement.setString(2, VALUE_TEST_2); 337 iUpdateCount = updateStatement.executeUpdate(); 338 339 DatabaseTransactionFactoryImpl.getInstance().commitTransaction(m_connection); 340 341 fail("Updating the unique column with the same value" + 342 " didn't generated exception."); 343 } 344 catch (Throwable throwable) 345 { 346 s_logger.log(Level.FINEST, 347 "Update of unique column with the same value" + 348 " generated exception as expected.", 349 throwable); 350 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction(m_connection); 353 } 355 finally 356 { 357 DatabaseUtils.closeStatement(updateStatement); 358 updateStatement = null; 359 } 360 } 361 finally 362 { 363 if (bOriginalAutoCommit) 364 { 365 m_connection.setAutoCommit(bOriginalAutoCommit); 366 } 367 } 368 } 369 finally 370 { 371 PreparedStatement deleteStatement = null; 372 373 m_transaction.begin(); 374 try 375 { 376 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 377 378 DatabaseUtils.executeUpdateAndClose(deleteStatement); 379 380 m_transaction.commit(); 381 } 382 catch (Throwable throwable) 383 { 384 m_transaction.rollback(); 385 throw throwable; 386 } 387 finally 388 { 389 DatabaseUtils.closeStatement(deleteStatement); 390 deleteStatement = null; 391 } 392 } 393 } 394 395 400 public void testCommitAfterUpdateSelect( 401 ) throws Throwable 402 { 403 final String DELETE_ALL = "delete from TRANSACTION_TEST"; 404 final String UPDATE_VALUE = "update TRANSACTION_TEST set TEST_VALUE = ? where TEST_ID = ?"; 405 final String INSERT_VALUE = "insert into TRANSACTION_TEST values (?, ?)"; 406 final String SELECT_VALUE = "select TEST_VALUE from TRANSACTION_TEST where TEST_ID = 1"; 407 final String VALUE_TEST = "test_value"; 408 final String VALUE_TEST1 = "test_value_updated_1"; 409 410 Connection connection = null; 411 PreparedStatement updateStatement = null; 412 PreparedStatement deleteStatement = null; 413 PreparedStatement selectStatement = null; 414 PreparedStatement insertStatement = null; 415 ResultSet rsResults = null; 416 int iUpdatedCount = 0; 417 int iDeletedCount = 0; 418 int iSelectedCount = 0; 419 int iInsertCount = 0; 420 String strUpdatedValue = ""; 421 422 try 423 { 424 try 427 { 428 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 430 431 m_transaction.begin(); 432 try 433 { 434 deleteStatement = connection.prepareStatement(DELETE_ALL); 435 436 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 437 438 try 439 { 440 insertStatement = connection.prepareStatement(INSERT_VALUE); 441 insertStatement.setInt(1, 1); insertStatement.setString(2, VALUE_TEST); 443 iInsertCount = insertStatement.executeUpdate(); 444 } 445 finally 446 { 447 DatabaseUtils.closeStatement(insertStatement); 448 } 449 450 m_transaction.commit(); 451 } 452 catch (Throwable throwable) 453 { 454 m_transaction.rollback(); 455 throw throwable; 456 } 457 } 458 finally 459 { 460 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 461 } 462 463 assertEquals("No records should be initially in the database.", 464 0, iDeletedCount); 465 assertEquals("Exactly one record should be inserted.", 1, iInsertCount); 466 467 try 470 { 471 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 473 try 474 { 475 selectStatement = connection.prepareStatement(SELECT_VALUE, 476 ResultSet.TYPE_SCROLL_INSENSITIVE, 477 ResultSet.CONCUR_READ_ONLY); 478 rsResults = selectStatement.executeQuery(); 479 if (rsResults.last()) 480 { iSelectedCount = rsResults.getRow(); 482 rsResults.beforeFirst(); 483 } 484 rsResults.beforeFirst(); 485 if (rsResults.next()) 486 { 487 strUpdatedValue = rsResults.getString(1); 488 } 489 } 490 finally 491 { 492 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 493 } 494 } 495 finally 496 { 497 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 498 } 499 assertEquals("Exactly one record have been selected.", 1, iSelectedCount); 500 assertEquals("Selected items should not have postfix '_updated_1'.", 501 VALUE_TEST, strUpdatedValue); 502 503 m_transaction.begin(); 505 try 506 { 507 try { 510 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 512 try 513 { 514 updateStatement = connection.prepareStatement(UPDATE_VALUE); 515 updateStatement.setString(1, VALUE_TEST1); 516 updateStatement.setInt(2, 1); 517 iUpdatedCount = DatabaseUtils.executeUpdateAndClose(updateStatement); 518 } 519 finally 520 { 521 DatabaseUtils.closeStatement(updateStatement); 522 } 523 } 524 finally 525 { 526 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 527 } 528 assertEquals("Exactly one record should have been updated.", 1, iUpdatedCount); 529 try { 532 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 534 try 535 { 536 selectStatement = connection.prepareStatement( 541 SELECT_VALUE, 542 ResultSet.TYPE_SCROLL_INSENSITIVE, 543 ResultSet.CONCUR_READ_ONLY); 544 rsResults = selectStatement.executeQuery(); 545 if (rsResults.last()) 546 { iSelectedCount = rsResults.getRow(); 548 rsResults.beforeFirst(); 549 } 550 rsResults.first(); 551 strUpdatedValue = rsResults.getString(1); 552 } 553 finally 554 { 555 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 556 } 557 } 558 finally 559 { 560 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 561 } 562 assertEquals("Exactly one record should have been selected.", 1, iSelectedCount); 563 assertEquals("Selected items should have updated value.", 564 VALUE_TEST1, strUpdatedValue); 565 m_transaction.commit(); 566 } 567 catch (Throwable throwable) 568 { 569 m_transaction.rollback(); 570 throw throwable; 571 } 572 573 try { 577 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 579 try 580 { 581 selectStatement = connection.prepareStatement(SELECT_VALUE, 582 ResultSet.TYPE_SCROLL_INSENSITIVE, 583 ResultSet.CONCUR_READ_ONLY); 584 rsResults = selectStatement.executeQuery(); 585 if (rsResults.last()) 586 { iSelectedCount = rsResults.getRow(); 588 rsResults.beforeFirst(); 589 } 590 rsResults.first(); 591 strUpdatedValue = rsResults.getString(1); 592 } 593 finally 594 { 595 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 596 } 597 } 598 finally 599 { 600 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 601 } 602 assertEquals("Exactly one record should have been selected.", 605 1, iSelectedCount); 606 assertEquals("Selected items should have updated value.", 607 VALUE_TEST1, strUpdatedValue); 608 609 } 610 finally 611 { 612 m_transaction.begin(); 613 try 614 { 615 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 616 617 DatabaseUtils.executeUpdateAndClose(deleteStatement); 618 619 m_transaction.commit(); 620 } 621 catch (Throwable throwable) 622 { 623 m_transaction.rollback(); 624 throw throwable; 625 } 626 } 627 } 628 629 639 public void testCommitAfterUpdateDelete( 640 ) throws Throwable 641 { 642 final String DELETE_ALL = "delete from TRANSACTION_TEST"; 643 final String UPDATE_VALUE = "update TRANSACTION_TEST set TEST_VALUE = ? where TEST_ID = ?"; 644 final String INSERT_VALUE = "insert into TRANSACTION_TEST values (?, ?)"; 645 final String SELECT_VALUE = "select TEST_VALUE from TRANSACTION_TEST where TEST_ID = 1"; 646 final String VALUE_TEST = "test_value"; 647 final String VALUE_TEST1 = "test_value_updated_1"; 648 649 Connection connection = null; 650 PreparedStatement updateStatement = null; 651 PreparedStatement deleteStatement = null; 652 PreparedStatement selectStatement = null; 653 PreparedStatement insertStatement = null; 654 ResultSet rsResults = null; 655 int iUpdatedCount = 0; 656 int iDeletedCount = 0; 657 int iSelectedCount = 0; 658 int iInsertCount = 0; 659 660 try 661 { 662 try 665 { 666 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 672 673 m_transaction.begin(); 674 try 675 { 676 deleteStatement = connection.prepareStatement(DELETE_ALL); 677 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 678 try 679 { 680 insertStatement = connection.prepareStatement(INSERT_VALUE); 681 insertStatement.setInt(1, 1); insertStatement.setString(2, VALUE_TEST1); 683 iInsertCount = insertStatement.executeUpdate(); 684 } 685 finally 686 { 687 DatabaseUtils.closeStatement(insertStatement); 688 } 689 690 m_transaction.commit(); 691 } 692 catch (Throwable throwable) 693 { 694 m_transaction.rollback(); 695 throw throwable; 696 } 697 } 698 finally 699 { 700 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 701 connection = null; 702 } 703 704 assertEquals("No records should be initially in the database.", 705 0, iDeletedCount); 706 assertEquals("Exactly one record should be inserted.", 1, iInsertCount); 707 708 m_transaction.begin(); 709 try 710 { 711 try { 713 717 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 719 try 720 { 721 updateStatement = connection.prepareStatement(UPDATE_VALUE); 722 updateStatement.setString(1, VALUE_TEST); 723 updateStatement.setInt(2, 1); 724 iUpdatedCount = DatabaseUtils.executeUpdateAndClose(updateStatement); 725 } 726 finally 727 { 728 DatabaseUtils.closeStatement(updateStatement); 729 } 730 } 731 finally 732 { 733 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 734 connection = null; 735 } 736 assertEquals("Exactly one record should have been updated.", 1, iUpdatedCount); 737 m_transaction.commit(); 738 } 739 catch (Throwable throwable) 740 { 741 m_transaction.rollback(); 742 throw throwable; 743 } 744 } 745 catch (Throwable throwable) 746 { 747 s_logger.log(Level.SEVERE, 750 "Unexpected error has occured during test.", 751 throwable); 752 } 753 finally 754 { 755 m_transaction.begin(); 756 try 757 { 758 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 762 763 DatabaseUtils.executeUpdateAndClose(deleteStatement); 764 765 m_transaction.commit(); 766 } 767 catch (Throwable throwable) 768 { 769 m_transaction.rollback(); 770 throw throwable; 771 } 772 773 iSelectedCount = 0; 776 try 777 { 778 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 780 try 781 { 782 selectStatement = connection.prepareStatement(SELECT_VALUE); 783 rsResults = selectStatement.executeQuery(); 788 if (rsResults.next()) 789 { 790 iSelectedCount = 1; 791 } 792 } 793 finally 794 { 795 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 796 } 797 } 798 finally 799 { 800 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 801 } 802 assertEquals("No record should exists in the database but there is a record", 803 0, iSelectedCount); 804 805 iSelectedCount = 0; 808 try 809 { 810 selectStatement = m_connection.prepareStatement(SELECT_VALUE); 811 rsResults = selectStatement.executeQuery(); 812 if (rsResults.next()) 813 { 814 iSelectedCount = 1; 815 } 816 } 817 finally 818 { 819 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 820 } 821 assertEquals("No record should exists in the database but there is a record", 822 0, iSelectedCount); 823 } 824 } 825 826 833 public void testCommitAfterUpdateDeleteWithAutoCommit( 834 ) throws Throwable 835 { 836 final String DELETE_ALL = "delete from TRANSACTION_TEST"; 837 final String UPDATE_VALUE = "update TRANSACTION_TEST set TEST_VALUE = ? where TEST_ID = ?"; 838 final String INSERT_VALUE = "insert into TRANSACTION_TEST values (?, ?)"; 839 final String SELECT_VALUE = "select TEST_VALUE from TRANSACTION_TEST where TEST_ID = 1"; 840 final String VALUE_TEST = "test_value"; 841 final String VALUE_TEST1 = "test_value_updated_1"; 842 843 Connection connection = null; 844 PreparedStatement updateStatement = null; 845 PreparedStatement deleteStatement = null; 846 PreparedStatement selectStatement = null; 847 PreparedStatement insertStatement = null; 848 ResultSet rsResults = null; 849 int iUpdatedCount = 0; 850 int iDeletedCount = 0; 851 int iSelectedCount = 0; 852 int iInsertCount = 0; 853 String strUpdatedValue = ""; 854 855 try 856 { 857 try 860 { 861 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 869 870 m_transaction.begin(); 872 try 873 { 874 deleteStatement = connection.prepareStatement(DELETE_ALL); 877 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 878 879 m_transaction.commit(); 881 } 882 catch (Throwable throwable) 883 { 884 m_transaction.rollback(); 885 throw throwable; 886 } 887 888 connection.setAutoCommit(true); 894 try 895 { 896 insertStatement = connection.prepareStatement(INSERT_VALUE); 897 insertStatement.setInt(1, 1); insertStatement.setString(2, VALUE_TEST1); 899 iInsertCount = insertStatement.executeUpdate(); 900 } 901 finally 902 { 903 DatabaseUtils.closeStatement(insertStatement); 904 } 905 } 906 finally 907 { 908 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 909 connection = null; 910 } 911 912 assertEquals("No records should be initially in the database.", 913 0, iDeletedCount); 914 915 assertEquals("Exactly one record should be inserted.", 1, iInsertCount); 916 917 m_transaction.begin(); 918 try 919 { 920 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 926 try 927 { 928 updateStatement = connection.prepareStatement(UPDATE_VALUE); 929 updateStatement.setString(1, VALUE_TEST1); 930 updateStatement.setInt(2, 1); 931 iUpdatedCount = DatabaseUtils.executeUpdateAndClose(updateStatement); 932 } 933 finally 934 { 935 DatabaseUtils.closeStatement(updateStatement); 936 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 937 connection = null; 938 } 939 m_transaction.commit(); 940 } 941 catch (Throwable throwable) 942 { 943 m_transaction.rollback(); 944 throw throwable; 945 } 946 947 assertEquals("Exactly one record should have been updated.", 948 1, iUpdatedCount); 949 950 iSelectedCount = 0; 954 strUpdatedValue = ""; 955 try 956 { 957 m_connection.setAutoCommit(true); 959 selectStatement = m_connection.prepareStatement(SELECT_VALUE, 960 ResultSet.TYPE_SCROLL_INSENSITIVE, 961 ResultSet.CONCUR_READ_ONLY); 962 rsResults = selectStatement.executeQuery(); 963 if (rsResults.last()) 964 { iSelectedCount = rsResults.getRow(); 966 rsResults.beforeFirst(); 967 } 968 rsResults.first(); 969 strUpdatedValue = rsResults.getString(1); 970 } 971 finally 972 { 973 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 974 selectStatement = null; 975 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 976 connection = null; 977 } 978 assertEquals("Exactly one record should have been selected.", 979 1, iSelectedCount); 980 assertEquals("Selected items should have updated value.", 981 VALUE_TEST1, strUpdatedValue); 982 983 m_connection.setAutoCommit(true); 985 try 986 { 987 updateStatement = m_connection.prepareStatement(UPDATE_VALUE); 991 updateStatement.setString(1, VALUE_TEST); 992 updateStatement.setInt(2, 1); 993 iUpdatedCount = DatabaseUtils.executeUpdateAndClose(updateStatement); 994 } 995 finally 996 { 997 DatabaseUtils.closeStatement(updateStatement); 998 } 999 1000 iSelectedCount = 0; 1005 strUpdatedValue = ""; 1006 try 1007 { 1008 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 1010 selectStatement = connection.prepareStatement(SELECT_VALUE, 1011 ResultSet.TYPE_SCROLL_INSENSITIVE, 1012 ResultSet.CONCUR_READ_ONLY); 1013 rsResults = selectStatement.executeQuery(); 1014 if (rsResults.last()) 1015 { iSelectedCount = rsResults.getRow(); 1017 rsResults.beforeFirst(); 1018 } 1019 rsResults.first(); 1020 strUpdatedValue = rsResults.getString(1); 1021 } 1022 finally 1023 { 1024 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 1025 selectStatement = null; 1026 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 1027 connection = null; 1028 } 1029 assertEquals("Exactly one record should have been selected.", 1030 1, iSelectedCount); 1031 assertEquals("Selected items should have updated value.", 1032 VALUE_TEST, strUpdatedValue); 1033 } 1034 catch (Throwable throwable) 1035 { 1036 s_logger.log(Level.SEVERE, 1039 "Unexpected error has occured during test.", 1040 throwable); 1041 } 1042 finally 1043 { 1044 try 1045 { 1046 connection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(false); 1050 1051 m_transaction.begin(); 1053 try 1054 { 1055 deleteStatement = connection.prepareStatement(DELETE_ALL); 1056 DatabaseUtils.executeUpdateAndClose(deleteStatement); 1057 1058 m_transaction.commit(); 1059 } 1060 catch (Throwable throwable2) 1061 { 1062 m_transaction.rollback(); 1063 throw throwable2; 1064 } 1065 1066 iSelectedCount = 0; 1069 m_connection.setAutoCommit(true); 1071 try 1072 { 1073 selectStatement = m_connection.prepareStatement(SELECT_VALUE); 1074 rsResults = selectStatement.executeQuery(); 1075 if (rsResults.next()) 1076 { 1077 iSelectedCount = 1; 1078 } 1079 } 1080 finally 1081 { 1082 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 1083 selectStatement = null; 1084 } 1085 assertEquals("No record should exists in the database but there is a record", 1086 0, iSelectedCount); 1087 1088 iSelectedCount = 0; 1091 try 1092 { 1093 connection.setAutoCommit(true); 1094 selectStatement = connection.prepareStatement(SELECT_VALUE); 1095 rsResults = selectStatement.executeQuery(); 1096 if (rsResults.next()) 1097 { 1098 iSelectedCount = 1; 1099 } 1100 } 1101 finally 1102 { 1103 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 1104 selectStatement = null; 1105 } 1106 assertEquals("No record should exists in the database but there is a record", 1107 0, iSelectedCount); 1108 } 1109 finally 1110 { 1111 DatabaseConnectionFactoryImpl.getInstance().returnConnection(connection); 1112 connection = null; 1113 } 1114 } 1115 } 1116 1117 1124 public void testEmptyCommit( 1125 ) throws Throwable 1126 { 1127 DatabaseConnectionFactoryImpl.getInstance().returnConnection(m_connection); 1134 m_connection = null; 1135 m_iRequestedConnectionCount--; 1136 DatabaseTransactionFactoryImpl.getInstance().reset(); 1137 1138 m_transaction.begin(); 1139 try 1140 { 1141 m_transaction.commit(); 1143 } 1144 catch (Throwable throwable) 1145 { 1146 m_transaction.rollback(); 1147 throw throwable; 1148 } 1149 m_transaction.begin(); 1151 try 1152 { 1153 m_transaction.commit(); 1155 } 1156 catch (Throwable throwable) 1157 { 1158 m_transaction.rollback(); 1159 throw throwable; 1160 } 1161 } 1162 1163 1170 public void testEmptyRollback( 1171 ) throws Throwable 1172 { 1173 DatabaseConnectionFactoryImpl.getInstance().returnConnection(m_connection); 1180 m_connection = null; 1181 m_iRequestedConnectionCount--; 1182 DatabaseTransactionFactoryImpl.getInstance().reset(); 1183 1184 m_transaction.begin(); 1185 m_transaction.rollback(); 1187 m_transaction.begin(); 1189 m_transaction.rollback(); 1191 } 1192 } 1193} 1194 | Popular Tags |