use of javax.sql.XAConnection in project derby by apache.
the class AbortTest method xa.
private void xa() throws Exception {
XADataSource xads = J2EEDataSource.getXADataSource();
XAConnection conn0 = getXAConnection(xads, "user6");
XAConnection conn1 = getXAConnection(xads, "user7");
XAConnection conn2 = getXAConnection(xads, "user8");
vet(conn0.getConnection(), conn1.getConnection(), conn2.getConnection());
}
use of javax.sql.XAConnection in project derby by apache.
the class cdsXid method testReuseAcrossGlobalLocal.
// This test includes some short-hand descriptions of the test cases
// left in for reference to the original non-junit test
public void testReuseAcrossGlobalLocal() throws SQLException, XAException {
// on tearDown. Embedded requires a database shutdown
if (usingDerbyNetClient())
return;
int[] onetwothree = { 1, 2, 3 };
int[] three = { 3 };
// expected parameter count for prepared statements
int[] pspc = { 1, 4 };
// for callable statements
int[] cspc = { 2, 12, 12 };
// statics for testReuseAcrossGlobalLocal
int[] StatementExpectedValues = { ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.FETCH_REVERSE, 444, 713, 19, ResultSet.HOLD_CURSORS_OVER_COMMIT };
// ResultSet.CLOSE_CURSORS_AT_COMMIT};
int[] PreparedStatementExpectedValues = { ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.FETCH_REVERSE, 888, 317, 91, ResultSet.HOLD_CURSORS_OVER_COMMIT };
int[] CallableStatementExpectedValues = { ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.FETCH_REVERSE, 999, 137, 85, ResultSet.HOLD_CURSORS_OVER_COMMIT };
XADataSource dsx = J2EEDataSource.getXADataSource();
XAConnection xac = dsx.getXAConnection();
AssertEventCatcher aes6 = new AssertEventCatcher(6);
xac.addConnectionEventListener(aes6);
XAResource xar = xac.getXAResource();
Xid xid = new cdsXid(1, (byte) 103, (byte) 119);
// now check re-use of *Statement objects across local/global
// connections.
Connection cs1 = xac.getConnection();
// ensure read locks stay around until end-of transaction
cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
cs1.setAutoCommit(false);
assertLocks(null, cs1);
Statement sru1 = cs1.createStatement();
sru1.setCursorName("SN1");
sru1.executeUpdate("insert into intTable values 1,2,3");
Statement sruBatch = cs1.createStatement();
sruBatch.setCursorName("sruBatch");
Statement sruState = createFloatStatementForStateChecking(StatementExpectedValues, cs1);
PreparedStatement psruState = createFloatStatementForStateChecking(new int[] { 1, 4 }, PreparedStatementExpectedValues, cs1, "select i from intTable where i = ?");
CallableStatement csruState = createFloatCallForStateChecking(new int[] { 2, 12, 12 }, CallableStatementExpectedValues, cs1, "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?,?)");
PreparedStatement psParams = cs1.prepareStatement("select * from intTable where i > ?");
psParams.setCursorName("params");
psParams.setInt(1, 2);
// Params-local-1
resultSetQuery("params", three, psParams.executeQuery());
sruBatch.addBatch("insert into intTable values 4");
// sru1-local-1
queryOnStatement("SN1", onetwothree, cs1, sru1);
// need to commit to switch to an global connection;
cs1.commit();
// simple case - underlying connection is re-used for global.
xar.start(xid, XAResource.TMNOFLAGS);
// Expecting downgrade because global transaction sru1-global-2 is
// using a statement with holdability true
// sru1-global-2
queryOnStatement("SN1", onetwothree, cs1, sru1);
sruBatch.addBatch("insert into intTable values 5");
Statement sru2 = cs1.createStatement();
sru2.setCursorName("OAK2");
// sru2-global-3
queryOnStatement("OAK2", onetwothree, cs1, sru2);
// Expecting downgrade because global transaction sru1-global-4 is
// using a statement with holdability true
// sru1-global-4
queryOnStatement("SN1", onetwothree, cs1, sru1);
// Global statement
StatementExpectedValues[6] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
PreparedStatementExpectedValues[6] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
CallableStatementExpectedValues[6] = ResultSet.CLOSE_CURSORS_AT_COMMIT;
assertStatementState(null, StatementExpectedValues, sruState);
// Global PreparedStatement
assertStatementState(pspc, PreparedStatementExpectedValues, psruState);
// Global CallableStatement
assertStatementState(cspc, CallableStatementExpectedValues, csruState);
// Params-global-1
resultSetQuery("params", three, psParams.executeQuery());
xar.end(xid, XAResource.TMSUCCESS);
// now a new underlying connection is created
// sru1-local-5
queryOnStatement("SN1", onetwothree, cs1, sru1);
// sru2-local-6
queryOnStatement("OAK2", onetwothree, cs1, sru2);
sruBatch.addBatch("insert into intTable values 6,7");
Statement sru3 = cs1.createStatement();
sru3.setCursorName("SF3");
// sru3-local-7
queryOnStatement("SF3", onetwothree, cs1, sru3);
// Two transactions should hold locks (global and the current XA);
// LOCAL
StatementExpectedValues[6] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
PreparedStatementExpectedValues[6] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
CallableStatementExpectedValues[6] = ResultSet.HOLD_CURSORS_OVER_COMMIT;
assertStatementState(null, StatementExpectedValues, sruState);
assertStatementState(pspc, PreparedStatementExpectedValues, psruState);
assertStatementState(cspc, CallableStatementExpectedValues, csruState);
// Params-local-2
resultSetQuery("params", three, psParams.executeQuery());
assertLocks(new int[] { 14, 14 }, cs1);
cs1.commit();
// Confirm - no connection closed event & connection error event
assertFalse(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
// attach the XA transaction to another connection and see what happens
XAConnection xac2 = dsx.getXAConnection();
AssertEventCatcher aes5 = new AssertEventCatcher(5);
xac2.addConnectionEventListener(aes5);
XAResource xar2 = xac2.getXAResource();
xar2.start(xid, XAResource.TMJOIN);
Connection cs2 = xac2.getConnection();
// these statements were generated by cs1 and thus are still
// in a local connection.
// sru1-local-8
queryOnStatement("SN1", onetwothree, cs1, sru1);
// sru2-local-9
queryOnStatement("OAK2", onetwothree, cs1, sru2);
// sru3-local-10
queryOnStatement("SF3", onetwothree, cs1, sru3);
sruBatch.addBatch("insert into intTable values 8");
// LOCAL 2
assertStatementState(null, StatementExpectedValues, sruState);
assertStatementState(pspc, PreparedStatementExpectedValues, psruState);
assertStatementState(cspc, CallableStatementExpectedValues, csruState);
assertLocks(new int[] { 14, 12 }, cs1);
int[] updateCounts = sruBatch.executeBatch();
int[] expectedUpdateCounts = { 1, 1, 2, 1 };
// sruBatch update counts:
for (int i = 0; i < updateCounts.length; i++) {
assertEquals(expectedUpdateCounts[i], updateCounts[i]);
}
// sruBatch
queryOnStatement("sruBatch", new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, cs1, sruBatch);
xar2.end(xid, XAResource.TMSUCCESS);
// Confirm - no connection closed event & connection error event
assertFalse(aes5.didConnectionClosedEventHappen());
assertFalse(aes5.didConnectionErrorEventHappen());
aes5.resetState();
xac2.close();
// allow close on already closed XAConnection
xac2.close();
xac2.addConnectionEventListener(null);
xac2.removeConnectionEventListener(null);
// test methods against a closed XAConnection and its resource
try {
// (DERBY-2532)
xac2.getXAResource();
fail("expected SQLException on closed XAConnection.getXAResource");
} catch (SQLException sqle) {
assertSQLState("08003", sqle);
}
try {
xac2.getConnection();
fail("expected SQLException on XAConnection.getConnection");
} catch (SQLException sqle) {
assertSQLState("08003", sqle);
}
try {
xar2.start(xid, XAResource.TMJOIN);
fail("expected XAException on XAResource.TMJOIN");
} catch (XAException xae) {
assertXAException("XAResource.start", xae);
}
try {
xar2.end(xid, XAResource.TMJOIN);
fail("expected XAException on XAResource.TMJOIN");
} catch (XAException xae) {
assertXAException("XAResource.end", xae);
}
try {
xar2.commit(xid, true);
fail("expected XAException on XAResource.commit");
} catch (XAException xae) {
assertXAException("XAResource.commit", xae);
}
try {
xar2.prepare(xid);
fail("expected XAException on XAResource.prepare");
} catch (XAException xae) {
assertXAException("XAResource.prepare", xae);
}
try {
xar2.recover(0);
fail("expected XAException on XAResource.recover");
} catch (XAException xae) {
assertXAException("XAResource.recover", xae);
}
try {
xar2.prepare(xid);
fail("expected XAException on XAResource.prepare");
} catch (XAException xae) {
assertXAException("XAResource.prepare", xae);
}
try {
xar2.isSameRM(xar2);
fail("expected XAException on XAResource.isSameRM");
} catch (XAException xae) {
assertXAException("XAResource.isSameRM", xae);
}
// close everything
cs1.rollback();
sruState.close();
psruState.close();
csruState.close();
psParams.close();
sruBatch.close();
sru1.close();
sru2.close();
sru3.close();
cs1.close();
cs2.close();
xac.removeConnectionEventListener(null);
xac.close();
xac2.close();
// but, still not enough.
// what with all the switching between global and local transactions
// we still have a lock open on intTable, which will interfere with
// our tearDown efforts. Bounce the database.
TestConfiguration.getCurrent().shutdownDatabase();
}
use of javax.sql.XAConnection in project derby by apache.
the class cdsXid method testGlobalLocalInterleaf.
public void testGlobalLocalInterleaf() throws SQLException, XAException {
// now some explicit tests for how connection state behaves
// when switching between global transactions and local
// and setting connection state.
// some of this may be tested elsewhere too.
XADataSource dsx = J2EEDataSource.getXADataSource();
XAConnection xac = dsx.getXAConnection();
AssertEventCatcher aes6 = new AssertEventCatcher(6);
xac.addConnectionEventListener(aes6);
XAResource xar = xac.getXAResource();
Xid xid = new cdsXid(1, (byte) 93, (byte) 103);
// series 1 - Single connection object
Connection cs1 = xac.getConnection();
// initial local
assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_READ_COMMITTED, true, false, cs1);
xar.start(xid, XAResource.TMNOFLAGS);
// initial X1
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_COMMITTED, false, false, cs1);
cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
cs1.setReadOnly(true);
// close cursors
setHoldability(cs1, false);
// modified X1
boolean ReadOnly = false;
// see DERBY-911, ReadOnly state different for Embedded/DerbyNetClient
if (usingEmbedded())
ReadOnly = true;
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, ReadOnly, cs1);
xar.end(xid, XAResource.TMSUCCESS);
// the underlying local transaction/connection must pick up the
// state of the Connection handle cs1
// modified local:
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, true, ReadOnly, cs1);
cs1.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
cs1.setReadOnly(false);
// close cursors
setHoldability(cs1, false);
// reset local
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_COMMITTED, true, false, 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);
// re-join X1
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, ReadOnly, cs1);
xar.end(xid, XAResource.TMSUCCESS);
// back to local - should be the same as the reset local
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_COMMITTED, true, false, cs1);
// test suspend/resume
// now re-join the transaction (X1) for the second time, should pick
// up the read-only and isolation level from the transaction,
// holdability remains that of this handle.
xar.start(xid, XAResource.TMJOIN);
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, ReadOnly, cs1);
xar.end(xid, XAResource.TMSUSPEND);
// local after suspend
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_COMMITTED, true, false, cs1);
xar.start(xid, XAResource.TMRESUME);
// resume X1
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, ReadOnly, cs1);
xar.end(xid, XAResource.TMSUCCESS);
// back to local (second time)
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_COMMITTED, true, false, cs1);
cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
cs1.setReadOnly(true);
// hold
setHoldability(cs1, true);
// Confirm - no connection closed event & connection error event
assertFalse(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
cs1.close();
// Check if got connection closed event but not connection error event
assertTrue(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
cs1 = xac.getConnection();
// new handle - local
assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_READ_COMMITTED, true, false, cs1);
cs1.close();
// Check if got connection closed event but not connection error event
assertTrue(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
xar.start(xid, XAResource.TMJOIN);
cs1 = xac.getConnection();
// re-join with new handle X1
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, ReadOnly, cs1);
cs1.close();
xar.end(xid, XAResource.TMSUCCESS);
// Check if got connection closed event but not connection error event
assertTrue(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
// 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);
// pre-X1 commit - local
assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, true, false, cs1);
xar.start(xid, XAResource.TMJOIN);
// pre-X1 commit - X1
assertConnectionState(ResultSet.CLOSE_CURSORS_AT_COMMIT, Connection.TRANSACTION_READ_UNCOMMITTED, false, ReadOnly, cs1);
xar.end(xid, XAResource.TMSUCCESS);
// post-X1 end - local
assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, true, false, cs1);
xar.commit(xid, true);
// post-X1 commit - local
assertConnectionState(ResultSet.HOLD_CURSORS_OVER_COMMIT, Connection.TRANSACTION_REPEATABLE_READ, true, false, cs1);
// Confirm - no connection closed event & connection error event
assertFalse(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
cs1.close();
// Check if got connection closed event but not connection error event
assertTrue(aes6.didConnectionClosedEventHappen());
assertFalse(aes6.didConnectionErrorEventHappen());
aes6.resetState();
}
use of javax.sql.XAConnection in project derby by apache.
the class cdsXid method assertToString.
/**
* Check uniqueness of strings for an XA data source
*/
private static void assertToString(XADataSource xds) throws Exception {
int numConnections = 10;
// First get a bunch of pooled connections
// and make sure they're all unique
HashMap<String, XAConnection> xaConns = new HashMap<String, XAConnection>();
for (int i = 0; i < numConnections; i++) {
XAConnection xc = xds.getXAConnection();
assertStringFormat(xc);
String str = xc.toString();
// XA connection toString should be unique
assertNull(xaConns.get(str));
xaConns.put(str, xc);
}
// Now check that connections from each of these
// pooled connections have different string values
Iterator it = xaConns.values().iterator();
clearConnections();
while (it.hasNext()) {
XAConnection xc = (XAConnection) it.next();
Connection conn = xc.getConnection();
assertToString(conn);
}
clearConnections();
// Now clear out the pooled connections
it = xaConns.values().iterator();
while (it.hasNext()) {
XAConnection xc = (XAConnection) it.next();
xc.close();
}
xaConns.clear();
}
use of javax.sql.XAConnection in project derby by apache.
the class cdsXid method testClientTraceFileDSConnectionAttribute.
/**
* Check that traceFile connection attribute functions correctly.
* tracefile was tested in checkDriver, but not for DataSources.
* tracefile= was used in datasourcepermissions_net, but that's
* incorrect syntax. Note that we're not checking the contents of
* the tracefile.
*
* Note also that this test cannot run against a remote server.
* <p>
* This is also a regression test for DERBY-4717.
*
* @throws SQLException
*/
public void testClientTraceFileDSConnectionAttribute() throws SQLException {
String traceFile;
// with ConnectionPoolDataSource
ConnectionPoolDataSource cpds = J2EEDataSource.getConnectionPoolDataSource();
traceFile = "trace3.out";
JDBCDataSource.setBeanProperty(cpds, "connectionAttributes", "traceFile=" + traceFile);
// DERBY-2468 - trace3.out does not get created
((PooledConnection) getPhysicalConnection(cpds)).close();
JDBCDataSource.clearStringBeanProperty(cpds, "connectionAttributes");
traceFile = "trace4.out";
JDBCDataSource.setBeanProperty(cpds, "traceFile", traceFile);
((PooledConnection) getPhysicalConnection(cpds)).close();
cpds = null;
// now with XADataSource
XADataSource xads = J2EEDataSource.getXADataSource();
traceFile = "trace5.out";
JDBCDataSource.setBeanProperty(xads, "connectionAttributes", "traceFile=" + traceFile);
((XAConnection) getPhysicalConnection(xads)).close();
// DERBY-2468 - trace5.out does not get created
JDBCDataSource.clearStringBeanProperty(xads, "connectionAttributes");
traceFile = "trace6.out";
JDBCDataSource.setBeanProperty(xads, "traceFile", traceFile);
((XAConnection) getPhysicalConnection(xads)).close();
assertTraceFilesExistAndCanBeDeleted();
}
Aggregations