1 2 import junit.framework.Assert; 3 4 import java.sql.PreparedStatement ; 5 import java.util.Map ; 6 import java.util.List ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 11 import java.sql.Connection ; 12 import java.sql.ResultSet ; 13 14 import org.enhydra.jdbc.standard.StandardXAConnectionHandle; 15 16 22 public class XAPoolTestSuite extends XAPoolTestCase { 23 public XAPoolTestSuite(String strTestName) { 24 super(strTestName); 25 } 26 27 public void testSimpleStatementCommit() throws Exception { 28 int newValue = 54; 29 String completion = "commit"; 30 System.out.println("SimpleStatementCommit: begin, getConnection, executeUpdate, VERIFY, closeAll, commit, getConnection, VERIFY, close"); 31 try { 32 utx.begin(); 33 34 conn = spds.getConnection(login, password); 35 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 36 pstmt.setInt(1, newValue); 37 pstmt.executeUpdate(); 38 Assert.assertTrue("executeUpdate with "+newValue+" does not work", getValue() == 54); 39 pstmt.close(); 40 conn.close(); 41 42 utx.commit(); 43 44 } catch (Exception e) { 45 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 46 System.err.println("Exception message :" + e.getMessage()); 47 e.printStackTrace(); 48 throw e; 49 } 50 51 conn = spds.getConnection(login, password); 52 Assert.assertTrue("after commit with "+newValue+" does not work", getValue() == 54); 53 conn.close(); 54 } 55 56 public void testSimpleStatementRollback() throws Exception { 57 int newValue = 12; 58 String completion = "rollback"; 59 System.out.println("SimpleStatementRollback: begin, getConnection, executeUpdate, VERIFY, closeAll, rollback, getConnection, VERIFY, close"); 60 try { 61 utx.begin(); 62 63 conn = spds.getConnection(login, password); 64 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 65 pstmt.setInt(1, newValue); 66 pstmt.executeUpdate(); 67 Assert.assertTrue("executeUpdate with "+newValue+" does not work", getValue() == 12); 68 pstmt.close(); 69 conn.close(); 70 71 utx.rollback(); 72 73 } catch (Exception e) { 74 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 75 System.err.println("Exception message :" + e.getMessage()); 76 e.printStackTrace(); 77 throw e; 78 } 79 80 conn = spds.getConnection(login, password); 81 Assert.assertTrue("after rollback with "+newValue+" does not work", getValue() == 54); 82 conn.close(); 83 } 84 85 public void testSaveAutoCommitTrue() throws Exception { 86 int newValue = 12; 87 System.out.println("SaveAutoCommitTrue: getConnection, setAutoCommit(true), begin, executeUpdate, rollback, VERIFY, closeAll"); 88 try { 89 conn = spds.getConnection(login, password); 90 boolean autocom = true; 91 conn.setAutoCommit(autocom); 92 93 utx.begin(); 94 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 95 pstmt.setInt(1, newValue); 96 pstmt.executeUpdate(); 97 98 utx.rollback(); 99 Assert.assertTrue("Saveautocommit with TRUE does not work", conn.getAutoCommit() == autocom); 100 pstmt.close(); 101 conn.close(); 102 103 } catch (Exception e) { 104 System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 105 System.out.println("Exception message :" + e.getMessage()); 106 e.printStackTrace(); 107 throw e; 108 } 109 } 110 111 public void testSaveAutoCommitFalse() throws Exception { 112 int newValue = 12; 113 System.out.println("SaveAutoCommitFalse: getConnection, setAutoCommit(false), begin, executeUpdate, rollback, VERIFY, closeAll"); 114 try { 115 conn = spds.getConnection(login, password); 116 boolean autocom = false; 117 conn.setAutoCommit(autocom); 118 119 utx.begin(); 120 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 121 pstmt.setInt(1, newValue); 122 pstmt.executeUpdate(); 123 utx.rollback(); 124 125 Assert.assertTrue("Saveautocommit with FALSE does not work", conn.getAutoCommit() == autocom); 126 pstmt.close(); 127 conn.close(); 128 129 } catch (Exception e) { 130 System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 131 System.out.println("Exception message :" + e.getMessage()); 132 e.printStackTrace(); 133 throw e; 134 } 135 } 136 137 138 public void testMultipleConnectionCommit() throws Exception { 139 int newValue = 66; 140 System.out.println("MultipleConnectionCommit: begin, getConnection, executeUpdate, VERIFY, closeAll, getConnection, executeUpdate, VERIFY, closeAll, commit, getConnection, VERIFY, close"); 141 try { 142 utx.begin(); 143 144 conn = spds.getConnection(login, password); 145 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 146 pstmt.setInt(1, newValue); 147 pstmt.executeUpdate(); 148 Assert.assertTrue("executeUpdate with "+(newValue)+" does not work", getValue() == 66); 149 pstmt.close(); 150 conn.close(); 151 152 conn = spds.getConnection(login, password); 153 PreparedStatement pstmt2 = conn.prepareStatement(SQL_QUERY); 154 pstmt2.setInt(1, newValue+2); 155 pstmt2.executeUpdate(); 156 Assert.assertTrue("executeUpdate with "+(newValue+2)+" does not work", getValue() == 68); 157 pstmt2.close(); 158 conn.close(); 159 160 utx.commit(); 161 162 } catch (Exception e) { 163 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 164 System.err.println("Exception message :" + e.getMessage()); 165 e.printStackTrace(); 166 throw e; 167 } 168 169 conn = spds.getConnection(login, password); 170 Assert.assertTrue("after commit with "+(newValue+2)+" does not work", getValue() == 68); 171 conn.close(); 172 } 173 174 175 public void testMultipleConnectionRollback() throws Exception { 176 int newValue = 900; 177 System.out.println("MultipleConnectionCommit: begin, getConnection, executeUpdate, VERIFY, closeAll, getConnection, executeUpdate, VERIFY, closeAll, rollback, getConnection, VERIFY, close"); 178 try { 179 utx.begin(); 180 181 conn = spds.getConnection(login, password); 182 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 183 pstmt.setInt(1, newValue); 184 pstmt.executeUpdate(); 185 Assert.assertTrue("executeUpdate with "+(newValue)+" does not work", getValue() == 900); 186 pstmt.close(); 187 conn.close(); 188 189 conn = spds.getConnection(login, password); 190 PreparedStatement pstmt2 = conn.prepareStatement(SQL_QUERY); 191 pstmt2.setInt(1, newValue+2); 192 pstmt2.executeUpdate(); 193 Assert.assertTrue("executeUpdate with "+(newValue+2)+" does not work", getValue() == 902); 194 pstmt2.close(); 195 conn.close(); 196 197 utx.rollback(); 199 } catch (Exception e) { 200 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 201 System.err.println("Exception message :" + e.getMessage()); 202 e.printStackTrace(); 203 throw e; 204 } 205 206 conn = spds.getConnection(login, password); 207 Assert.assertTrue("after rollback with "+(newValue+2)+" does not work", getValue() == 68); 208 conn.close(); 209 } 210 211 public void testMultipleTransaction() throws Exception { 212 System.out.println("MultipleTransaction: getConnection, begin, executeUpdate, VERIFY, commit, VERIFY, begin, executeUpdate, VERIFY, rollback, VERIFY, close"); 213 try { 214 conn = spds.getConnection(login, password); 215 } catch(Exception e) { 216 System.out.println("problem"); 217 if (conn == null) { 218 System.out.println("the connection is null"); 219 } 221 else { 222 System.out.println("the connection is not null"); 223 } 225 } 226 int newValue = 44; 227 try { 228 utx.begin(); 229 PreparedStatement pstmt0 = conn.prepareStatement(SQL_QUERY); 230 pstmt0.setInt(1, 13); 231 pstmt0.executeUpdate(); 232 pstmt0.close(); 233 Assert.assertTrue("executeUpdate with 13 does not work", getValue() == 13); 234 utx.commit(); 235 236 Assert.assertTrue("after commit with 13 does not work", getValue() == 13); 237 238 utx.begin(); 239 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 240 pstmt.setInt(1, newValue); 241 pstmt.executeUpdate(); 242 pstmt.close(); 243 Assert.assertTrue("executeUpdate with "+newValue+" does not work", getValue() == 44); 244 245 utx.rollback(); 246 247 Assert.assertTrue("after rollback with "+newValue+" does not work", getValue() == 13); 248 249 } catch (Exception e) { 250 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 251 System.err.println("Exception message :" + e.getMessage()); 252 e.printStackTrace(); 253 throw e; 254 } 255 256 conn.close(); 257 258 } 259 260 public void testPotentialDeadLock() throws Exception { 261 System.out.println("PotentialDeadLock: begin, getConnection, executeUpdate, VERIFY, pclose, VERIFY, close, VERIFY, commit, VERIFY"); 262 int newValue = 44; 263 try { 264 utx.begin(); 265 conn = spds.getConnection(login, password); 266 PreparedStatement pstmt0 = conn.prepareStatement(SQL_QUERY); 267 pstmt0.setInt(1, 133); 268 pstmt0.executeUpdate(); 269 Assert.assertTrue("executeUpdate with 133 does not work", getValue() == 133); 270 pstmt0.close(); 271 Assert.assertTrue("after stmt.close: executeUpdate with 133 does not work", getValue() == 133); 272 conn.close(); 273 Assert.assertTrue("after con.close: executeUpdate with 133 does not work (value is "+getValue()+") ", getValue() == 133); 274 utx.commit(); 275 Assert.assertTrue("after commit with 133 does not work", getValue() == 133); 276 } catch (Exception e) { 277 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 278 System.err.println("Exception message :" + e.getMessage()); 279 e.printStackTrace(); 280 throw e; 281 } 282 } 283 284 public void testMiroRequestConnectionCountInTransaction() throws Exception { 285 StandardXAConnectionHandle tempCon = null; 286 StringBuffer sbBuffer = new StringBuffer (); 287 String strCon; 288 PreparedStatement insertStatement = null;; 289 Map mpReturnPatternMap = new HashMap (); 290 Map mpConnections = new HashMap (); 291 Map mpUsedConnections = new HashMap (); 292 List lstConnections = new ArrayList (); 293 int iTable1Index = 0; 294 int iInsertCount; 295 System.out.println("RequestConnectionCountInTransaction: Request multiple connections in one transactions over and over and see if they will be returned in the same order."); 296 try { 297 try { 298 utx.begin(); 299 300 for (int iIndex = 0; iIndex < 10; iIndex++) { 301 try { 302 sbBuffer.delete(0, sbBuffer.length()); 303 for (int iCon = 0; iCon < 4; iCon++) { 304 conn = spds.getConnection(); 305 Assert.assertTrue("XAPool no longer returns instance" + 306 " of StandardXAConnectionHandle", 307 conn instanceof StandardXAConnectionHandle); 308 tempCon = (StandardXAConnectionHandle)conn; 309 strCon = tempCon.con.toString(); 310 sbBuffer.append(strCon); 311 sbBuffer.append("::"); 312 lstConnections.add(conn); 313 mpConnections.put(strCon, strCon); 314 try { 315 insertStatement = conn.prepareStatement(SQL_QUERY); 316 insertStatement.setInt(1, iTable1Index++); 317 iInsertCount = insertStatement.executeUpdate(); 318 Assert.assertEquals("One record should have been inserted.", 319 1, iInsertCount); 320 } 321 finally { 322 insertStatement.close(); 323 insertStatement = null; 324 } 325 tempCon = (StandardXAConnectionHandle)conn; 326 strCon = tempCon.con.toString(); 327 mpUsedConnections.put(strCon, strCon); 328 } 329 mpReturnPatternMap.put(sbBuffer.toString(), sbBuffer.toString()); 330 } 331 finally { 332 for (int iCon = 0; iCon < 4; iCon++) { 333 ((Connection )lstConnections.remove(0)).close(); 334 } 335 } 336 } 337 345 Assert.assertEquals("More than expected number of connections were used.", 4, mpUsedConnections.size()); 346 Assert.assertEquals("More than expected number of connections were allocated.", 4, mpConnections.size()); 347 utx.commit(); 348 } 349 catch (Throwable throwable) { 350 utx.rollback(); 351 } 352 353 } finally { 354 371 } 372 } 373 374 375 376 377 378 379 public void testMiroRequestConnectionCountOutOfTransaction() throws Exception { 380 Connection con = null; 381 StandardXAConnectionHandle tempCon; 382 String strCon; 383 int iInsertCount; 384 PreparedStatement insertStatement = null;; 385 Map mpReturnPatternMap = new HashMap (); 386 Map mpConnections = new HashMap (); 387 Map mpUsedConnections = new HashMap (); 388 List lstConnections = new ArrayList (); 389 int iTable1Index = 0; 390 int iIndex; 391 int iCon; 392 StringBuffer sbBuffer = new StringBuffer (); 393 System.out.println("RequestConnectionCountOutOfTransaction: Request multiple connections outside of transactions over and over and see if they will be returned in the same order."); 394 try { 395 396 for (iIndex = 0; iIndex < 10; iIndex++) { 397 try { 398 sbBuffer.delete(0, sbBuffer.length()); 399 for (iCon = 0; iCon < 4; iCon++) { 400 conn = spds.getConnection(); 401 Assert.assertTrue("XAPool no longer returns instance" + 402 " of StandardXAConnectionHandle", 403 conn instanceof StandardXAConnectionHandle); 404 tempCon = (StandardXAConnectionHandle)conn; 405 strCon = tempCon.con.toString(); 406 sbBuffer.append(strCon); 407 sbBuffer.append("::"); 408 lstConnections.add(conn); 409 mpConnections.put(strCon, strCon); 410 try { 411 insertStatement = conn.prepareStatement(SQL_QUERY); 412 insertStatement.setInt(1, iTable1Index++); 413 iInsertCount = insertStatement.executeUpdate(); 414 Assert.assertEquals("One record should have been inserted.", 415 1, iInsertCount); 416 } finally { 417 insertStatement.close(); 418 insertStatement = null; 419 } 420 tempCon = (StandardXAConnectionHandle)conn; 421 strCon = tempCon.con.toString(); 422 mpUsedConnections.put(strCon, strCon); 423 424 } 427 mpReturnPatternMap.put(sbBuffer.toString(), sbBuffer.toString()); 428 } finally { 429 for (iCon = 0; iCon < 4; iCon++) { 430 try { 431 conn = (Connection )lstConnections.remove(0); 432 433 } catch (Throwable thr) { 434 } finally { 435 conn.close(); 436 con = null; 437 } 438 } 439 } 440 } 441 455 Assert.assertEquals("More than expected number of connections were used.", 456 4, mpUsedConnections.size()); 457 Assert.assertEquals("More than expected number of connections were allocated.", 458 4, mpConnections.size()); 459 } finally { 462 480 } 481 } 482 483 484 485 public void testMiroRepeatedRequestConnectionInTransaction() throws Throwable { 486 System.out.println("RepeatedRequestConnectionInTransaction: Request one connection over and over in one transactions and see if the same connection will be returned."); 487 try { 488 try { 490 StandardXAConnectionHandle tempCon; 491 String strCon; 492 int iInsertCount; 493 PreparedStatement insertStatement = null;; 494 int iTable1Index = 0; 495 int iIndex; 496 int iConChange = 0; 497 String strLastCon = null; 498 Map mpConnections = new HashMap (); 499 500 utx.begin(); 501 for (iIndex = 0; iIndex < 10; iIndex++) { 502 try { 503 conn = spds.getConnection(); 504 Assert.assertTrue("XAPool no longer returns instance" + 505 " of StandardXAConnectionHandle", 506 conn instanceof StandardXAConnectionHandle); 507 tempCon = (StandardXAConnectionHandle)conn; 508 strCon = tempCon.con.toString(); 509 if (strLastCon == null) { 510 strLastCon = strCon; 511 } else { 512 if (!strCon.equals(strLastCon)) { 513 strLastCon = strCon; 516 iConChange++; 517 } 518 } 519 mpConnections.put(strCon, strCon); 520 521 } 523 finally { 524 conn.close(); 525 conn = null; 526 } 527 } 528 540 Assert.assertEquals("More than expected number of connections were allocated.", 541 1, mpConnections.size()); 542 543 utx.commit(); 544 } catch (Throwable throwable) { 545 utx.rollback(); 546 throw throwable; 547 } 548 } finally { 549 567 } 568 } 569 570 571 572 public void testMiroRepeatedRequestConnectionWithSelectInTransaction() throws Throwable { 573 System.out.println("RepeatedRequestConnectionWithSelectInTransaction: Request one connection over and over in one transactions and do select and then see if the same connection will be returned and used"); 574 try { 575 try { 577 StandardXAConnectionHandle tempCon; 578 String strCon; 579 PreparedStatement selectStatement = null; 580 int iTable1Index = 0; 581 int iIndex; 582 int iConChange = 0; 583 String strLastCon = null; 584 ResultSet rsResults = null; 585 Map mpConnections = new HashMap (); 586 Map mpUsedConnections = new HashMap (); 587 588 utx.begin(); 589 for (iIndex = 0; iIndex < 10; iIndex++) { 590 try { 591 conn = spds.getConnection(); 592 Assert.assertTrue("XAPool no longer returns instance" + 593 " of StandardXAConnectionHandle", 594 conn instanceof StandardXAConnectionHandle); 595 tempCon = (StandardXAConnectionHandle)conn; 596 strCon = tempCon.con.toString(); 597 if (strLastCon == null) { 598 strLastCon = strCon; 599 } else { 600 if (!strCon.equals(strLastCon)) { 601 strLastCon = strCon; 604 iConChange++; 605 } 606 } 607 mpConnections.put(strCon, strCon); 608 609 try { 611 selectStatement = conn.prepareStatement(SQL_REQUEST); 612 rsResults = selectStatement.executeQuery(); 613 rsResults.close(); 614 } finally { 615 616 selectStatement.close(); 617 rsResults = null; 618 selectStatement = null; 619 } 620 621 tempCon = (StandardXAConnectionHandle)conn; 622 strCon = tempCon.con.toString(); 623 mpUsedConnections.put(strCon, strCon); 624 } finally { 625 conn.close(); 626 conn = null; 627 } 628 } 629 648 Assert.assertEquals("More than expected number of connections were used" + 649 " when selecting.", 1, mpUsedConnections.size()); 650 655 utx.commit(); 656 } catch (Throwable throwable) { 657 utx.rollback(); 658 throw throwable; 659 } 660 } finally { 661 685 } 686 } 687 688 689 public void testMiroRepeatedRequestConnectionWithSelectOutOfTransaction() throws Throwable { 690 System.out.println("RepeatedRequestConnectionWithSelectOutOfTransaction: Request one connection over and over without transactions and do select and then see if the same connection will be returned and used"); 691 try { 692 StandardXAConnectionHandle tempCon; 693 String strCon; 694 PreparedStatement selectStatement = null;; 695 int iTable1Index = 0; 696 int iIndex; 697 int iConChange = 0; 698 String strLastCon = null; 699 ResultSet rsResults = null; 700 Map mpConnections = new HashMap (); 701 Map mpUsedConnections = new HashMap (); 702 703 for (iIndex = 0; iIndex < 10; iIndex++) { 704 try { 705 conn = spds.getConnection(); 706 Assert.assertTrue("XAPool no longer returns instance" + 707 " of StandardXAConnectionHandle", 708 conn instanceof StandardXAConnectionHandle); 709 tempCon = (StandardXAConnectionHandle)conn; 710 strCon = tempCon.con.toString(); 711 if (strLastCon == null) { 712 strLastCon = strCon; 713 } else { 714 if (!strCon.equals(strLastCon)) { 715 strLastCon = strCon; 718 iConChange++; 719 } 720 } 721 mpConnections.put(strCon, strCon); 722 723 try { 725 selectStatement = conn.prepareStatement(SQL_REQUEST); 726 rsResults = selectStatement.executeQuery(); 727 rsResults.close(); 728 } finally { 729 730 selectStatement.close(); 731 732 rsResults = null; 733 selectStatement = null; 734 } 735 736 tempCon = (StandardXAConnectionHandle)conn; 737 strCon = tempCon.con.toString(); 738 mpUsedConnections.put(strCon, strCon); 739 } finally { 740 conn.close(); 741 conn = null; 742 } 743 } 744 763 Assert.assertEquals("More than expected number of connections were used" + 764 " when selecting.", 1, mpUsedConnections.size()); 765 Assert.assertEquals("More than expected number of connections were allocated" + 766 " when selecting.", 1, mpConnections.size()); 767 } finally { 768 792 } 793 } 794 795 796 public void testMiroRepeatedRequestConnectionWithInsertInTransaction() throws Throwable { 797 System.out.println("RepeatedRequestConnectionWithInsertInTransaction: Request one connection over and over in one transactions and do insert and then see if the same connection will be returned and used"); 798 try { 799 try { 800 StandardXAConnectionHandle tempCon; 801 String strCon; 802 int iInsertCount; 803 PreparedStatement insertStatement = null;; 804 int iTable1Index = 0; 805 int iIndex; 806 int iConChange = 0; 807 String strLastCon = null; 808 Map mpConnections = new HashMap (); 809 Map mpUsedConnections = new HashMap (); 810 811 utx.begin(); 812 for (iIndex = 0; iIndex < 10; iIndex++) { 813 try { 814 conn = spds.getConnection(); 815 Assert.assertTrue("XAPool no longer returns instance" + 816 " of StandardXAConnectionHandle", 817 conn instanceof StandardXAConnectionHandle); 818 tempCon = (StandardXAConnectionHandle)conn; 819 strCon = tempCon.con.toString(); 820 if (strLastCon == null) { 821 strLastCon = strCon; 822 } else { 823 if (!strCon.equals(strLastCon)) { 824 strLastCon = strCon; 827 iConChange++; 828 } 829 } 830 mpConnections.put(strCon, strCon); 831 832 try { 834 insertStatement = conn.prepareStatement(SQL_QUERY); 835 insertStatement.setInt(1, iTable1Index++); 836 iInsertCount = insertStatement.executeUpdate(); 837 Assert.assertEquals("One record should have been inserted.", 838 1, iInsertCount); 839 } finally { 840 insertStatement.close(); 841 insertStatement = null; 842 } 843 844 tempCon = (StandardXAConnectionHandle)conn; 845 strCon = tempCon.con.toString(); 846 mpUsedConnections.put(strCon, strCon); 847 } finally { 848 conn.close(); 849 conn = null; 850 } 851 } 852 871 Assert.assertEquals("More than expected number of connections were used" + 872 " when inserting.", 1, mpUsedConnections.size()); 873 878 utx.commit(); 879 } catch (Throwable throwable) { 880 utx.rollback(); 881 throw throwable; 882 } 883 } finally { 884 908 } 909 } 910 911 public void testMiroInsertSelectInTransaction() throws Throwable { 912 System.out.println("InsertSelectInTransaction: We try to simulate situation, when xapool returns different connections for first and second request inside of the same transaction and see if it affects functionality."); 913 try { 914 Map mpConnections = new HashMap (); 915 Map mpUsedConnections = new HashMap (); 916 int iConChange = 0; 917 918 try { 919 StandardXAConnectionHandle tempCon; 920 String strCon; 921 int iInsertCount; 922 PreparedStatement insertStatement = null; 923 PreparedStatement selectStatement = null; 924 ResultSet rsResults = null; 925 int iTable1Index = 0; 926 int iIndex; 927 String strLastCon = null; 928 929 utx.begin(); 930 931 try { 932 conn = spds.getConnection(); 933 Assert.assertTrue("XAPool no longer returns instance" + 934 " of StandardXAConnectionHandle", 935 conn instanceof StandardXAConnectionHandle); 936 tempCon = (StandardXAConnectionHandle)conn; 937 strCon = tempCon.con.toString(); 938 if (strLastCon == null) { 939 strLastCon = strCon; 940 } else { 941 if (!strCon.equals(strLastCon)) { 942 strLastCon = strCon; 945 iConChange++; 946 } 947 } 948 mpConnections.put(strCon, strCon); 949 950 try { 952 insertStatement = conn.prepareStatement(SQL_QUERY); 953 insertStatement.setInt(1, iTable1Index++); 954 iInsertCount = insertStatement.executeUpdate(); 955 Assert.assertEquals("One record should have been inserted.", 956 1, iInsertCount); 957 } finally { 958 insertStatement.close(); 959 insertStatement = null; 960 } 961 962 tempCon = (StandardXAConnectionHandle)conn; 963 strCon = tempCon.con.toString(); 964 mpUsedConnections.put(strCon, strCon); 965 } finally { 966 conn.close(); 967 conn = null; 968 } 969 970 try { 971 conn = spds.getConnection(); 972 Assert.assertTrue("XAPool no longer returns instance" + 973 " of StandardXAConnectionHandle", 974 conn instanceof StandardXAConnectionHandle); 975 tempCon = (StandardXAConnectionHandle)conn; 976 strCon = tempCon.con.toString(); 977 if (strLastCon == null) { 978 strLastCon = strCon; 979 } else { 980 if (!strCon.equals(strLastCon)) { 981 strLastCon = strCon; 984 iConChange++; 985 } 986 } 987 mpConnections.put(strCon, strCon); 988 989 try { 991 selectStatement = conn.prepareStatement(SQL_REQUEST); 992 rsResults = selectStatement.executeQuery(); 993 Assert.assertTrue("Select didn't find inserted record in the database.", 994 rsResults.next()); 995 } finally { 996 rsResults.close(); 997 selectStatement.close(); 998 rsResults = null; 999 selectStatement = null; 1000 } 1001 1002 tempCon = (StandardXAConnectionHandle)conn; 1003 strCon = tempCon.con.toString(); 1004 mpUsedConnections.put(strCon, strCon); 1005 } finally { 1006 conn.close(); 1007 conn = null; 1008 } 1009 1010 utx.commit(); 1011 } catch (Throwable throwable) { 1012 utx.rollback(); 1013 throw throwable; 1014 } 1015 1034 Assert.assertEquals("More than expected number of connections were used" + 1035 " when insert/select.", 1, mpUsedConnections.size()); 1036 } finally { 1041 1065 } 1066 } 1067 1068 1069 public void testMiroEmptyRequestReturnConnection() throws Throwable { 1070 System.out.println("EmptyRequestReturnConnection: This test triggers bug in out modified XAPool 1.3.1 which doesn't occur with latest the original XAPool or the latest XAPool."); 1071 1072 Connection con1 = null; 1073 Connection con2 = null; 1074 StandardXAConnectionHandle tempCon; 1075 String strCon1; 1076 String strCon2; 1077 int iInsertCount; 1078 1079 PreparedStatement insertStatement = null;; 1080 PreparedStatement selectStatement = null;; 1081 ResultSet rsResults = null; 1082 1083 try { 1084 try { 1085 utx.begin(); 1086 final int VALUE_TEST_1 = 0; 1087 Map mpConnectionMap = new HashMap (); 1088 1089 try { 1091 con1 = spds.getConnection(); 1092 Assert.assertTrue("XAPool no longer returns instance of StandardXAConnectionHandle", 1093 con1 instanceof StandardXAConnectionHandle); 1094 tempCon = (StandardXAConnectionHandle)con1; 1095 strCon1 = tempCon.con.toString(); 1096 mpConnectionMap.put(strCon1, new Integer (0)); 1097 1098 insertStatement = con1.prepareStatement(SQL_QUERY); 1100 insertStatement.setInt(1, VALUE_TEST_1); 1101 iInsertCount = insertStatement.executeUpdate(); 1102 Assert.assertEquals("One record should have been inserted.", 1, iInsertCount); 1103 } finally { 1104 insertStatement.close(); 1105 insertStatement = null; 1106 con1.close(); 1107 con1 = null; 1108 } 1109 1110 boolean bDetectedTheSame = false; 1111 int iIndex; 1112 1113 for (iIndex = 1; iIndex < 100; iIndex++) { 1124 try { 1125 con2 = spds.getConnection(); 1126 Assert.assertTrue("XAPool no longer returns instance" 1127 + " of StandardXAConnectionHandle", 1128 con2 instanceof StandardXAConnectionHandle); 1129 tempCon = (StandardXAConnectionHandle)con2; 1130 strCon2 = tempCon.con.toString(); 1131 1132 if (mpConnectionMap.get(strCon2) == null) { 1133 1136 insertStatement = con2.prepareStatement(SQL_QUERY); 1138 insertStatement.setInt(1, iIndex); 1139 iInsertCount = insertStatement.executeUpdate(); 1140 Assert.assertEquals("One record should have been inserted.", 1, iInsertCount); 1141 } else { 1142 iIndex = ((Integer )mpConnectionMap.get(strCon2)).intValue(); 1144 bDetectedTheSame = true; 1145 break; 1151 } 1152 mpConnectionMap.put(strCon2, new Integer (iIndex)); 1153 } finally { 1154 if (insertStatement != null) 1155 insertStatement.close(); 1156 insertStatement = null; 1157 con2.close(); 1158 con2 = null; 1159 } 1160 } 1161 1162 Assert.assertTrue("Unable to get the same connection from the pool.", 1163 bDetectedTheSame); 1164 1165 try { 1170 con1 = spds.getConnection(); 1171 selectStatement = con1.prepareStatement(SQL_QUERY); 1172 selectStatement.setInt(1, iIndex); 1173 try { 1174 rsResults = selectStatement.executeQuery(); 1175 Assert.assertTrue("Canot find inserted record with index " + iIndex, 1176 rsResults.next()); 1177 rsResults.close(); 1178 1179 } catch (Exception e) { 1180 } 1182 } finally { 1183 1184 selectStatement.close(); 1185 selectStatement = null; 1186 con1.close(); 1187 con1 = null; 1188 } 1189 1190 utx.commit(); 1191 } catch (Throwable throwable) { 1192 utx.rollback(); 1193 throw throwable; 1194 } 1195 } finally { 1196 utx.begin(); 1197 try { 1198 conn = spds.getConnection(); 1199 1204 conn.close(); 1205 utx.commit(); 1206 } catch (Throwable throwable) { 1207 utx.rollback(); 1208 throw throwable; 1209 } 1210 } 1211 } 1212 1213} 1214 | Popular Tags |