1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.io.Serializable ; 25 import java.sql.CallableStatement ; 26 import java.sql.Connection ; 27 import java.sql.DriverManager ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 import java.util.Properties ; 35 36 import javax.sql.ConnectionEvent ; 37 import javax.sql.ConnectionEventListener ; 38 import javax.sql.ConnectionPoolDataSource ; 39 import javax.sql.DataSource ; 40 import javax.sql.PooledConnection ; 41 import javax.sql.XAConnection ; 42 import javax.sql.XADataSource ; 43 import javax.transaction.xa.XAException ; 44 import javax.transaction.xa.XAResource ; 45 import javax.transaction.xa.Xid ; 46 47 import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource; 48 import org.apache.derby.jdbc.EmbeddedDataSource; 49 import org.apache.derby.jdbc.EmbeddedXADataSource; 50 import org.apache.derby.jdbc.ClientConnectionPoolDataSource; 51 import org.apache.derby.jdbc.ClientDataSource; 52 import org.apache.derby.jdbc.ClientXADataSource; 53 54 import org.apache.derby.tools.JDBCDisplayUtil; 55 import org.apache.derby.tools.ij; 56 import org.apache.derbyTesting.functionTests.util.SecurityCheck; 57 import org.apache.derbyTesting.functionTests.util.TestUtil; 58 import org.apache.oro.text.perl.Perl5Util; 59 60 70 public class checkDataSource 71 { 72 private boolean testConnectionToString = TestUtil.isEmbeddedFramework(); 77 78 private boolean testSimpleDataSource = TestUtil.isEmbeddedFramework(); 81 82 88 protected static Hashtable conns = new Hashtable (); 89 90 94 protected static Perl5Util p5u = new Perl5Util(); 95 96 100 private static final String CONNSTRING_FORMAT = "\\S+@[0-9]+ " + 101 "\\(XID = .*\\), \\(SESSIONID = [0-9]+\\), " + 102 "\\(DATABASE = [A-Za-z]+\\), \\(DRDAID = .+\\)"; 103 104 109 private final Object nogc = SecurityCheck.class; 110 111 112 113 public static void main(String [] args) throws Exception { 114 115 try 116 { 117 new checkDataSource().runTest(args); 118 119 SecurityCheck.report(); 122 123 } 124 catch ( Exception e ) 125 { 126 e.printStackTrace(); 127 throw e; 128 } 129 System.out.println("Completed checkDataSource"); 130 131 } 132 133 public checkDataSource() { 134 } 135 136 protected void runTest(String [] args) throws Exception { 137 138 ij.getPropertyArg(args); 140 Connection dmc = ij.startJBMS(); 141 142 dmc.createStatement().executeUpdate("create table y(i int)"); 143 144 dmc.createStatement().executeUpdate( 145 "create procedure checkConn2(in dsname varchar(20)) " + 146 "parameter style java language java modifies SQL DATA " + 147 "external name 'org.apache.derbyTesting.functionTests.tests.jdbcapi." + 148 this.getNestedMethodName() + 149 "'"); 150 CallableStatement cs = dmc.prepareCall("call checkConn2(?)"); 151 cs.setString(1,"Nested"); 152 cs.execute(); 153 154 155 checkConnection("DriverManager ", dmc); 156 if (testConnectionToString) 157 checkJBMSToString(); 158 159 160 Properties attrs = new Properties (); 161 attrs.setProperty("databaseName", "wombat"); 162 DataSource dscs = TestUtil.getDataSource(attrs); 163 164 if (testConnectionToString) 165 checkToString(dscs); 166 167 DataSource ds = dscs; 168 169 checkConnection("DataSource", ds.getConnection()); 170 171 DataSource dssimple = null; 172 if (testSimpleDataSource) 173 { 174 dssimple = TestUtil.getSimpleDataSource(attrs); 175 ds = dssimple; 176 checkConnection("SimpleDataSource", ds.getConnection()); 177 } 178 179 ConnectionPoolDataSource dsp = TestUtil.getConnectionPoolDataSource(attrs); 180 181 182 if (testConnectionToString) 183 checkToString(dsp); 184 185 186 PooledConnection pc = dsp.getPooledConnection(); 187 SecurityCheck.inspect(pc, "javax.sql.PooledConnection"); 188 pc.addConnectionEventListener(new EventCatcher(1)); 189 190 checkConnection("ConnectionPoolDataSource", pc.getConnection()); 191 checkConnection("ConnectionPoolDataSource", pc.getConnection()); 192 193 Connection c1 = pc.getConnection(); 195 196 Statement s = c1.createStatement(); 197 198 s.executeUpdate("create table t (i int)"); 199 200 s.executeUpdate("insert into t values(1)"); 201 202 c1.setAutoCommit(false); 203 204 s.executeUpdate("insert into t values(2)"); 206 207 c1 = pc.getConnection(); 208 209 ResultSet rs = c1.createStatement().executeQuery("select count(*) from t"); 210 rs.next(); 211 int count = rs.getInt(1); 212 213 System.out.println(count == 1 ? "Changes rolled back OK in auto closed pooled connection" : 214 ("FAIL changes committed in in auto closed pooled connection - " + count)); 215 216 c1.close(); 217 218 try { 220 rs.next(); 221 System.out.println("FAIL - ResultSet is open for a closed connection obtained from PooledConnection"); 222 } catch (SQLException sqle) { 223 System.out.println("expected SQL Exception: (" + sqle.getSQLState() 224 + ") " + sqle.getMessage()); 225 } 226 227 try { 228 s.executeUpdate("update t set i = 1"); 229 System.out.println("FAIL - Statement is open for a closed connection obtained from PooledConnection"); 230 } catch (SQLException sqle) { 231 System.out.println("expected SQL Exception: (" + sqle.getSQLState() 232 + ") " + sqle.getMessage()); 233 } 234 235 pc.close(); 236 pc = null; 237 238 testPoolReset("ConnectionPoolDataSource", dsp.getPooledConnection()); 239 240 XADataSource dsx = TestUtil.getXADataSource(attrs); 241 if (testConnectionToString) 242 checkToString(dsx); 243 244 XAConnection xac = dsx.getXAConnection(); 245 SecurityCheck.inspect(xac, "javax.sql.XAConnection"); 246 xac.addConnectionEventListener(new EventCatcher(3)); 247 248 checkConnection("XADataSource", xac.getConnection()); 249 250 c1 = xac.getConnection(); 252 253 s = c1.createStatement(); 254 255 s.executeUpdate("insert into t values(1)"); 256 257 c1.setAutoCommit(false); 258 259 s.executeUpdate("insert into t values(2)"); 261 262 c1 = xac.getConnection(); 263 264 rs = c1.createStatement().executeQuery("select count(*) from t"); 265 rs.next(); 266 count = rs.getInt(1); 267 rs.close(); 268 269 System.out.println(count == 2 ? "Changes rolled back OK in auto closed local XAConnection" : 270 ("FAIL changes committed in in auto closed pooled connection - " + count)); 271 272 c1.close(); 273 xac.close(); 274 xac = null; 275 276 testPoolReset("XADataSource", dsx.getXAConnection()); 277 278 try { 279 TestUtil.getConnection("","shutdown=true"); 280 } catch (SQLException sqle) { 281 JDBCDisplayUtil.ShowSQLException(System.out, sqle); 282 } 283 284 dmc = ij.startJBMS(); 285 286 cs = dmc.prepareCall("call checkConn2(?)"); 287 SecurityCheck.inspect(cs, "java.sql.CallableStatement"); 288 cs.setString(1,"Nested"); 289 cs.execute(); 290 291 292 checkConnection("DriverManager ", dmc); 293 294 ds = dscs; 296 checkConnection("DataSource", ds.getConnection()); 297 298 if(TestUtil.isEmbeddedFramework()) 300 { 301 ds = dssimple; 303 checkConnection("EmbeddedSimpleDataSource", dssimple.getConnection()); 304 } 305 306 pc = dsp.getPooledConnection(); 307 pc.addConnectionEventListener(new EventCatcher(2)); 308 checkConnection("ConnectionPoolDataSource", pc.getConnection()); 309 checkConnection("ConnectionPoolDataSource", pc.getConnection()); 310 311 xac = dsx.getXAConnection(); 313 xac.addConnectionEventListener(new EventCatcher(4)); 314 checkConnection("XADataSource", xac.getConnection()); 315 checkConnection("XADataSource", xac.getConnection()); 316 xac.close(); 317 318 xac = dsx.getXAConnection(); 320 xac.addConnectionEventListener(new EventCatcher(5)); 321 XAResource xar = xac.getXAResource(); 322 SecurityCheck.inspect(xar, "javax.transaction.xa.XAResource"); 323 Xid xid = new cdsXid(1, (byte) 35, (byte) 47); 324 xar.start(xid, XAResource.TMNOFLAGS); 325 Connection xacc = xac.getConnection(); 326 xacc.close(); 327 checkConnection("Global XADataSource", xac.getConnection()); 328 checkConnection("Global XADataSource", xac.getConnection()); 329 330 xar.end(xid, XAResource.TMSUCCESS); 331 332 checkConnection("Switch to local XADataSource", xac.getConnection()); 333 checkConnection("Switch to local XADataSource", xac.getConnection()); 334 335 Connection backtoGlobal = xac.getConnection(); 336 337 xar.start(xid, XAResource.TMJOIN); 338 checkConnection("Switch to global XADataSource", backtoGlobal); 339 checkConnection("Switch to global XADataSource", xac.getConnection()); 340 xar.end(xid, XAResource.TMSUCCESS); 341 xar.commit(xid, true); 342 343 xac.close(); 344 345 xac = dsx.getXAConnection(); 351 xac.addConnectionEventListener(new EventCatcher(6)); 352 xar = xac.getXAResource(); 353 xid = new cdsXid(1, (byte) 93, (byte) 103); 354 355 Connection cs1 = xac.getConnection(); 357 printState("initial local", cs1); 358 xar.start(xid, XAResource.TMNOFLAGS); 359 printState("initial X1", cs1); 360 cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 361 cs1.setReadOnly(true); 362 setHoldability(cs1, false); 363 printState("modified X1", cs1); 364 xar.end(xid, XAResource.TMSUCCESS); 365 printState("modified local", cs1); 368 369 cs1.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 370 cs1.setReadOnly(false); 371 setHoldability(cs1, false); 372 373 printState("reset local", cs1); 374 375 xar.start(xid, XAResource.TMJOIN); 379 printState("re-join X1", cs1); 380 xar.end(xid, XAResource.TMSUCCESS); 381 382 printState("back to local (same as reset)", cs1); 384 385 xar.start(xid, XAResource.TMJOIN); 390 printState("re-join X1 second time", cs1); 391 392 xar.end(xid, XAResource.TMSUSPEND); 393 printState("local after suspend", cs1); 394 395 xar.start(xid, XAResource.TMRESUME); 396 printState("resume X1", cs1); 397 398 xar.end(xid, XAResource.TMSUCCESS); 399 printState("back to local (second time)", cs1); 400 401 cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 402 cs1.setReadOnly(true); 403 setHoldability(cs1, true); 404 cs1.close(); 405 406 cs1 = xac.getConnection(); 407 printState("new handle - local ", cs1); 408 cs1.close(); 409 410 xar.start(xid, XAResource.TMJOIN); 411 cs1 = xac.getConnection(); 412 printState("re-join with new handle X1", cs1); 413 cs1.close(); 414 xar.end(xid, XAResource.TMSUCCESS); 415 416 cs1 = xac.getConnection(); 420 cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 421 printState("pre-X1 commit - local", cs1); 422 xar.start(xid, XAResource.TMJOIN); 423 printState("pre-X1 commit - X1", cs1); 424 xar.end(xid, XAResource.TMSUCCESS); 425 printState("post-X1 end - local", cs1); 426 xar.commit(xid, true); 427 printState("post-X1 commit - local", cs1); 428 cs1.close(); 429 430 System.out.println("Some more isolation testing using SQL and JDBC api"); 432 cs1 = xac.getConnection(); 433 s = cs1.createStatement(); 434 printState("initial local", cs1); 435 436 System.out.println("Issue setTransactionIsolation in local transaction"); 437 cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 438 printState("setTransactionIsolation in local", cs1); 439 440 testSetIsolationWithStatement(s, xar, cs1); 441 442 System.out.println("TESTING RE_USE OF STATEMENT OBJECTS"); 444 cs1 = xac.getConnection(); 445 446 447 cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 449 cs1.setAutoCommit(false); 450 451 checkLocks(cs1); 452 453 Statement sru1 = cs1.createStatement(); 454 sru1.setCursorName("SN1"); 455 sru1.executeUpdate("create table ru(i int)"); 456 sru1.executeUpdate("insert into ru values 1,2,3"); 457 Statement sruBatch = cs1.createStatement(); 458 sruBatch.setCursorName("sruBatch"); 459 Statement sruState = createFloatStatementForStateChecking(cs1); 460 PreparedStatement psruState = createFloatStatementForStateChecking(cs1, "select i from ru where i = ?"); 461 CallableStatement csruState = createFloatCallForStateChecking(cs1, "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?,?)"); 462 PreparedStatement psParams = cs1.prepareStatement("select * from ru where i > ?"); 463 psParams.setCursorName("params"); 464 psParams.setInt(1, 2); 465 resultSetQuery("Params-local-1", psParams.executeQuery()); 466 467 sruBatch.addBatch("insert into ru values 4"); 468 queryOnStatement("sru1-local-1", cs1, sru1); 469 cs1.commit(); xid = new cdsXid(1, (byte) 103, (byte) 119); 471 xar.start(xid, XAResource.TMNOFLAGS); System.out.println("Expecting downgrade because global transaction sru1-global-2 is using a statement with holdability true"); 473 queryOnStatement("sru1-global-2", cs1, sru1); 474 sruBatch.addBatch("insert into ru values 5"); 475 Statement sru2 = cs1.createStatement(); 476 sru2.setCursorName("OAK2"); 477 queryOnStatement("sru2-global-3", cs1, sru2); 478 System.out.println("Expecting downgrade because global transaction sru1-global-4 is using a statement with holdability true"); 479 queryOnStatement("sru1-global-4", cs1, sru1); 480 showStatementState("GLOBAL ", sruState); 481 showStatementState("PS GLOBAL ", psruState); 482 showStatementState("CS GLOBAL ", csruState); 483 resultSetQuery("Params-global-1", psParams.executeQuery()); 484 485 xar.end(xid, XAResource.TMSUCCESS); 486 queryOnStatement("sru1-local-5", cs1, sru1); 488 queryOnStatement("sru2-local-6", cs1, sru2); 489 sruBatch.addBatch("insert into ru values 6,7"); 490 Statement sru3 = cs1.createStatement(); 491 sru3.setCursorName("SF3"); 492 queryOnStatement("sru3-local-7", cs1, sru3); 493 showStatementState("LOCAL ", sruState); 495 showStatementState("PS LOCAL ", psruState); 496 showStatementState("CS LOCAL ", csruState); 497 resultSetQuery("Params-local-2", psParams.executeQuery()); 498 checkLocks(cs1); 499 cs1.commit(); 500 501 XAConnection xac2 = dsx.getXAConnection(); 503 xac2.addConnectionEventListener(new EventCatcher(5)); 504 XAResource xar2 = xac2.getXAResource(); 505 506 xar2.start(xid, XAResource.TMJOIN); 507 Connection cs2 = xac2.getConnection(); 508 509 queryOnStatement("sru1-local-8", cs1, sru1); 512 queryOnStatement("sru2-local-9", cs1, sru2); 513 queryOnStatement("sru3-local-10", cs1, sru3); 514 sruBatch.addBatch("insert into ru values 8"); 515 showStatementState("LOCAL 2 ", sruState); 516 showStatementState("PS LOCAL 2 ", psruState); 517 showStatementState("CS LOCAL 2", csruState); 518 519 checkLocks(cs1); 520 521 int[] updateCounts = sruBatch.executeBatch(); 522 System.out.print("sruBatch update counts :"); 523 for (int i = 0; i < updateCounts.length; i++) { 524 System.out.print(" " + updateCounts[i] + " "); 525 } 526 System.out.println(":"); 527 queryOnStatement("sruBatch", cs1, sruBatch); 528 529 530 xar2.end(xid, XAResource.TMSUCCESS); 531 532 xac2.close(); 533 534 xac2.close(); 536 xac2.addConnectionEventListener(null); 537 xac2.removeConnectionEventListener(null); 538 539 try { 541 xac2.getXAResource(); 542 } catch (SQLException sqle) { 543 System.out.println("XAConnection.getXAResource : " + sqle.getMessage()); 544 } 545 try { 546 xac2.getConnection(); 547 } catch (SQLException sqle) { 548 System.out.println("XAConnection.getConnection : " + sqle.getMessage()); 549 } 550 try { 551 xar2.start(xid, XAResource.TMJOIN); 552 } catch (XAException xae) { 553 showXAException("XAResource.start", xae); 554 } 555 556 try { 557 xar2.end(xid, XAResource.TMJOIN); 558 } catch (XAException xae) { 559 showXAException("XAResource.end", xae); 560 } 561 try { 562 xar2.commit(xid, true); 563 } catch (XAException xae) { 564 showXAException("XAResource.commit", xae); 565 } 566 try { 567 xar2.prepare(xid); 568 } catch (XAException xae) { 569 showXAException("XAResource.prepare", xae); 570 } 571 try { 572 xar2.recover(0); 573 } catch (XAException xae) { 574 showXAException("XAResource.recover", xae); 575 } 576 try { 577 xar2.prepare(xid); 578 } catch (XAException xae) { 579 showXAException("XAResource.prepare", xae); 580 } 581 try { 582 xar2.isSameRM(xar2); 583 } catch (XAException xae) { 584 showXAException("XAResource.isSameRM", xae); 585 } 586 587 dmc = ij.startJBMS(); 589 dmc.createStatement().executeUpdate("create schema SCHEMA_Patricio"); 590 dmc.createStatement().executeUpdate("create table SCHEMA_Patricio.Patricio (id VARCHAR(255), value INTEGER)"); 591 dmc.commit(); 592 593 dmc.close(); 594 595 XAConnection xac3 = dsx.getXAConnection(); 596 Connection conn3 = xac3.getConnection(); 597 Statement st3 = conn3.createStatement(); 598 st3.execute("SET SCHEMA SCHEMA_Patricio"); 599 st3.close(); 600 601 PreparedStatement ps3 = conn3.prepareStatement("INSERT INTO Patricio VALUES (? , ?)"); 602 ps3.setString(1, "Patricio"); 603 ps3.setInt(2, 3); 604 ps3.executeUpdate(); 605 606 System.out.println("Patricio update count " + ps3.getUpdateCount()); 607 ps3.close(); 608 conn3.close(); 609 xac3.close(); 610 611 XAConnection xac4 = dsx.getXAConnection(); 613 Xid xid4a = new cdsXid(4, (byte) 23, (byte) 76); 614 Connection conn4 = xac4.getConnection(); 615 System.out.println("conn4 autcommit " + conn4.getAutoCommit()); 616 617 Statement s4 = conn4.createStatement(); 618 s4.executeUpdate("create table autocommitxastart(i int)"); 619 s4.executeUpdate("insert into autocommitxastart values 1,2,3,4,5"); 620 621 ResultSet rs4 = s4.executeQuery("select i from autocommitxastart"); 622 rs4.next(); System.out.println("acxs " + rs4.getInt(1)); 623 rs4.next(); System.out.println("acxs " + rs4.getInt(1)); 624 625 xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS); 626 xac4.getXAResource().end(xid4a, XAResource.TMSUCCESS); 627 628 try { 629 rs4.next(); System.out.println("acxs " + rs.getInt(1)); 630 } catch (SQLException sqle) { 631 System.out.println("autocommitxastart expected " + sqle.getMessage()); 632 } 633 634 conn4.setAutoCommit(false); 635 636 rs4 = s4.executeQuery("select i from autocommitxastart"); 637 rs4.next(); System.out.println("acxs " + rs4.getInt(1)); 638 rs4.next(); System.out.println("acxs " + rs4.getInt(1)); 639 640 xid4a = new cdsXid(4, (byte) 93, (byte) 103); 644 try { 645 xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS); 646 } catch (XAException xae) { 647 showXAException("autocommitxastart expected ", xae); 648 System.out.println("Expected XA error code: " + xae.errorCode); 649 } 650 rs4.next(); System.out.println("acxs " + rs4.getInt(1)); 651 rs4.close(); 652 653 conn4.rollback(); 654 conn4.close(); 655 xac4.close(); 656 657 658 Connection dmc95 = ij.startJBMS(); 661 String sqls; 662 try { 663 testJira95ds( dmc95, "jdbc:derby:mydb" ); 664 } catch (SQLException sqle) { 665 sqls = sqle.getSQLState(); 666 if (sqls.equals("XCY00")) 667 System.out.println("; ok - expected exception: " + sqls); 668 else 669 System.out.println("; wrong, unexpected exception: " + sqls + " - " + sqle.toString()); 670 } catch (Exception e) { 671 System.out.println("; wrong, unexpected exception: " + e.toString()); 672 } 673 674 try { 675 testJira95xads( dmc95, "jdbc:derby:wombat" ); 676 } catch (SQLException sqle) { 677 sqls = sqle.getSQLState(); 678 if (sqls.equals("XCY00")) 679 System.out.println("; ok - expected exception: " + sqls + "\n"); 680 else 681 System.out.println("; wrong - unexpected exception: " + sqls + " - " + sqle.toString()); 682 } catch (Exception e) { 683 System.out.println("; wrong, unexpected exception: " + e.toString()); 684 } 685 686 if (TestUtil.isDerbyNetClientFramework()) 687 testClientDSConnectionAttributes(); 688 689 if (TestUtil.isDerbyNetClientFramework()) 693 return; 694 testDSRequestAuthentication(); 695 } 696 697 704 private void testSetIsolationWithStatement(Statement s, XAResource xar, Connection conn) throws SQLException , XAException { 705 Xid xid; 706 System.out.println("Issue SQL to change isolation in local transaction"); 707 s.executeUpdate("set current isolation = RR"); 708 printState("SQL to change isolation in local", conn); 709 710 xid = new cdsXid(1, (byte) 35, (byte) 47); 711 xar.start(xid, XAResource.TMNOFLAGS); 712 printState("1st global(new)", conn); 713 xar.end(xid, XAResource.TMSUCCESS); 714 715 printState("local", conn); 716 System.out.println("Issue SQL to change isolation in local transaction"); 717 s.executeUpdate("set current isolation = RS"); 718 printState("SQL to change isolation in local", conn); 719 720 xar.start(xid, XAResource.TMJOIN); 724 printState("1st global(existing)", conn); 725 xar.end(xid, XAResource.TMSUCCESS); 726 printState("local", conn); 727 729 Xid xid2 = new cdsXid(1, (byte) 93, (byte) 103); 730 xar.start(xid2, XAResource.TMNOFLAGS); 731 printState("2nd global(new)", conn); 732 xar.end(xid2, XAResource.TMSUCCESS); 733 734 xar.start(xid, XAResource.TMJOIN); 735 printState("1st global(existing)", conn); 736 xar.end(xid, XAResource.TMSUCCESS); 737 738 printState("local", conn); 739 740 xar.start(xid, XAResource.TMJOIN); 741 printState("1st global(existing)", conn); 742 System.out.println("Issue SQL to change isolation in 1st global transaction"); 743 s.executeUpdate("set current isolation = UR"); 744 printState("change isolation of existing 1st global transaction", conn); 745 xar.end(xid, XAResource.TMSUCCESS); 746 747 printState("local", conn); 748 749 xar.start(xid2, XAResource.TMJOIN); 750 printState("2nd global(existing)", conn); 751 xar.end(xid2, XAResource.TMSUCCESS); 752 xar.rollback(xid2); 753 printState("(After 2nd global rollback) local", conn); 754 755 xar.rollback(xid); 756 printState("(After 1st global rollback) local", conn); 757 } 758 759 protected void showXAException(String tag, XAException xae) { 760 761 System.out.println(tag + " : XAException - " + xae.getMessage()); 762 } 763 764 767 protected Statement createFloatStatementForStateChecking(Connection conn) throws SQLException { 768 Statement s = internalCreateFloatStatementForStateChecking(conn); 769 s.setCursorName("StokeNewington"); 770 s.setFetchDirection(ResultSet.FETCH_REVERSE); 771 s.setFetchSize(444); 772 s.setMaxFieldSize(713); 773 s.setMaxRows(19); 774 775 showStatementState("Create ", s); 776 return s; 777 } 778 779 protected Statement internalCreateFloatStatementForStateChecking(Connection conn) throws SQLException { 780 return conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 781 } 782 783 protected void showStatementState(String when, Statement s) throws SQLException { 784 System.out.println("Statement State @ " + when); 785 System.out.println(" getResultSetType() " + rsType(s.getResultSetType())); 786 System.out.println(" getResultSetConcurrency() " + rsConcurrency(s.getResultSetConcurrency())); 787 System.out.println(" getFetchDirection() " + rsFetchDirection(s.getFetchDirection())); 788 System.out.println(" getFetchSize() " + s.getFetchSize()); 789 System.out.println(" getMaxFieldSize() " + s.getMaxFieldSize()); 790 System.out.println(" getMaxRows() " + s.getMaxRows()); 791 } 792 protected PreparedStatement createFloatStatementForStateChecking(Connection conn, String sql) throws SQLException { 793 PreparedStatement s = internalCreateFloatStatementForStateChecking(conn, sql); 794 s.setCursorName("StokeNewington"); 795 s.setFetchDirection(ResultSet.FETCH_REVERSE); 796 s.setFetchSize(888); 797 s.setMaxFieldSize(317); 798 s.setMaxRows(91); 799 800 showStatementState("PS Create ", s); 801 return s; 802 } 803 protected CallableStatement createFloatCallForStateChecking(Connection conn, String sql) throws SQLException { 804 CallableStatement s = internalCreateFloatCallForStateChecking(conn, sql); 805 s.setCursorName("StokeNewington"); 806 s.setFetchDirection(ResultSet.FETCH_REVERSE); 807 s.setFetchSize(999); 808 s.setMaxFieldSize(137); 809 s.setMaxRows(85); 810 811 showStatementState("CS Create ", s); 812 return s; 813 } 814 protected PreparedStatement internalCreateFloatStatementForStateChecking(Connection conn, String sql) throws SQLException { 815 return conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 816 } 817 protected CallableStatement internalCreateFloatCallForStateChecking(Connection conn, String sql) throws SQLException { 818 return conn.prepareCall(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 819 } 820 821 826 protected String getNestedMethodName() 827 { 828 return "checkDataSource.checkNesConn"; 829 } 830 831 static String rsType(int type) { 832 switch (type) { 833 case ResultSet.TYPE_FORWARD_ONLY: 834 return "FORWARD_ONLY"; 835 case ResultSet.TYPE_SCROLL_SENSITIVE: 836 return "SCROLL_SENSITIVE"; 837 case ResultSet.TYPE_SCROLL_INSENSITIVE: 838 return "SCROLL_INSENSITIVE"; 839 default: 840 return "?? TYPE UNKNOWN ??"; 841 842 } 843 } 844 845 static String rsConcurrency(int type) { 846 switch (type) { 847 case ResultSet.CONCUR_READ_ONLY: 848 return "READ_ONLY"; 849 case ResultSet.CONCUR_UPDATABLE: 850 return "UPDATEABLE"; 851 default: 852 return "?? CONCURRENCY UNKNOWN ??"; 853 854 } 855 } 856 static String rsFetchDirection(int type) { 857 switch (type) { 858 case ResultSet.FETCH_FORWARD: 859 return "FORWARD"; 860 case ResultSet.FETCH_REVERSE: 861 return "REVERSE"; 862 case ResultSet.FETCH_UNKNOWN: 863 return "UNKNOWN"; 864 default: 865 return "?? FETCH DIRECTION REALLY UNKNOWN ??"; 866 867 } 868 } 869 private static void checkLocks(Connection conn) throws SQLException { 870 Statement s = conn.createStatement(); 871 ResultSet rs = s.executeQuery("SELECT XID, sum(cast (LOCKCOUNT AS INT)) FROM SYSCS_DIAG.LOCK_TABLE AS L GROUP BY XID"); 872 System.out.println("LOCK TABLE"); 873 874 int xact_index = 0; 877 while (rs.next()) { 878 System.out.println(" xid row " + xact_index + " lock count " + rs.getInt(2)); 880 xact_index++; 881 } 882 s.close(); 883 System.out.println("END LOCK TABLE"); 884 } 885 886 private static void queryOnStatement(String tag, Connection conn, Statement s) throws SQLException { 887 888 try { 889 if (s.getConnection() != conn) 890 System.out.println(tag + ": mismatched Statement connection"); 891 resultSetQuery(tag, s.executeQuery("select * from ru")); 892 } catch (SQLException sqle) { 893 System.out.println(tag + ": " + sqle.toString()); 894 } 895 } 896 897 private static void resultSetQuery(String tag, ResultSet rs) throws SQLException { 898 String cursorName = rs.getCursorName(); 899 System.out.print(tag + ": ru(" + cursorName + ") contents"); 900 SecurityCheck.inspect(rs, "java.sql.ResultSet"); 901 while (rs.next()) { 902 System.out.print(" {" + rs.getInt(1) + "}"); 903 } 904 System.out.println(""); 905 rs.close(); 906 } 907 908 private void printState(String header, Connection conn) throws SQLException { 909 System.out.println(header); 910 getHoldability(conn); 911 System.out.println(" isolation level " + translateIso(conn.getTransactionIsolation())); 912 System.out.println(" auto commit " + conn.getAutoCommit()); 913 System.out.println(" read only " + conn.isReadOnly()); 914 } 915 916 protected void setHoldability(Connection conn, boolean hold) throws SQLException { 917 } 918 919 protected void getHoldability(Connection conn) throws SQLException { 920 } 921 922 public static void checkNesConn (String dsName) throws SQLException { 924 Connection conn = DriverManager.getConnection("jdbc:default:connection"); 925 new checkDataSource().checkConnection(dsName, conn); 926 } 927 928 public void checkConnection(String dsName, Connection conn) throws SQLException { 929 930 System.out.println("Running connection checks on " + dsName); 931 932 SecurityCheck.inspect(conn, "java.sql.Connection"); 933 SecurityCheck.inspect(conn.getMetaData(), "java.sql.DatabaseMetaData"); 934 935 System.out.println(" isolation level " + conn.getTransactionIsolation()); 937 System.out.println(" auto commit " + conn.getAutoCommit()); 938 System.out.println(" read only " + conn.isReadOnly()); 939 940 if (dsName.endsWith("DataSource") && !dsName.startsWith("Global")) 942 System.out.println(" has warnings " + (conn.getWarnings() != null)); 943 944 Statement s1 = conn.createStatement(); 945 checkStatement(dsName, conn, s1); 946 checkStatement(dsName, conn, conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)); 947 948 Connection c1 = conn.getMetaData().getConnection(); 949 if (c1 != conn) 950 System.out.println("FAIL incorrect connection object returned for DatabaseMetaData.getConnection()"); 951 952 try { 954 conn.setTypeMap(java.util.Collections.EMPTY_MAP); 955 System.out.println("setTypeMap(EMPTY_MAP) - ok"); 956 } catch (SQLException sqle) { 957 System.out.println("setTypeMap(EMPTY_MAP) - FAIL " + sqle.getSQLState() + " - " + sqle.getMessage()); 958 } 959 try { 960 conn.setTypeMap(null); 961 System.out.println("setTypeMap(null) - FAIL - should throw exception"); 962 } catch (SQLException sqle) { 963 System.out.println("setTypeMap(null) - ok " + sqle.getSQLState() + " - " + sqle.getMessage()); 964 } 965 try { 966 java.util.Map map = new java.util.HashMap (); 968 map.put("name", "class"); 969 970 conn.setTypeMap(map); 971 System.out.println("setTypeMap(map) - FAIL - should throw exception"); 972 } catch (SQLException sqle) { 973 System.out.println("setTypeMap(map) - ok " + sqle.getSQLState() + " - " + sqle.getMessage()); 974 } 975 976 checkConnectionPreClose(dsName, conn); 977 conn.close(); 978 979 System.out.println("method calls on a closed connection"); 980 981 try { 982 conn.close(); 983 System.out.println(dsName + " <closedconn>.close() no error"); 984 } catch (SQLException sqle) { 985 System.out.println(dsName + " <closedconn>.close() " + sqle.getSQLState() + " - " + sqle.getMessage()); 986 } 987 try { 988 conn.createStatement(); 989 System.out.println(dsName + " <closedconn>.createStatement() no error"); 990 } catch (SQLException sqle) { 991 System.out.println(dsName + " <closedconn>.createStatement() " + sqle.getSQLState() + " - " + sqle.getMessage()); 992 } 993 try { 994 s1.execute("values 1"); 995 System.out.println(dsName + " <closedstmt>.execute() no error"); 996 } catch (SQLException sqle) { 997 System.out.println(dsName + " <closedstmt>.execute() " + sqle.getSQLState() + " - " + sqle.getMessage()); 998 } 999 } 1000 1001 1004 protected static void checkToString(Connection conn) throws Exception 1005 { 1006 checkStringFormat(conn); 1007 String str = conn.toString(); 1008 1009 if ( conns.containsKey(str)) 1010 { 1011 throw new Exception ("ERROR: Connection toString() is not unique: " 1012 + str); 1013 } 1014 conns.put(str, conn); 1015 } 1016 1017 1020 protected static void checkStringFormat(PooledConnection pc) throws Exception 1021 { 1022 String prefix = checkStringPrefix(pc); 1023 String connstr = pc.toString(); 1024 String format = "/" + prefix + 1025 " \\(ID = [0-9]+\\), Physical Connection = " + 1026 "<none>|" + CONNSTRING_FORMAT + "/"; 1027 1028 if ( ! p5u.match(format, connstr) ) 1029 { 1030 throw new Exception ( "Connection.toString() (" + connstr + ") " + 1031 "does not match expected format (" + format + ")"); 1032 } 1033 } 1034 1035 1039 protected static void checkStringFormat(Connection conn) throws Exception 1040 { 1041 String prefix = checkStringPrefix(conn); 1042 1043 String str = conn.toString(); 1044 1045 if ( ! p5u.match("/" + CONNSTRING_FORMAT + "/", str) ) 1047 { 1048 throw new Exception ( "Connection.toString() (" + str + ") " + 1049 "does not match expected format (" + CONNSTRING_FORMAT + ")"); 1050 } 1051 } 1052 1053 1060 protected static String checkStringPrefix(Object conn) throws Exception 1061 { 1062 String connstr = conn.toString(); 1063 String prefix = conn.getClass().getName() + "@" + conn.hashCode(); 1064 if ( ! connstr.startsWith(prefix) ) 1065 { 1066 throw new Exception ("Connection class and hash code for " + 1067 "connection string (" + connstr + ") does not match expected " + 1068 "(" + prefix + ")"); 1069 } 1070 1071 return prefix; 1072 } 1073 1074 1078 protected static void clearConnections() throws SQLException 1079 { 1080 java.util.Iterator it = conns.values().iterator(); 1081 while ( it.hasNext() ) 1082 { 1083 Connection conn = (Connection )it.next(); 1084 conn.close(); 1085 } 1086 conns.clear(); 1087 } 1088 1089 1093 protected static void checkJBMSToString() throws Exception 1094 { 1095 clearConnections(); 1096 int numConnections = 10; 1100 for ( int i = 0 ; i < numConnections ; i++ ) 1101 { 1102 Connection conn = ij.startJBMS(); 1103 checkToString(conn); 1104 } 1105 1106 clearConnections(); 1108 } 1109 1110 1114 protected static void checkToString(DataSource ds) throws Exception 1115 { 1116 clearConnections(); 1117 1118 int numConnections = 10; 1119 for ( int i = 0 ; i < numConnections ; i++ ) 1120 { 1121 Connection conn = ds.getConnection(); 1122 checkToString(conn); 1123 } 1124 1125 clearConnections(); 1126 } 1127 1128 1133 protected static void checkToString(ConnectionPoolDataSource pds) 1134 throws Exception 1135 { 1136 int numConnections = 10; 1137 1138 Hashtable pooledConns = new Hashtable (); 1141 for ( int i = 0 ; i < numConnections ; i++ ) 1142 { 1143 PooledConnection pc = pds.getPooledConnection(); 1144 checkStringFormat(pc); 1145 String str = pc.toString(); 1146 if ( pooledConns.get(str) != null ) 1147 { 1148 throw new Exception ("Pooled connection toString " + 1149 "value " + str + " is not unique"); 1150 } 1151 pooledConns.put(str, pc); 1152 } 1153 1154 Iterator it = pooledConns.values().iterator(); 1157 clearConnections(); 1158 while ( it.hasNext() ) 1159 { 1160 PooledConnection pc = (PooledConnection )it.next(); 1161 Connection conn = pc.getConnection(); 1162 checkToString(conn); 1163 } 1164 clearConnections(); 1165 1166 it = pooledConns.values().iterator(); 1168 while ( it.hasNext() ) 1169 { 1170 PooledConnection pc = (PooledConnection )it.next(); 1171 pc.close(); 1172 } 1173 pooledConns.clear(); 1174 } 1175 1176 1179 protected static void checkToString(XADataSource xds) throws Exception 1180 { 1181 int numConnections = 10; 1182 1183 Hashtable xaConns = new Hashtable (); 1186 for ( int i = 0 ; i < numConnections ; i++ ) 1187 { 1188 XAConnection xc = xds.getXAConnection(); 1189 checkStringFormat(xc); 1190 String str = xc.toString(); 1191 if ( xaConns.get(str) != null ) 1192 { 1193 throw new Exception ("XA connection toString " + 1194 "value " + str + " is not unique"); 1195 } 1196 xaConns.put(str, xc); 1197 } 1198 1199 Iterator it = xaConns.values().iterator(); 1202 clearConnections(); 1203 while ( it.hasNext() ) 1204 { 1205 XAConnection xc = (XAConnection )it.next(); 1206 Connection conn = xc.getConnection(); 1207 checkToString(conn); 1208 } 1209 clearConnections(); 1210 1211 it = xaConns.values().iterator(); 1213 while ( it.hasNext() ) 1214 { 1215 XAConnection xc = (XAConnection )it.next(); 1216 xc.close(); 1217 } 1218 xaConns.clear(); 1219 } 1220 1221 protected void checkConnectionPreClose(String dsName, Connection conn) throws SQLException { 1222 if (dsName.endsWith("DataSource")) { 1223 1224 try { 1227 conn.setReadOnly(true); 1228 } catch (SQLException sqle) { 1229 } 1232 } 1233 } 1234 1235 protected void checkStatement(String dsName, Connection conn, Statement s) throws SQLException { 1236 1237 SecurityCheck.inspect(s, "java.sql.Statement"); 1238 1239 Connection c1 = s.getConnection(); 1240 if (c1 != conn) 1241 System.out.println("FAIL incorrect connection object returned for Statement.getConnection()"); 1242 1243 s.addBatch("insert into y values 1"); 1244 s.addBatch("insert into y values 2,3"); 1245 int[] states = s.executeBatch(); 1246 if (states[0] != 1) 1247 System.out.println("FAIL invalid update count for first batch statement"); 1248 if (states[1] != 2) 1249 System.out.println("FAIL invalid update count for second batch statement"); 1250 1251 ResultSet rs = s.executeQuery("VALUES 1"); 1252 if (rs.getStatement() != s) 1253 System.out.println(dsName + " FAIL incorrect Statement object returned for ResultSet.getStatement"); 1254 rs.close(); 1255 s.close(); 1256 } 1257 1258 private static void testDSRequestAuthentication() throws SQLException { 1259 1260 EmbeddedDataSource ds = new EmbeddedDataSource(); 1261 1262 System.out.println("DataSource - EMPTY"); 1263 dsConnectionRequests(ds); 1264 1265 System.out.println("DataSource - connectionAttributes=databaseName=wombat"); 1266 ds.setConnectionAttributes("databaseName=wombat"); 1267 dsConnectionRequests(ds); 1268 ds.setConnectionAttributes(null); 1269 1270 System.out.println("DataSource - attributesAsPassword=true"); 1271 ds.setAttributesAsPassword(true); 1272 dsConnectionRequests(ds); 1273 ds.setAttributesAsPassword(false); 1274 1275 System.out.println("DataSource - attributesAsPassword=true, connectionAttributes=databaseName=kangaroo"); 1276 ds.setAttributesAsPassword(true); 1277 ds.setConnectionAttributes("databaseName=kangaroo"); 1278 dsConnectionRequests(ds); 1279 ds.setAttributesAsPassword(false); 1280 ds.setConnectionAttributes(null); 1281 1282 System.out.println("Enable Authentication"); 1283 ds.setDatabaseName("wombat"); 1284 Connection cadmin = ds.getConnection(); 1285 CallableStatement cs = cadmin.prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); 1286 cs.setString(1, "derby.user.fred"); 1287 cs.setString(2, "wilma"); 1288 cs.execute(); 1289 1290 cs.setString(1, "derby.authentication.provider"); 1291 cs.setString(2, "BUILTIN"); 1292 cs.execute(); 1293 1294 cs.setString(1, "derby.connection.requireAuthentication"); 1295 cs.setString(2, "true"); 1296 cs.execute(); 1297 1298 cs.close(); 1299 1300 cadmin.close(); 1301 1302 ds.setShutdownDatabase("shutdown"); 1303 try { 1304 ds.getConnection(); 1305 } catch (SQLException sqle) { 1306 System.out.println(sqle.getSQLState() + ":" + sqle.getMessage() ); 1307 } 1308 1309 ds.setDatabaseName(null); 1310 ds.setShutdownDatabase(null); 1311 1312 System.out.println("AUTHENTICATION NOW ENABLED"); 1313 1314 System.out.println("DataSource - attributesAsPassword=true"); 1315 ds.setAttributesAsPassword(true); 1316 dsConnectionRequests(ds); 1317 ds.setAttributesAsPassword(false); 1318 1319 System.out.println("DataSource - attributesAsPassword=true, user=fred, password=databaseName=wombat;password=wilma"); 1321 ds.setAttributesAsPassword(true); 1322 ds.setUser("fred"); 1323 ds.setPassword("databaseName=wombat;password=wilma"); 1324 dsConnectionRequests(ds); 1325 ds.setAttributesAsPassword(false); 1326 ds.setUser(null); 1327 ds.setPassword(null); 1328 ds = null; 1329 1330 EmbeddedConnectionPoolDataSource cpds = new EmbeddedConnectionPoolDataSource(); 1332 System.out.println("ConnectionPoolDataSource - EMPTY"); 1333 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1334 1335 System.out.println("ConnectionPoolDataSource - connectionAttributes=databaseName=wombat"); 1336 cpds.setConnectionAttributes("databaseName=wombat"); 1337 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1338 cpds.setConnectionAttributes(null); 1339 1340 System.out.println("ConnectionPoolDataSource - attributesAsPassword=true"); 1341 cpds.setAttributesAsPassword(true); 1342 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1343 cpds.setAttributesAsPassword(false); 1344 1345 System.out.println("ConnectionPoolDataSource - attributesAsPassword=true, user=fred, password=databaseName=wombat;password=wilma"); 1347 cpds.setAttributesAsPassword(true); 1348 cpds.setUser("fred"); 1349 cpds.setPassword("databaseName=wombat;password=wilma"); 1350 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1351 cpds.setAttributesAsPassword(false); 1352 cpds.setUser(null); 1353 cpds.setPassword(null); 1354 cpds = null; 1355 1356 EmbeddedXADataSource xads = new EmbeddedXADataSource(); 1358 System.out.println("XADataSource - EMPTY"); 1359 dsConnectionRequests((XADataSource ) xads); 1360 1361 System.out.println("XADataSource - databaseName=wombat"); 1362 xads.setDatabaseName("wombat"); 1363 dsConnectionRequests((XADataSource ) xads); 1364 xads.setDatabaseName(null); 1365 1366 System.out.println("XADataSource - connectionAttributes=databaseName=wombat"); 1367 xads.setConnectionAttributes("databaseName=wombat"); 1368 dsConnectionRequests((XADataSource ) xads); 1369 xads.setConnectionAttributes(null); 1370 1371 System.out.println("XADataSource - attributesAsPassword=true"); 1372 xads.setAttributesAsPassword(true); 1373 dsConnectionRequests((XADataSource ) xads); 1374 xads.setAttributesAsPassword(false); 1375 1376 System.out.println("XADataSource - databaseName=wombat, attributesAsPassword=true"); 1377 xads.setDatabaseName("wombat"); 1378 xads.setAttributesAsPassword(true); 1379 dsConnectionRequests((XADataSource ) xads); 1380 xads.setAttributesAsPassword(false); 1381 xads.setDatabaseName(null); 1382 } 1383 1384 1390 private static void testClientDSConnectionAttributes() throws SQLException { 1391 1392 ClientDataSource ds = new ClientDataSource(); 1393 1394 System.out.println("DataSource - EMPTY"); 1395 dsConnectionRequests(ds); 1396 1397 System.out.println("DataSource - connectionAttributes=databaseName=wombat"); 1398 ds.setConnectionAttributes("databaseName=wombat"); 1399 dsConnectionRequests(ds); 1400 ds.setConnectionAttributes(null); 1401 1402 System.out.println("DataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo"); 1404 ds.setConnectionAttributes("databaseName=kangaroo"); 1405 ds.setDatabaseName("wombat"); 1406 dsConnectionRequests(ds); 1407 ds.setConnectionAttributes(null); 1408 ds.setDatabaseName(null); 1409 1410 ClientConnectionPoolDataSource cpds = new ClientConnectionPoolDataSource(); 1412 System.out.println("ConnectionPoolDataSource - EMPTY"); 1413 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1414 1415 System.out.println("ConnectionPoolDataSource - connectionAttributes=databaseName=wombat"); 1416 cpds.setConnectionAttributes("databaseName=wombat"); 1417 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1418 cpds.setConnectionAttributes(null); 1419 1420 System.out.println("ConnectionPoolDataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo"); 1422 cpds.setConnectionAttributes("databaseName=kangaroo"); 1423 cpds.setDatabaseName("wombat"); 1424 dsConnectionRequests((ConnectionPoolDataSource )cpds); 1425 cpds.setConnectionAttributes(null); 1426 cpds.setDatabaseName(null); 1427 1428 ClientXADataSource xads = new ClientXADataSource(); 1430 System.out.println("XADataSource - EMPTY"); 1431 dsConnectionRequests((XADataSource ) xads); 1432 1433 System.out.println("XADataSource - connectionAttributes=databaseName=wombat"); 1434 xads.setConnectionAttributes("databaseName=wombat"); 1435 dsConnectionRequests((XADataSource ) xads); 1436 xads.setConnectionAttributes(null); 1437 1438 System.out.println("XADataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo"); 1440 xads.setConnectionAttributes("databaseName=kangaroo"); 1441 xads.setDatabaseName("wombat"); 1442 dsConnectionRequests((XADataSource ) xads); 1443 xads.setConnectionAttributes(null); 1444 xads.setDatabaseName(null); 1445 } 1446 1447 private static void dsConnectionRequests(DataSource ds) { 1448 1449 SecurityCheck.inspect(ds, "javax.sql.DataSource"); 1450 1451 try { 1452 Connection c1 = ds.getConnection(); 1453 System.out.println(" getConnection() - OK"); 1454 c1.close(); 1455 } catch (SQLException sqle) { 1456 System.out.println(" getConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1457 } 1458 1459 try { 1460 Connection c1 = ds.getConnection(null, null); 1461 System.out.println(" getConnection(null, null) - OK"); 1462 c1.close(); 1463 } catch (SQLException sqle) { 1464 System.out.println(" getConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1465 } 1466 try { 1467 Connection c1 = ds.getConnection("fred", null); 1468 System.out.println(" getConnection(fred, null) - OK"); 1469 c1.close(); 1470 } catch (SQLException sqle) { 1471 System.out.println(" getConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1472 } 1473 try { 1474 Connection c1 = ds.getConnection("fred", "wilma"); 1475 System.out.println(" getConnection(fred, wilma) - OK"); 1476 c1.close(); 1477 } catch (SQLException sqle) { 1478 System.out.println(" getConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1479 } 1480 try { 1481 Connection c1 = ds.getConnection(null, "wilma"); 1482 System.out.println(" getConnection(null, wilma) - OK"); 1483 c1.close(); 1484 } catch (SQLException sqle) { 1485 System.out.println(" getConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1486 } 1487 try { 1488 Connection c1 = ds.getConnection(null, "databaseName=wombat"); 1489 System.out.println(" getConnection(null, databaseName=wombat) - OK"); 1490 c1.close(); 1491 } catch (SQLException sqle) { 1492 System.out.println(" getConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1493 } 1494 try { 1495 Connection c1 = ds.getConnection("fred", "databaseName=wombat"); 1496 System.out.println(" getConnection(fred, databaseName=wombat) - OK"); 1497 c1.close(); 1498 } catch (SQLException sqle) { 1499 System.out.println(" getConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1500 } 1501 try { 1502 Connection c1 = ds.getConnection("fred", "databaseName=wombat;password=wilma"); 1503 System.out.println(" getConnection(fred, databaseName=wombat;password=wilma) - OK"); 1504 c1.close(); 1505 } catch (SQLException sqle) { 1506 System.out.println(" getConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1507 } 1508 try { 1509 Connection c1 = ds.getConnection("fred", "databaseName=wombat;password=betty"); 1510 System.out.println(" getConnection(fred, databaseName=wombat;password=betty) - OK"); 1511 c1.close(); 1512 } catch (SQLException sqle) { 1513 System.out.println(" getConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1514 } 1515 } 1516 private static void dsConnectionRequests(ConnectionPoolDataSource ds) { 1517 try { 1518 PooledConnection pc = ds.getPooledConnection(); 1519 System.out.println(" getPooledConnection() - OK"); 1520 Connection c1 = pc.getConnection(); 1521 c1.close(); 1522 pc.close(); 1523 } catch (SQLException sqle) { 1524 System.out.println(" getPooledConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1525 } 1526 1527 try { 1528 PooledConnection pc = ds.getPooledConnection(null, null); 1529 System.out.println(" getPooledConnection(null, null) - OK"); 1530 Connection c1 = pc.getConnection(); 1531 c1.close(); 1532 } catch (SQLException sqle) { 1533 System.out.println(" getPooledConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1534 } 1535 try { 1536 PooledConnection pc = ds.getPooledConnection("fred", null); 1537 System.out.println(" getPooledConnection(fred, null) - OK"); 1538 Connection c1 = pc.getConnection(); 1539 c1.close(); 1540 pc.close(); 1541 } catch (SQLException sqle) { 1542 System.out.println(" getPooledConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1543 } 1544 try { 1545 PooledConnection pc = ds.getPooledConnection("fred", "wilma"); 1546 System.out.println(" getPooledConnection(fred, wilma) - OK"); 1547 Connection c1 = pc.getConnection(); 1548 c1.close(); 1549 pc.close(); 1550 } catch (SQLException sqle) { 1551 System.out.println(" getPooledConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1552 } 1553 try { 1554 PooledConnection pc = ds.getPooledConnection(null, "wilma"); 1555 System.out.println(" getPooledConnection(null, wilma) - OK"); 1556 Connection c1 = pc.getConnection(); 1557 c1.close(); 1558 pc.close(); 1559 } catch (SQLException sqle) { 1560 System.out.println(" getPooledConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1561 } 1562 try { 1563 PooledConnection pc = ds.getPooledConnection(null, "databaseName=wombat"); 1564 System.out.println(" getPooledConnection(null, databaseName=wombat) - OK"); 1565 Connection c1 = pc.getConnection(); 1566 c1.close(); 1567 pc.close(); 1568 } catch (SQLException sqle) { 1569 System.out.println(" getPooledConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1570 } 1571 try { 1572 PooledConnection pc = ds.getPooledConnection("fred", "databaseName=wombat"); 1573 System.out.println(" getPooledConnection(fred, databaseName=wombat) - OK"); 1574 Connection c1 = pc.getConnection(); 1575 c1.close(); 1576 pc.close(); 1577 } catch (SQLException sqle) { 1578 System.out.println(" getPooledConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1579 } 1580 try { 1581 PooledConnection pc = ds.getPooledConnection("fred", "databaseName=wombat;password=wilma"); 1582 System.out.println(" getPooledConnection(fred, databaseName=wombat;password=wilma) - OK"); 1583 Connection c1 = pc.getConnection(); 1584 c1.close(); 1585 pc.close(); 1586 } catch (SQLException sqle) { 1587 System.out.println(" getPooledConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1588 } 1589 try { 1590 PooledConnection pc = ds.getPooledConnection("fred", "databaseName=wombat;password=betty"); 1591 System.out.println(" getPooledConnection(fred, databaseName=wombat;password=betty) - OK"); 1592 Connection c1 = pc.getConnection(); 1593 c1.close(); 1594 pc.close(); 1595 } catch (SQLException sqle) { 1596 System.out.println(" getPooledConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1597 } 1598 } 1599 private static void dsConnectionRequests(XADataSource ds) { 1600 try { 1601 XAConnection xc = ds.getXAConnection(); 1602 System.out.println(" getXAConnection() - OK"); 1603 Connection c1 = xc.getConnection(); 1604 c1.close(); 1605 xc.close(); 1606 } catch (SQLException sqle) { 1607 System.out.println(" getXAConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1608 } 1609 1610 try { 1611 XAConnection xc = ds.getXAConnection(null, null); 1612 System.out.println(" getXAConnection(null, null) - OK"); 1613 Connection c1 = xc.getConnection(); 1614 c1.close(); 1615 } catch (SQLException sqle) { 1616 System.out.println(" getXAConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1617 } 1618 try { 1619 XAConnection xc = ds.getXAConnection("fred", null); 1620 System.out.println(" getXAConnection(fred, null) - OK"); 1621 Connection c1 = xc.getConnection(); 1622 c1.close(); 1623 xc.close(); 1624 } catch (SQLException sqle) { 1625 System.out.println(" getXAConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1626 } 1627 try { 1628 XAConnection xc = ds.getXAConnection("fred", "wilma"); 1629 System.out.println(" getXAConnection(fred, wilma) - OK"); 1630 Connection c1 = xc.getConnection(); 1631 c1.close(); 1632 xc.close(); 1633 } catch (SQLException sqle) { 1634 System.out.println(" getXAConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1635 } 1636 try { 1637 XAConnection xc = ds.getXAConnection(null, "wilma"); 1638 System.out.println(" getXAConnection(null, wilma) - OK"); 1639 Connection c1 = xc.getConnection(); 1640 c1.close(); 1641 xc.close(); 1642 } catch (SQLException sqle) { 1643 System.out.println(" getXAConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1644 } 1645 try { 1646 XAConnection xc = ds.getXAConnection(null, "databaseName=wombat"); 1647 System.out.println(" getXAConnection(null, databaseName=wombat) - OK"); 1648 Connection c1 = xc.getConnection(); 1649 c1.close(); 1650 xc.close(); 1651 } catch (SQLException sqle) { 1652 System.out.println(" getXAConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1653 } 1654 try { 1655 XAConnection xc = ds.getXAConnection("fred", "databaseName=wombat"); 1656 System.out.println(" getXAConnection(fred, databaseName=wombat) - OK"); 1657 Connection c1 = xc.getConnection(); 1658 c1.close(); 1659 xc.close(); 1660 } catch (SQLException sqle) { 1661 System.out.println(" getXAConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1662 } 1663 try { 1664 XAConnection xc = ds.getXAConnection("fred", "databaseName=wombat;password=wilma"); 1665 System.out.println(" getXAConnection(fred, databaseName=wombat;password=wilma) - OK"); 1666 Connection c1 = xc.getConnection(); 1667 c1.close(); 1668 xc.close(); 1669 } catch (SQLException sqle) { 1670 System.out.println(" getXAConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1671 } 1672 try { 1673 XAConnection xc = ds.getXAConnection("fred", "databaseName=wombat;password=betty"); 1674 System.out.println(" getXAConnection(fred, databaseName=wombat;password=betty) - OK"); 1675 Connection c1 = xc.getConnection(); 1676 c1.close(); 1677 xc.close(); 1678 } catch (SQLException sqle) { 1679 System.out.println(" getXAConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() ); 1680 } 1681 } 1682 1683 protected Xid getXid(int xid, byte b1, byte b2) { 1684 return new cdsXid(xid, b1, b2); 1685 } 1686 1687 public static String translateIso(int iso) 1688 { 1689 switch(iso) 1690 { 1691 case Connection.TRANSACTION_READ_COMMITTED: return "READ_COMMITTED"; 1692 case Connection.TRANSACTION_SERIALIZABLE: return "SERIALIZABLE"; 1693 case Connection.TRANSACTION_REPEATABLE_READ: return "REPEATABLE_READ"; 1694 case Connection.TRANSACTION_READ_UNCOMMITTED: return "READ_UNCOMMITTED"; 1695 } 1696 return "unknown"; 1697 } 1698 1699 1706 private static void testPoolReset(String type, PooledConnection pc) throws SQLException 1707 { 1708 System.out.println("Start testPoolReset " + type); 1709 testPoolResetWork("C", pc.getConnection()); 1710 testPoolResetWork("", pc.getConnection()); 1711 testPoolResetWork("D", pc.getConnection()); 1712 1713 pc.close(); 1714 System.out.println("End testPoolReset " + type); 1715 } 1716 1717 private static void testPoolResetWork(String tableAction, Connection conn) throws SQLException 1718 { 1719 Statement s = conn.createStatement(); 1720 if (tableAction.equals("C")) 1721 { 1722 s.execute("CREATE TABLE testPoolResetWork (id int generated always as identity, name varchar(25))"); 1723 } 1724 1725 ResultSet rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()"); 1726 rs.next(); 1727 String val = rs.getString(1); 1728 if (!rs.wasNull() || (val != null)) 1729 System.out.println("FAIL - initial call to IDENTITY_VAL_LOCAL is not NULL!" + val); 1730 rs.close(); 1731 1732 s.executeUpdate("INSERT INTO testPoolResetWork(name) values ('derby-222')"); 1733 1734 rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()"); 1735 rs.next(); 1736 val = rs.getString(1); 1737 System.out.println("IDENTITY_VAL_LOCAL=" + val); 1738 rs.close(); 1739 1740 if (tableAction.equals("D")) 1741 { 1742 s.execute("DROP TABLE testPoolResetWork"); 1743 } 1744 1745 1746 s.close(); 1747 conn.close(); 1748 1749 } 1750 1751 public void testJira95ds(Connection conn, String dbName) throws SQLException 1752 { 1753 System.out.print("\ntesting jira 95 for DataSource"); 1754 EmbeddedDataSource ds = new EmbeddedDataSource(); 1755 ds.setDatabaseName( dbName); 1756 Connection conn1 = ds.getConnection(); 1757 conn1.close(); 1758 } 1759 1760 public void testJira95xads(Connection conn, String dbName) throws SQLException 1761 { 1762 System.out.print("testing jira 95 for XADataSource"); 1763 EmbeddedXADataSource dxs = new EmbeddedXADataSource(); 1764 dxs.setDatabaseName(dbName); 1765 Connection conn2 = dxs.getXAConnection().getConnection(); 1766 conn2.close(); 1767 } 1768 1769 1770} 1771class cdsXid implements Xid , Serializable 1772{ 1773 private static final long serialVersionUID = 64467338100036L; 1774 1775 private final int format_id; 1776 private byte[] global_id; 1777 private byte[] branch_id; 1778 1779 1780 cdsXid(int xid, byte b1, byte b2) 1781 { 1782 format_id = xid; 1783 global_id = new byte[Xid.MAXGTRIDSIZE]; 1784 branch_id = new byte[Xid.MAXBQUALSIZE]; 1785 1786 for (int i = 0; i < global_id.length; i++) { 1787 global_id[i] = b1; 1788 } 1789 1790 for (int i = 0; i < branch_id.length; i++) { 1791 branch_id[i] = b2; 1792 } 1793 } 1794 1795 1801 public int getFormatId() 1802 { 1803 return(format_id); 1804 } 1805 1806 1813 public byte[] getGlobalTransactionId() 1814 { 1815 return(global_id); 1816 } 1817 1818 1824 public byte[] getBranchQualifier() 1825 { 1826 return(branch_id); 1827 } 1828 1829 1830} 1831 1832class EventCatcher implements ConnectionEventListener 1833{ 1834 private final int catcher; 1835 1836 EventCatcher(int which) { 1837 catcher=which; 1838 } 1839 1840 public void connectionClosed(ConnectionEvent event) 1842 { 1843 System.out.print("EVENT("+catcher+"):connectionClosed"); 1844 SQLException sqle = event.getSQLException(); 1845 if (sqle != null) 1846 System.out.print(" SQLSTATE=" + sqle.getSQLState()); 1847 System.out.println(""); 1848 } 1849 1850 public void connectionErrorOccurred(ConnectionEvent event) 1851 { 1852 System.out.print("EVENT("+catcher+"):connectionErrorOccurred"); 1853 SQLException sqle = event.getSQLException(); 1854 if (sqle != null) 1855 System.out.print(" SQLSTATE=" + sqle.getSQLState()); 1856 System.out.println(""); 1857 1858 } 1859 1860} 1861 | Popular Tags |