📄 checkdatasource.java
字号:
/* Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.checkDataSource Copyright 2002, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyTesting.functionTests.tests.jdbcapi;import java.io.Serializable;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Hashtable;import java.util.Iterator;import java.util.Properties;import javax.sql.ConnectionEvent;import javax.sql.ConnectionEventListener;import javax.sql.ConnectionPoolDataSource;import javax.sql.DataSource;import javax.sql.PooledConnection;import javax.sql.XAConnection;import javax.sql.XADataSource;import javax.transaction.xa.XAException;import javax.transaction.xa.XAResource;import javax.transaction.xa.Xid;import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;import org.apache.derby.jdbc.EmbeddedDataSource;import org.apache.derby.jdbc.EmbeddedXADataSource;import org.apache.derby.tools.JDBCDisplayUtil;import org.apache.derby.tools.ij;import org.apache.derbyTesting.functionTests.util.TestUtil;public class checkDataSource{ // Only test connection toString values for embedded. // Client connection toString values are not correlated at this time and just // use default toString // These tests are exempted from other frameworks private boolean testConnectionToString = TestUtil.isEmbeddedFramework(); // Only embedded supports SimpleDataSource (JSR169). // These tests are exempted from other frameworks private boolean testSimpleDataSource = TestUtil.isEmbeddedFramework(); // DERBY-1183 getCursorName not correct after first statement execution private static boolean hasGetCursorNameBug = TestUtil.isDerbyNetClientFramework(); // DERBY-1326 - Network server may abandon sessions when Derby system is shutdown // and this causes intermittent hangs in the client private static boolean hangAfterSystemShutdown = TestUtil.isDerbyNetClientFramework(); /** * A hashtable of opened connections. This is used when checking to * make sure connection strings are unique; we need to make sure all * the connections are closed when we are done, so they are stored * in this hashtable */ protected static Hashtable conns = new Hashtable(); /** The expected format of a connection string. In English: * "<classname>@<hashcode> (XID=<xid>), (SESSION = <sessionid>), * (DATABASE=<dbname>), (DRDAID = <drdaid>)" */ private static final String CONNSTRING_FORMAT = "\\S+@[0-9]+ " + "\\(XID = .*\\), \\(SESSIONID = [0-9]+\\), " + "\\(DATABASE = [A-Za-z]+\\), \\(DRDAID = .+\\)"; public static void main(String[] args) throws Exception { try { new checkDataSource().runTest(args); } catch ( Exception e ) { e.printStackTrace(); throw e; } System.out.println("Completed checkDataSource"); } public checkDataSource() { } protected void runTest(String[] args) throws Exception { // Check the returned type of the JDBC Connections. ij.getPropertyArg(args); Connection dmc = ij.startJBMS(); dmc.createStatement().executeUpdate("create table y(i int)"); dmc.createStatement().executeUpdate( "create procedure checkConn2(in dsname varchar(20)) " + "parameter style java language java modifies SQL DATA " + "external name 'org.apache.derbyTesting.functionTests.tests.jdbcapi." + this.getNestedMethodName() + "'"); CallableStatement cs = dmc.prepareCall("call checkConn2(?)"); cs.setString(1,"Nested"); cs.execute(); checkConnection("DriverManager ", dmc); if (testConnectionToString) checkJBMSToString(); Properties attrs = new Properties(); attrs.setProperty("databaseName", "wombat"); DataSource dscs = TestUtil.getDataSource(attrs); if (testConnectionToString) checkToString(dscs); DataSource ds = dscs; checkConnection("DataSource", ds.getConnection()); DataSource dssimple = null; if (testSimpleDataSource) { dssimple = TestUtil.getSimpleDataSource(attrs); ds = dssimple; checkConnection("SimpleDataSource", ds.getConnection()); } ConnectionPoolDataSource dsp = TestUtil.getConnectionPoolDataSource(attrs); if (testConnectionToString) checkToString(dsp); PooledConnection pc = dsp.getPooledConnection(); pc.addConnectionEventListener(new EventCatcher(1)); checkConnection("ConnectionPoolDataSource", pc.getConnection()); checkConnection("ConnectionPoolDataSource", pc.getConnection()); // BUG 4471 - check outstanding updates are rolled back. Connection c1 = pc.getConnection(); Statement s = c1.createStatement(); s.executeUpdate("create table t (i int)"); s.executeUpdate("insert into t values(1)"); c1.setAutoCommit(false); // this update should be rolled back s.executeUpdate("insert into t values(2)"); c1 = pc.getConnection(); ResultSet rs = c1.createStatement().executeQuery("select count(*) from t"); rs.next(); int count = rs.getInt(1); System.out.println(count == 1 ? "Changes rolled back OK in auto closed pooled connection" : ("FAIL changes committed in in auto closed pooled connection - " + count)); c1.close(); // check connection objects are closed once connection is closed try { rs.next(); System.out.println("FAIL - ResultSet is open for a closed connection obtained from PooledConnection"); } catch (SQLException sqle) { System.out.println("expected " + sqle.toString()); } try { s.executeUpdate("update t set i = 1"); System.out.println("FAIL - Statement is open for a closed connection obtained from PooledConnection"); } catch (SQLException sqle) { System.out.println("expected " + sqle.toString()); } pc.close(); pc = null; testPoolReset("ConnectionPoolDataSource", dsp.getPooledConnection()); XADataSource dsx = TestUtil.getXADataSource(attrs); if (testConnectionToString) checkToString(dsx); XAConnection xac = dsx.getXAConnection(); xac.addConnectionEventListener(new EventCatcher(3)); checkConnection("XADataSource", xac.getConnection()); // BUG 4471 - check outstanding updates are rolled back wi XAConnection. c1 = xac.getConnection(); s = c1.createStatement(); s.executeUpdate("insert into t values(1)"); c1.setAutoCommit(false); // this update should be rolled back s.executeUpdate("insert into t values(2)"); c1 = xac.getConnection(); rs = c1.createStatement().executeQuery("select count(*) from t"); rs.next(); count = rs.getInt(1); rs.close(); System.out.println(count == 2 ? "Changes rolled back OK in auto closed local XAConnection" : ("FAIL changes committed in in auto closed pooled connection - " + count)); c1.close(); xac.close(); xac = null; testPoolReset("XADataSource", dsx.getXAConnection()); // DERBY-1326 - hang in client after Derby system shutdown if(! hangAfterSystemShutdown) { try { TestUtil.getConnection("","shutdown=true"); } catch (SQLException sqle) { JDBCDisplayUtil.ShowSQLException(System.out, sqle); } } dmc = ij.startJBMS(); cs = dmc.prepareCall("call checkConn2(?)"); cs.setString(1,"Nested"); cs.execute(); checkConnection("DriverManager ", dmc); // reset ds back to the Regular DataSource ds = dscs; checkConnection("DataSource", ds.getConnection()); // and back to EmbeddedSimpleDataSource if(TestUtil.isEmbeddedFramework()) { // JSR169 (SimpleDataSource) is only available on embedded. ds = dssimple; checkConnection("EmbeddedSimpleDataSource", dssimple.getConnection()); } pc = dsp.getPooledConnection(); pc.addConnectionEventListener(new EventCatcher(2)); checkConnection("ConnectionPoolDataSource", pc.getConnection()); checkConnection("ConnectionPoolDataSource", pc.getConnection()); // test "local" XAConnections xac = dsx.getXAConnection(); xac.addConnectionEventListener(new EventCatcher(4)); checkConnection("XADataSource", xac.getConnection()); checkConnection("XADataSource", xac.getConnection()); xac.close(); // test "global" XAConnections xac = dsx.getXAConnection(); xac.addConnectionEventListener(new EventCatcher(5)); XAResource xar = xac.getXAResource(); Xid xid = new cdsXid(1, (byte) 35, (byte) 47); xar.start(xid, XAResource.TMNOFLAGS); Connection xacc = xac.getConnection(); xacc.close(); checkConnection("Global XADataSource", xac.getConnection()); checkConnection("Global XADataSource", xac.getConnection()); xar.end(xid, XAResource.TMSUCCESS); checkConnection("Switch to local XADataSource", xac.getConnection()); checkConnection("Switch to local XADataSource", xac.getConnection()); Connection backtoGlobal = xac.getConnection(); xar.start(xid, XAResource.TMJOIN); checkConnection("Switch to global XADataSource", backtoGlobal); checkConnection("Switch to global XADataSource", xac.getConnection()); xar.end(xid, XAResource.TMSUCCESS); xar.commit(xid, true); xac.close(); // now some explicit tests for how connection state behaves // when switching between global transactions and local // and setting connection state. // some of this is already tested in simpleDataSource and checkDataSource // but I want to make sure I cover all situations. (djd) xac = dsx.getXAConnection(); xac.addConnectionEventListener(new EventCatcher(6)); xar = xac.getXAResource(); xid = new cdsXid(1, (byte) 93, (byte) 103); // series 1 - Single connection object Connection cs1 = xac.getConnection(); printState("initial local", cs1); xar.start(xid, XAResource.TMNOFLAGS); printState("initial X1", cs1); cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); cs1.setReadOnly(true); setHoldability(cs1, false); printState("modified X1", cs1); xar.end(xid, XAResource.TMSUCCESS); // the underlying local transaction/connection must pick up the // state of the Connection handle cs1 printState("modified local", cs1); cs1.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); cs1.setReadOnly(false); setHoldability(cs1, false); printState("reset local", cs1); // now re-join the transaction, should pick up the read-only // and isolation level from the transaction, // holdability remains that of this handle. xar.start(xid, XAResource.TMJOIN); printState("re-join X1", cs1); xar.end(xid, XAResource.TMSUCCESS); // should be the same as the reset local printState("back to local (same as reset)", cs1); // test suspend/resume // now re-join the transaction, should pick up the read-only // and isolation level from the transaction, // holdability remains that of this handle. xar.start(xid, XAResource.TMJOIN); printState("re-join X1 second time", cs1); xar.end(xid, XAResource.TMSUSPEND); printState("local after suspend", cs1); xar.start(xid, XAResource.TMRESUME); printState("resume X1", cs1); xar.end(xid, XAResource.TMSUCCESS); printState("back to local (second time)", cs1); cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); cs1.setReadOnly(true); setHoldability(cs1, true); cs1.close(); cs1 = xac.getConnection(); printState("new handle - local ", cs1); cs1.close(); xar.start(xid, XAResource.TMJOIN); cs1 = xac.getConnection(); printState("re-join with new handle X1", cs1); cs1.close(); xar.end(xid, XAResource.TMSUCCESS); // now get a connection (attached to a local) // attach to the global and commit it. // state should be that of the local after the commit. cs1 = xac.getConnection(); cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); printState("pre-X1 commit - local", cs1); xar.start(xid, XAResource.TMJOIN); printState("pre-X1 commit - X1", cs1); xar.end(xid, XAResource.TMSUCCESS); printState("post-X1 end - local", cs1); xar.commit(xid, true); printState("post-X1 commit - local", cs1); cs1.close(); //Derby-421 Setting isolation level with SQL was not getting handled correctly System.out.println("Some more isolation testing using SQL and JDBC api"); cs1 = xac.getConnection(); s = cs1.createStatement(); printState("initial local", cs1); System.out.println("Issue setTransactionIsolation in local transaction"); cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); printState("setTransactionIsolation in local", cs1); testSetIsolationWithStatement(s, xar, cs1); // now check re-use of *Statement objects across local/global connections. System.out.println("TESTING RE_USE OF STATEMENT OBJECTS"); cs1 = xac.getConnection(); // ensure read locsk stay around until end-of transaction cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); cs1.setAutoCommit(false); checkLocks(cs1); Statement sru1 = cs1.createStatement(); sru1.setCursorName("SN1"); sru1.executeUpdate("create table ru(i int)"); sru1.executeUpdate("insert into ru values 1,2,3"); Statement sruBatch = cs1.createStatement(); Statement sruState = createFloatStatementForStateChecking(cs1); PreparedStatement psruState = createFloatStatementForStateChecking(cs1, "select i from ru where i = ?"); CallableStatement csruState = createFloatCallForStateChecking(cs1, "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?,?)");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -