1 5 import java.io.StringWriter ; 6 import java.io.Writer ; 7 import java.sql.Connection ; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.sql.Statement ; 12 import java.sql.Timestamp ; 13 import java.util.Date ; 14 import java.util.Properties ; 15 import java.util.Random ; 16 17 import javax.sql.XAConnection ; 18 import javax.transaction.HeuristicMixedException ; 19 import javax.transaction.HeuristicRollbackException ; 20 import javax.transaction.InvalidTransactionException ; 21 import javax.transaction.NotSupportedException ; 22 import javax.transaction.RollbackException ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.Transaction ; 25 import javax.transaction.TransactionManager ; 26 import javax.transaction.UserTransaction ; 27 28 import org.apache.log4j.Logger; 29 import org.apache.log4j.Priority; 30 import org.enhydra.jdbc.pool.StandardXAPoolDataSource; 31 import org.enhydra.jdbc.standard.StandardXADataSource; 32 import org.objectweb.jotm.Jotm; 33 import org.objectweb.transaction.jta.TMService; 34 35 public class JotmXaPoolTest { 36 37 protected static Logger logger = Logger.getLogger("jotm.test"); 38 39 protected String createColumns = null; 40 protected String tableName = null; 41 protected int counter = 0; 42 protected int loops = 500; 43 protected TMService jotm = null; 44 protected StandardXAPoolDataSource pool = null; 45 46 public JotmXaPoolTest() throws Exception { 47 jotm = new Jotm(true, false); 49 logger.info("Started JOTM..."); 50 51 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 53 54 String dsConfigName = System.getProperty("jdbc.config.file"); 56 57 if (dsConfigName == null || dsConfigName.length() == 0) { 59 Properties dsProps = new Properties (); 61 dsProps.load(cl.getResourceAsStream("config.properties")); 62 dsConfigName = dsProps.getProperty("jdbc.config.file"); 63 tableName = dsProps.getProperty("table.name", "jotm_xapool_test"); 64 String loopAmt = dsProps.getProperty("loop.amount", "500"); 65 try { 66 loops = Integer.parseInt(loopAmt); 67 } catch (NumberFormatException e) { 68 loops = 500; 69 } 70 } 71 72 if (dsConfigName == null || dsConfigName.length() == 0) { 74 throw new IllegalStateException ("No datasource properties configured; set the jdbc.config.file variable in the environment or in datasource.properties"); 75 } 76 77 Properties props = new Properties (); 79 props.load(cl.getResourceAsStream(dsConfigName)); 80 81 createColumns = props.getProperty("sql.create.columns"); 83 84 String defaultWrapper = "org.enhydra.jdbc.standard.StandardXADataSource"; 86 String wrapperName = props.getProperty("jdbc.wrapper", defaultWrapper); 87 88 Class c = cl.loadClass(wrapperName); 89 Object o = c.newInstance(); 90 91 StandardXADataSource ds = (StandardXADataSource) o; 93 ds.setTransactionManager(jotm.getTransactionManager()); 94 95 ds.setDriverName(props.getProperty("jdbc.driver")); 96 ds.setUrl(props.getProperty("jdbc.url")); 97 ds.setUser(props.getProperty("jdbc.login")); 98 ds.setPassword(props.getProperty("jdbc.password", "")); 99 ds.setDescription(props.getProperty("jdbc.description", "NA")); 100 101 int isoLevel = 2; 103 try { 104 isoLevel = Integer.parseInt(props.getProperty("jdbc.isolation.level", "2")); 105 } catch (NumberFormatException e) { 106 logger.error("Problems parsing the isolation level (should be a number) using READ_COMMITED"); 107 isoLevel = 2; 108 } 109 ds.setTransactionIsolation(isoLevel); 110 111 pool = new StandardXAPoolDataSource(); 113 pool.setDataSource(ds); 114 pool.setDescription(ds.getDescription()); 115 pool.setUser(ds.getUser()); 116 pool.setPassword(ds.getPassword()); 117 pool.setTransactionManager(jotm.getTransactionManager()); 118 119 logger.info("Created pool..."); 120 } 121 122 public void dropTest() throws SQLException { 123 Connection con = pool.getConnection(); 124 Statement s = con.createStatement(); 125 s.executeUpdate("DROP TABLE " + tableName); 126 s.close(); 127 con.close(); 128 logger.info("Table '" + tableName + "' dropped."); 129 } 130 131 public void createTest() throws SQLException { 132 Connection con = pool.getConnection(); 134 logger.info("Isolation level : " + con.getTransactionIsolation()); 135 136 String sql = "CREATE TABLE " + tableName + " " + this.createColumns; 137 Statement s = con.createStatement(); 138 s.executeUpdate(sql); 139 s.close(); 140 con.close(); 141 logger.info("Table '" + tableName + "' created."); 142 } 143 144 145 public void insertTest() { 146 UserTransaction trans = jotm.getUserTransaction(); 148 logger.info("Beginning multiple insert with unique transactions/connections..."); 149 150 for (int i = 0; i < loops; i++) { 151 try { 152 trans.begin(); 153 } catch (NotSupportedException e1) { 154 logger.error("Exception", e1); 155 } catch (SystemException e1) { 156 logger.error("Exception", e1); 157 } 158 159 Connection con = null; 160 try { 161 con = pool.getConnection(); 162 } catch (SQLException e) { 163 logger.error("Problems getting new connection - Test Failed!", e); 164 return; 165 } 166 if (con == null) { 167 logger.error("Pool returned null connection with no exception - Test Failed!"); 168 return; 169 } 170 171 logger.debug("[A] Looping.. inserting #" + i); 172 try { 173 String sql1 = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 175 PreparedStatement ps1 = con.prepareStatement(sql1); 176 ps1.setInt(1, ++counter); 177 ps1.setString(2, "insTest" + i); 178 ps1.setString(3, "Insert Test"); 179 ps1.setTimestamp(4, new Timestamp (new Date ().getTime())); 180 ps1.executeUpdate(); 181 ps1.close(); 182 String sql2 = "SELECT * FROM " + tableName + " WHERE idx_1 = ?"; 184 PreparedStatement ps2 = con.prepareStatement(sql2); 185 ps2.setInt(1, counter); 186 ResultSet res = ps2.executeQuery(); 187 if (res == null || !res.next()) { 188 logger.error("Could not get inserted item back from select!"); 189 } else { 190 logger.debug(res.getString(1) + " : " + res.getString(2) + "[" + res.getString(3) + "] - " + res.getString(4)); 191 } 192 res.close(); 193 ps2.close(); 194 } catch (Exception e) { 195 logger.error("Exception", e); 196 try { 197 trans.setRollbackOnly(); 198 } catch (IllegalStateException e2) { 199 logger.error("Exception", e2); 200 } catch (SystemException e2) { 201 logger.error("Exception", e2); 202 } 203 } finally { 204 try { 206 con.close(); 207 } catch (SQLException e2) { 208 logger.error("Exception", e2); 209 } 210 211 try { 213 trans.commit(); 214 } catch (Exception e) { 215 logger.error("Exception", e); 216 } 217 } 218 } 219 } 220 221 222 public void connectionTest() { 223 UserTransaction trans = jotm.getUserTransaction(); 225 try { 226 trans.begin(); 227 } catch (NotSupportedException e1) { 228 logger.error("Exception", e1); 229 } catch (SystemException e1) { 230 logger.error("Exception", e1); 231 } 232 233 logger.info("Beginning multiple insert/connection single transaction..."); 234 for (int i = 0; i < loops; i++) { 235 Connection con = null; 236 try { 237 con = pool.getConnection(); 238 } catch (SQLException e) { 239 logger.error("Problems getting new connection - Test Failed!", e); 240 return; 241 } 242 if (con == null) { 243 logger.error("Pool returned null connection with no exception - Test Failed!"); 244 return; 245 } 246 247 logger.debug("Got connection.. inserting #" + i); 248 try { 249 String sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 250 PreparedStatement ps = con.prepareStatement(sql); 251 ps.setInt(1, ++counter); 252 ps.setString(2, "conTest" + i); 253 ps.setString(3, "Connection Test"); 254 ps.setTimestamp(4, new Timestamp (new Date ().getTime())); 255 ps.executeUpdate(); 256 ps.close(); 257 } catch (Exception e) { 258 e.printStackTrace(); 259 try { 260 trans.setRollbackOnly(); 261 } catch (IllegalStateException e2) { 262 logger.error("Exception", e2); 263 } catch (SystemException e2) { 264 logger.error("Exception", e2); 265 } 266 } finally { 267 try { 268 con.close(); 269 } catch (SQLException e2) { 270 logger.error("Exception", e2); 271 } 272 } 273 } 274 try { 275 trans.commit(); 276 } catch (Exception e) { 277 logger.error("Exception", e); 278 } 279 } 280 281 282 public void loopTest() { 283 UserTransaction trans = jotm.getUserTransaction(); 285 try { 286 trans.begin(); 287 } catch (NotSupportedException e1) { 288 logger.error("Exception", e1); 289 } catch (SystemException e1) { 290 logger.error("Exception", e1); 291 } 292 293 logger.info("Beginning multiple insert single transaction/connection..."); 294 295 Connection con = null; 296 try { 297 con = pool.getConnection(); 298 } catch (SQLException e) { 299 logger.error("Problems getting new connection - Test Failed!", e); 300 return; 301 } 302 if (con == null) { 303 logger.error("Pool returned null connection with no exception - Test Failed!"); 304 return; 305 } 306 307 logger.debug("Got connection.. "); 308 309 for (int i = 0; i < loops; i++) { 310 logger.debug("[B] Looping.. inserting #" + i); 311 try { 312 String sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 313 PreparedStatement ps = con.prepareStatement(sql); 314 ps.setInt(1, ++counter); 315 ps.setString(2, "loopTest" + i); 316 ps.setString(3, "Loop Test"); 317 ps.setTimestamp(4, new Timestamp (new Date ().getTime())); 318 ps.executeUpdate(); 319 ps.close(); 320 } catch (Exception e) { 321 logger.error("Exception", e); 322 try { 323 trans.setRollbackOnly(); 324 } catch (IllegalStateException e2) { 325 logger.error("Exception", e2); 326 } catch (SystemException e2) { 327 logger.error("Exception", e2); 328 } 329 } 330 } 331 332 try { 334 con.close(); 335 } catch (SQLException e2) { 336 logger.error("Exception", e2); 337 } 338 logger.debug("Closed connection.."); 339 340 try { 342 trans.commit(); 343 } catch (Exception e) { 344 logger.error("Exception", e); 345 } 346 } 347 348 349 public void suspendTest() { 350 UserTransaction trans = jotm.getUserTransaction(); 352 TransactionManager tm = jotm.getTransactionManager(); 353 354 try { 356 trans.setTransactionTimeout(300); 357 } catch (SystemException e) { 358 logger.error("Exception", e); 359 return; 360 } 361 362 try { 364 trans.begin(); 365 } catch (NotSupportedException e1) { 366 logger.error("Exception", e1); 367 } catch (SystemException e1) { 368 logger.error("Exception", e1); 369 } 370 371 logger.info("Beginning multiple insert/connection on suspend main transaction..."); 372 for (int i = 0; i < loops; i++) { 373 Transaction transaction = null; 375 try { 376 transaction = tm.suspend(); 377 } catch (SystemException e2) { 378 logger.error("Exception", e2); 379 } 380 logger.debug("Suspended #" + i); 381 382 try { 384 trans.begin(); 385 } catch (NotSupportedException e3) { 386 logger.error("Exception", e3); 387 } catch (SystemException e3) { 388 logger.error("Exception", e3); 389 } 390 logger.debug("Began new transaction."); 391 392 Connection con1 = null; 394 try { 395 con1 = pool.getConnection(); 396 } catch (SQLException e) { 397 logger.error("Problems getting new (sub) connection - Test Failed!", e); 398 return; 399 } 400 if (con1 == null) { 401 logger.error("Pool returned null connection with no exception - Test Failed!"); 402 return; 403 } 404 logger.debug("Got connection."); 405 406 try { 407 String sql1 = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 409 PreparedStatement ps1 = con1.prepareStatement(sql1); 410 ps1.setInt(1, ++counter); 411 ps1.setString(2, "susTest" + i); 412 ps1.setString(3, "Suspend Test - Main Suspended"); 413 ps1.setTimestamp(4, new Timestamp (new Date ().getTime())); 414 ps1.executeUpdate(); 415 ps1.close(); 416 } catch (Exception e) { 417 logger.error("Exception", e); 418 try { 419 trans.setRollbackOnly(); 420 } catch (IllegalStateException e4) { 421 logger.error("Exception", e4); 422 } catch (SystemException e4) { 423 logger.error("Exception", e4); 424 } 425 } finally { 426 try { 427 con1.close(); 428 } catch (SQLException e5) { 429 logger.error("Exception", e5); 430 } 431 432 try { 434 trans.commit(); 435 } catch (SecurityException e4) { 436 logger.error("Exception", e4); 437 } catch (IllegalStateException e4) { 438 logger.error("Exception", e4); 439 } catch (RollbackException e4) { 440 logger.error("Exception", e4); 441 } catch (HeuristicMixedException e4) { 442 logger.error("Exception", e4); 443 } catch (HeuristicRollbackException e4) { 444 logger.error("Exception", e4); 445 } catch (SystemException e4) { 446 logger.error("Exception", e4); 447 } 448 } 449 logger.debug("Inserted record."); 450 451 try { 453 tm.resume(transaction); 454 } catch (InvalidTransactionException e4) { 455 logger.error("Exception", e4); 456 } catch (IllegalStateException e4) { 457 logger.error("Exception", e4); 458 } catch (SystemException e4) { 459 logger.error("Exception", e4); 460 } 461 logger.debug("Resumed #" + i); 462 463 Connection con2 = null; 465 try { 466 con2 = pool.getConnection(); 467 } catch (SQLException e) { 468 logger.error("Problems getting new (main) connection - Test Failed!", e); 469 return; 470 } 471 if (con2 == null) { 472 logger.error("Pool returned null connection with no exception - Test Failed!"); 473 return; 474 } 475 476 try { 477 String sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 478 PreparedStatement ps = con2.prepareStatement(sql, 0, 0); 479 ps.setInt(1, ++counter); 480 ps.setString(2, "susTest" + i); 481 ps.setString(3, "Suspend Test - Main"); 482 ps.setTimestamp(4, new Timestamp (new Date ().getTime())); 483 ps.executeUpdate(); 484 ps.close(); 485 logger.debug("Inserted main transaction."); 486 } catch (Exception e) { 487 logger.error("Exception", e); 488 try { 489 trans.setRollbackOnly(); 490 } catch (IllegalStateException e5) { 491 logger.error("Exception", e5); 492 } catch (SystemException e5) { 493 logger.error("Exception", e5); 494 } 495 } finally { 496 try { 497 con2.close(); 498 } catch (SQLException e5) { 499 logger.error("Exception", e5); 500 } 501 } 502 503 Connection con3 = null; 505 try { 506 con3 = pool.getConnection(); 507 } catch (SQLException e) { 508 logger.error("Problems getting new (main select) connection - Test Failed!", e); 509 return; 510 } 511 if (con3 == null) { 512 logger.error("Pool returned null connection with no exception - Test Failed!"); 513 return; 514 } 515 516 try { 518 String sql = "SELECT * FROM " + tableName + " WHERE idx_1 = ?"; 519 PreparedStatement ps = con3.prepareStatement(sql); 520 ps.setInt(1, counter); 521 ResultSet res = ps.executeQuery(); 522 if (res == null || !res.next()) { 523 logger.error("Could not get inserted item back from select!"); 524 } else { 525 logger.debug(res.getString(1) + " : " + res.getString(2) + "[" + res.getString(3) + "] - " + res.getString(4)); 526 } 527 res.close(); 528 ps.close(); 529 } catch (Exception e) { 530 logger.error("Exception", e); 531 } finally { 532 try { 533 con3.close(); 534 } catch (SQLException e2) { 535 logger.error("Exception", e2); 536 } 537 } 538 } 539 540 try { 542 trans.commit(); 543 } catch (SecurityException e) { 544 logger.error("Exception", e); 545 } catch (IllegalStateException e) { 546 logger.error("Exception", e); 547 } catch (RollbackException e) { 548 logger.error("Exception", e); 549 } catch (HeuristicMixedException e) { 550 logger.error("Exception", e); 551 } catch (HeuristicRollbackException e) { 552 logger.error("Exception", e); 553 } catch (SystemException e) { 554 logger.error("Exception", e); 555 } 556 } 557 558 public void transactionTest() { 559 UserTransaction trans = jotm.getUserTransaction(); 560 Connection con = null; 561 String sql = null; 562 PreparedStatement ps = null; 563 564 logger.info("Beginning transaction test..."); 565 566 try { 568 con = pool.getConnection(); 569 sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 570 ps = con.prepareStatement(sql); 571 ps.setInt(1, ++counter); 572 ps.setString(2, "transTest"); 573 ps.setString(3, "Transaction Test - This Should Be Updated!"); 574 ps.setTimestamp(4, new Timestamp (new Date ().getTime())); 575 ps.executeUpdate(); 576 ps.close(); 577 con.close(); 578 } catch (SQLException e) { 579 logger.error("Transaction test failed on first insert", e); 580 return; 581 } 582 583 try { 585 con = pool.getConnection(); 586 sql = "SELECT * FROM " + tableName + " WHERE idx_1 = ?"; 587 ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 588 ps.setInt(1, counter); 589 ResultSet res = ps.executeQuery(); 590 if (!res.next()) { 591 logger.error("Transaction test failed; no results returned from first insert"); 592 return; 593 } else { 594 logger.debug(res.getString(1) + " : " + res.getString(2) + "[" + res.getString(3) + "] - " + res.getString(4)); 595 } 596 res.close(); 597 ps.close(); 598 con.close(); 599 } catch (SQLException e) { 600 logger.error("Transaction test failed; cannot select first insert", e); 601 return; 602 } 603 604 try { 606 trans.begin(); 607 } catch (NotSupportedException e1) { 608 logger.error("Exception", e1); 609 return; 610 } catch (SystemException e1) { 611 logger.error("Exception", e1); 612 return; 613 } 614 615 try { 617 con = pool.getConnection(); 618 sql = "UPDATE " + tableName + " SET col_3 = ? WHERE idx_1 = ?"; 619 ps = con.prepareStatement(sql); 620 ps.setString(1, "Transaction Test - First Update; One To Go!"); 621 ps.setInt(2, counter); 622 ps.executeUpdate(); 623 ps.close(); 624 con.close(); 625 } catch (SQLException e) { 626 logger.error("Transaction test failed; cannot do first update", e); 627 return; 628 } 629 630 try { 632 con = pool.getConnection(); 633 sql = "SELECT * FROM " + tableName + " WHERE idx_1 = ?"; 634 ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 635 ps.setInt(1, counter); 636 ResultSet res = ps.executeQuery(); 637 if (!res.next()) { 638 logger.error("Transaction test failed; no results returned after first update"); 639 return; 640 } else { 641 logger.debug(res.getString(1) + " : " + res.getString(2) + "[" + res.getString(3) + "] - " + res.getString(4)); 642 } 643 res.close(); 644 ps.close(); 645 con.close(); 646 } catch (SQLException e) { 647 logger.error("Transaction test failed; cannot select first update", e); 648 return; 649 } 650 651 try { 653 trans.commit(); 654 } catch (SystemException e) { 655 logger.error("Exception", e); 656 return; 657 } catch (SecurityException e) { 658 logger.error("Exception", e); 659 return; 660 } catch (IllegalStateException e) { 661 logger.error("Exception", e); 662 return; 663 } catch (RollbackException re) { 664 logger.error("Transaction rolledback!", re); 665 return; 666 } catch (HeuristicMixedException e) { 667 logger.error("Exception", e); 668 return; 669 } catch (HeuristicRollbackException e) { 670 logger.error("Exception", e); 671 return; 672 } 673 674 try { 676 con = pool.getConnection(); 677 sql = "SELECT * FROM " + tableName + " WHERE idx_1 = ?"; 678 ps = con.prepareStatement(sql); 679 ps.setInt(1, counter); 680 ResultSet res = ps.executeQuery(); 681 if (!res.next()) { 682 logger.error("Transaction test failed; no results returned from select after commit"); 683 return; 684 } 685 res.close(); 686 ps.close(); 687 con.close(); 688 } catch (SQLException e) { 689 logger.error("Transaction test failed; cannot select after commit", e); 690 return; 691 } 692 693 try { 695 trans.begin(); 696 } catch (NotSupportedException e1) { 697 logger.error("Exception", e1); 698 return; 699 } catch (SystemException e1) { 700 logger.error("Exception", e1); 701 return; 702 } 703 704 try { 706 con = pool.getConnection(); 707 sql = "UPDATE " + tableName + " SET col_3 = ? WHERE idx_1 = ?"; 708 ps = con.prepareStatement(sql); 709 ps.setString(1, "Transaction Test - Second Update!"); 710 ps.setInt(2, counter); 711 ps.executeUpdate(); 712 ps.close(); 713 con.close(); 714 } catch (SQLException e) { 715 logger.error("Transaction test failed; cannot do second update", e); 716 return; 717 } 718 719 try { 721 trans.commit(); 722 } catch (SystemException e) { 723 logger.error("Exception", e); 724 return; 725 } catch (SecurityException e) { 726 logger.error("Exception", e); 727 return; 728 } catch (IllegalStateException e) { 729 logger.error("Exception", e); 730 return; 731 } catch (RollbackException re) { 732 logger.error("Transaction rolledback!", re); 733 return; 734 } catch (HeuristicMixedException e) { 735 logger.error("Exception", e); 736 return; 737 } catch (HeuristicRollbackException e) { 738 logger.error("Exception", e); 739 return; 740 } 741 } 742 743 public void rollbackOnlyTest() { 744 UserTransaction trans = jotm.getUserTransaction(); 746 try { 747 trans.begin(); 748 } catch (NotSupportedException e1) { 749 logger.error("Exception", e1); 750 return; 751 } catch (SystemException e1) { 752 logger.error("Exception", e1); 753 return; 754 } 755 756 logger.info("Beginning rollback test..."); 757 Random rand = new Random (); 758 int randomInt = rand.nextInt(9); 759 760 for (int i = 0; i < 10; i++) { 761 Connection con = null; 762 try { 763 con = pool.getConnection(); 764 } catch (SQLException e) { 765 logger.error("Problems getting connection - rolling back now.", e); 766 try { 767 trans.rollback(); 768 } catch (IllegalStateException e2) { 769 logger.error("Exception", e2); 770 } catch (SecurityException e2) { 771 logger.error("Exception", e2); 772 } catch (SystemException e2) { 773 logger.error("Exception", e2); 774 } 775 } 776 if (con == null) { 777 logger.error("Pool returned a null connection w/ no exception! Test Failed!"); 778 return; 779 } 780 781 logger.debug("Got connection.. inserting #" + i); 782 try { 783 String sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?)"; 784 PreparedStatement ps = con.prepareStatement(sql); 785 ps.setInt(1, ++counter); 786 ps.setString(2, "rollTest" + i); 787 ps.setString(3, "Rollback Test - This should not show in selectTest!"); 788 ps.setTimestamp(4, new Timestamp (new Date ().getTime())); 789 ps.executeUpdate(); 790 ps.close(); 791 } catch (Exception e) { 792 logger.error("Exception", e); 793 try { 794 trans.setRollbackOnly(); 795 } catch (IllegalStateException e2) { 796 logger.error("Exception", e2); 797 } catch (SystemException e2) { 798 logger.error("Exception", e2); 799 } 800 } finally { 801 try { 802 con.close(); 803 } catch (SQLException e2) { 804 logger.error("Exception", e2); 805 } 806 } 807 808 if (randomInt == i) { 810 logger.info("Setting rollback only on pass #" + i); 811 try { 812 trans.setRollbackOnly(); 813 } catch (IllegalStateException e2) { 814 logger.error("Exception", e2); 815 } catch (SystemException e2) { 816 logger.error("Exception", e2); 817 } 818 } 819 820 } 821 822 try { 823 trans.commit(); 824 } catch (SystemException e) { 825 logger.error("Commit failed; RollbackException not thrown!", e); 826 } catch (SecurityException e) { 827 logger.error("Commit failed; RollbackException not thrown!", e); 828 } catch (IllegalStateException e) { 829 logger.error("Commit failed; RollbackException not thrown!", e); 830 } catch (RollbackException re) { 831 logger.info("Commit failed (good), transaction rolled back by commit()."); 833 } catch (HeuristicMixedException e) { 834 logger.error("Commit failed; RollbackException not thrown!", e); 835 } catch (HeuristicRollbackException e) { 836 logger.error("Commit failed; RollbackException not thrown!", e); 837 } 838 } 839 840 public void timeoutTest() { 841 UserTransaction trans = jotm.getUserTransaction(); 843 844 logger.info("Beginning timeout test..."); 845 Random rand = new Random (); 846 int randomInt = rand.nextInt(60); 847 randomInt++; 848 849 logger.info("Setting timeout to: " + randomInt); 850 try { 851 trans.setTransactionTimeout(randomInt); } catch (SystemException e) { 853 logger.error("Exception", e); 854 } 855 856 try { 858 trans.begin(); 859 } catch (NotSupportedException e1) { 860 logger.error("Exception", e1); 861 } catch (SystemException e1) { 862 logger.error("Exception", e1); 863 } 864 logger.info("Began transaction; now waiting..."); 865 866 long wait = new Date ().getTime() + ((randomInt + 2) * 1000); 868 long now = 0; 869 while ((now = new Date ().getTime()) < wait) { 870 } 872 873 try { 875 trans.commit(); 876 logger.info("Transaction commited; shouldn't have happened!"); 877 } catch (SecurityException e) { 878 logger.error("Exception", e); 879 } catch (IllegalStateException e) { 880 logger.error("Exception", e); 881 } catch (RollbackException e) { 882 logger.info("RollBackException caught! Good! The transaction was rolled back."); 883 } catch (HeuristicMixedException e) { 884 logger.error("Exception", e); 885 } catch (HeuristicRollbackException e) { 886 logger.error("Exception", e); 887 } catch (SystemException e) { 888 logger.error("Exception", e); 889 } 890 } 891 892 public void selectTest() throws SQLException { 893 logger.info("Beginning select test.. We should have exactly " + (loops * 5) + " records."); 894 Connection con = pool.getConnection(); 895 Statement s = con.createStatement(); 896 ResultSet res = s.executeQuery("SELECT * FROM " + tableName + " ORDER BY idx_1"); 897 int rowCount = 0; 898 while (res.next()) { 899 rowCount++; 900 logger.debug(res.getString(1) + " : " + res.getString(2) + "[" + res.getString(3) + "] - " + res.getString(4)); 901 } 902 res.close(); 903 con.close(); 904 logger.info("Total Rows: " + rowCount + " of " + (loops * 5)); 905 if (rowCount == (loops * 5)) logger.info("Looks good..."); 906 } 907 908 public void close() { 909 pool.shutdown(true); 910 jotm.stop(); 911 logger.info("Shutdown pool and jotm."); 912 } 913 914 public void runPreTests() { 915 try { 917 dropTest(); 918 } catch (SQLException e) { 919 } 921 922 try { 924 createTest(); 925 } catch (SQLException e) { 926 logger.error("Fatal SQL Error", e); 927 this.close(); 928 System.exit(-1); 929 } 930 } 931 932 public void runPostTests() { 933 try { 935 selectTest(); 936 } catch (SQLException e) { 937 logger.error("SQL Error", e); 938 } 939 940 try { 942 dropTest(); 943 } catch (SQLException e) { 944 logger.error("Fatal SQL Error", e); 945 this.close(); 946 System.exit(-1); 947 } 948 } 949 950 public void runAllTests() { 951 runPreTests(); 952 953 insertTest(); 955 956 connectionTest(); 958 959 loopTest(); 961 962 transactionTest(); 964 965 suspendTest(); 967 968 rollbackOnlyTest(); 970 971 timeoutTest(); 973 974 runPostTests(); 975 976 } 977 978 public static void main(String [] args) throws Exception { 979 JotmXaPoolTest test = new JotmXaPoolTest(); 980 test.runAllTests(); 981 test.close(); 982 System.exit(1); 983 } 984 } | Popular Tags |