use of javax.sql.XAConnection in project derby by apache.
the class InternationalConnectTest method testXADSConnect.
/**
* Test XA Connection for chinese database name, user and password.
* @throws SQLException
*/
public void testXADSConnect() throws SQLException {
// Test chinese database name.
XADataSource ds = J2EEDataSource.getXADataSource();
J2EEDataSource.setBeanProperty(ds, "databaseName", "\u4e10");
J2EEDataSource.setBeanProperty(ds, "createDatabase", "create");
XAConnection xaconn = ds.getXAConnection();
Connection conn = xaconn.getConnection();
conn.close();
xaconn.close();
// Chinese user
J2EEDataSource.setBeanProperty(ds, "user", "\u4e10");
xaconn = ds.getXAConnection();
conn = xaconn.getConnection();
conn.close();
xaconn.close();
// Chinese password
J2EEDataSource.setBeanProperty(ds, "password", "\u4e10");
xaconn = ds.getXAConnection();
conn = xaconn.getConnection();
conn.close();
xaconn.close();
/* Add the created database for cleanup by tearDown() */
databasesForCleanup.add("\u4e10");
}
use of javax.sql.XAConnection in project derby by apache.
the class ClientSideSystemPropertiesTest method testClientXADataSourceConnection.
public void testClientXADataSourceConnection() throws Exception {
XAConnection xaconn = J2EEDataSource.getXADataSource().getXAConnection();
Connection conn = xaconn.getConnection();
conn.setAutoCommit(false);
checkTraceFileIsPresent();
conn.close();
xaconn.close();
}
use of javax.sql.XAConnection in project derby by apache.
the class ConstraintCharacteristicsTest method testXA.
public void testXA() throws SQLException, XAException {
final XADataSource xads = J2EEDataSource.getXADataSource();
J2EEDataSource.setBeanProperty(xads, "databaseName", "wombat");
// loop iteration 0
final int UNIQUE_PK = 0;
// loop iteration 1
final int CHECK = 1;
final String[] expectedError = { LANG_DEFERRED_DUP_VIOLATION_T, LANG_DEFERRED_CHECK_VIOLATION_T };
for (int i = UNIQUE_PK; i <= CHECK; i++) {
final XAConnection xaconn = xads.getXAConnection();
try {
final XAResource xar = xaconn.getXAResource();
final Connection conn = xaconn.getConnection();
conn.setAutoCommit(false);
final Statement s = conn.createStatement();
//
// Do XA rollback when we have a violation; expect normal
// operation.
//
Xid xid = (i == UNIQUE_PK) ? doXAWorkUniquePK(s, xar) : doXAWorkCheck(s, xar);
xar.rollback(xid);
assertXidRolledBack(xar, xid);
//
// Do an XA prepare when we have a violation; expect exception
// and rollback.
//
xid = (i == UNIQUE_PK) ? doXAWorkUniquePK(s, xar) : doXAWorkCheck(s, xar);
try {
xar.prepare(xid);
fail("Expected XA prepare to fail due to " + "constraint violation");
} catch (XAException xe) {
assertEquals(XAException.XA_RBINTEGRITY, xe.errorCode);
if (!usingDerbyNetClient()) {
Throwable t = xe.getCause();
assertTrue(t != null && t instanceof SQLException);
assertSQLState(expectedError[i], (SQLException) t);
}
assertXidRolledBack(xar, xid);
}
//
// Do XA commit (1PC, no prepare) when we have a violation;
// expect exception and rollback.
//
xid = (i == UNIQUE_PK) ? doXAWorkUniquePK(s, xar) : doXAWorkCheck(s, xar);
try {
xar.commit(xid, true);
fail("Expected XA commit to fail due to " + "constraint violation");
} catch (XAException xe) {
if (xe.errorCode == -3) {
System.err.println("huff");
} else {
assertEquals(XAException.XA_RBINTEGRITY, xe.errorCode);
if (!usingDerbyNetClient()) {
Throwable t = xe.getCause();
assertTrue(t != null && t instanceof SQLException);
assertSQLState(expectedError[i], (SQLException) t);
}
}
assertXidRolledBack(xar, xid);
}
} finally {
if (usingDerbyNetClient()) {
xaconn.getConnection().rollback();
}
xaconn.close();
}
}
}
use of javax.sql.XAConnection in project derby by apache.
the class XATest method testNoTransaction.
/**
* Tests on INIT STATE (no tr Original SQL from xaStateTran.sql. <code>
*
* -- the following should error XAER_NOTA
* xa_start xa_join 11;
* -- the following should error XAER_NOTA
* xa_start xa_resume 11;
* -- the following should error XAER_NOTA
* xa_end xa_success 11;
* -- the following should error XAER_NOTA
* xa_end xa_fail 11;
* -- the following should error XAER_NOTA
* xa_end xa_suspend 11;
* -- the following should error XAER_NOTA
* xa_prepare 11;
* -- the following should error XAER_NOTA
* xa_commit xa_1phase 11;
* -- the following should error XAER_NOTA
* xa_commit xa_2phase 11;
* -- the following should error XAER_NOTA
* xa_rollback 11;
* -- the following should error XAER_NOTA
* xa_forget 11;
* </code>
*/
public void testNoTransaction() throws SQLException, XAException {
XADataSource xads = J2EEDataSource.getXADataSource();
XAConnection xac = xads.getXAConnection();
XAResource xar = xac.getXAResource();
Xid xid11 = XATestUtil.getXid(11, 3, 128);
try {
xar.start(xid11, XAResource.TMJOIN);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.start(xid11, XAResource.TMRESUME);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.end(xid11, XAResource.TMSUCCESS);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.end(xid11, XAResource.TMFAIL);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.end(xid11, XAResource.TMSUSPEND);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.prepare(xid11);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.commit(xid11, false);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.commit(xid11, true);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.rollback(xid11);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
try {
xar.forget(xid11);
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}
}
use of javax.sql.XAConnection in project derby by apache.
the class XATest method testMorph.
/**
* Morph a connection between local anf global transactions.
*/
public void testMorph() throws SQLException, XAException {
Statement preStatement = getConnection().createStatement();
preStatement.execute("create table fooMorph (a int)");
preStatement.executeUpdate("insert into APP.fooMorph values (0)");
preStatement.executeUpdate("insert into APP.fooMorph values (1)");
preStatement.executeUpdate("insert into APP.fooMorph values (2)");
preStatement.executeUpdate("insert into APP.fooMorph values (3)");
preStatement.executeUpdate("insert into APP.fooMorph values (4)");
preStatement.close();
XADataSource xads = J2EEDataSource.getXADataSource();
XAConnection xac = xads.getXAConnection();
XAResource xar = xac.getXAResource();
Connection conn = xac.getConnection();
/*
* autocommit off; insert into foo values (1); select * from
* global_xactTable where gxid is not null order by gxid,username;
* commit;
*/
conn.setAutoCommit(false);
Statement s = conn.createStatement();
s.executeUpdate("insert into APP.fooMorph values (2001)");
// no rows expected
XATestUtil.checkXATransactionView(conn, null);
conn.commit();
/*
* autocommit on; insert into foo values (2); select * from
* global_xactTable where gxid is not null order by gxid,username;
*
*/
conn.setAutoCommit(true);
s.executeUpdate("insert into APP.fooMorph values (2002)");
XATestUtil.checkXATransactionView(conn, null);
/*
* -- morph the connection to a global transaction xa_start xa_noflags
* 1; select * from global_xactTable where gxid is not null order by
* gxid,username; insert into foo values (3);
*/
Xid xid = XATestUtil.getXid(1001, 66, 13);
xar.start(xid, XAResource.TMNOFLAGS);
String[][] expectedRows = { { "(1", "IDLE", "NULL", "APP", "UserTransaction" } };
XATestUtil.checkXATransactionView(conn, expectedRows);
s.executeUpdate("insert into APP.fooMorph values (2003)");
/*
* -- disallowed commit; -- disallowed rollback; -- disallowed
* autocommit on; -- OK autocommit off;
*/
try {
conn.commit();
fail("FAIL: commit allowed in global xact");
} catch (SQLException e) {
}
try {
conn.rollback();
fail("FAIL: roll back allowed in global xact");
} catch (SQLException e) {
}
try {
conn.setAutoCommit(true);
fail("FAIL: setAutoCommit(true) allowed " + "in global xact");
} catch (SQLException e) {
}
try {
conn.setSavepoint();
fail("FAIL: setSavepoint() allowed in global xact");
} catch (SQLException e) {
}
try {
conn.setSavepoint("badsavepoint");
fail("FAIL: setSavepoint(String) allowed in " + "global xact");
} catch (SQLException e) {
}
conn.setAutoCommit(false);
// s was created in local mode so it has holdibilty
// set, will execute but ResultSet will have close on commit
// DERBY-1158 query with holdable statement
s.executeQuery("select * from APP.fooMorph where A >= 2000").close();
s.close();
// statement created in global xact is CLOSE_CURSORS_AT_COMMIT
s = conn.createStatement();
assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, s.getResultSetHoldability());
/*
* select * from foo; xa_end xa_success 1; xa_prepare 1;
*/
ResultSet rs = s.executeQuery("select * from APP.fooMorph where A >= 2000");
expectedRows = new String[][] { { "2001" }, { "2002" }, { "2003" } };
rs.close();
xar.end(xid, XAResource.TMSUCCESS);
xar.prepare(xid);
/*
* -- dup id xa_start xa_noflags 1;
*/
try {
xar.start(xid, XAResource.TMNOFLAGS);
fail("FAIL - start with duplicate XID");
} catch (XAException e) {
if (e.errorCode != XAException.XAER_DUPID)
throw e;
}
/*
* xa_start xa_noflags 2; -- still should disallow autommit; autocommit
* on; -- still should disallow commit and rollback commit; rollback;
* select * from global_xactTable where gxid is not null order by
* gxid,username; xa_end xa_suspend 2;
*/
Xid xid2 = XATestUtil.getXid(1002, 23, 3);
xar.start(xid2, XAResource.TMNOFLAGS);
try {
conn.commit();
fail("FAIL: commit allowed in global xact");
} catch (SQLException e) {
}
try {
conn.rollback();
fail("FAIL: roll back allowed in global xact");
} catch (SQLException e) {
}
try {
conn.setAutoCommit(true);
fail("FAIL: setAutoCommit(true) allowed in global xact");
} catch (SQLException e) {
}
conn.setAutoCommit(false);
xar.end(xid2, XAResource.TMSUSPEND);
/*
* -- get local connection again xa_getconnection;
*
* insert into foo values (5); -- autocommit should be on by default;
* commit;
*
* autocommit off; insert into foo values (6); -- commit and rollback is
* allowed on local connection rollback;
*
* insert into foo values (6); commit;
*/
conn = xac.getConnection();
s = conn.createStatement();
s.executeUpdate("insert into APP.fooMorph values (2005)");
conn.commit();
conn.setAutoCommit(false);
s.executeUpdate("insert into APP.fooMorph values (2006)");
conn.rollback();
s.executeUpdate("insert into APP.fooMorph values (2007)");
conn.commit();
expectedRows = new String[][] { { "(1", "PREPARED", "false", "APP", "UserTransaction" }, { "(1", "IDLE", "NULL", "APP", "UserTransaction" } };
XATestUtil.checkXATransactionView(conn, expectedRows);
/*
* -- I am still able to commit other global transactions while I am
* attached to a -- local transaction. xa_commit xa_2phase 1; xa_end
* xa_success 2; xa_rollback 2;
*/
xar.commit(xid, false);
xar.end(xid2, XAResource.TMSUCCESS);
xar.rollback(xid2);
XATestUtil.checkXATransactionView(conn, null);
rs = s.executeQuery("select * from APP.fooMorph where A >= 2000");
expectedRows = new String[][] { { "2001" }, { "2002" }, { "2003" }, { "2005" }, { "2007" } };
JDBC.assertFullResultSet(rs, expectedRows);
rs.close();
conn.rollback();
conn.close();
/*
* xa_getconnection; select * from global_xactTable where gxid is not
* null order by gxid,username; select * from foo; autocommit off;
* delete from foo;
*/
conn = xac.getConnection();
conn.setAutoCommit(false);
s = conn.createStatement();
s.executeUpdate("delete from app.fooMorph");
rs = s.executeQuery("select * from APP.fooMorph");
JDBC.assertEmpty(rs);
rs.close();
/*
* -- yanking a local connection away should rollback the changes
*/
conn = xac.getConnection();
conn.setAutoCommit(false);
s = conn.createStatement();
rs = s.executeQuery("select * from APP.fooMorph where A >= 2000");
expectedRows = new String[][] { { "2001" }, { "2002" }, { "2003" }, { "2005" }, { "2007" } };
JDBC.assertFullResultSet(rs, expectedRows);
/*
* -- cannot morph it if the local transaction is not idle xa_start
* xa_noflags 3; commit; -- now morph it to a global transaction
* xa_start xa_noflags 3;
*/
Xid xid3 = XATestUtil.getXid(1003, 27, 9);
try {
xar.start(xid3, XAResource.TMNOFLAGS);
fail("FAIL XAResource.start on a global transaction with an active local transaction (autocommit false)");
} catch (XAException xae) {
if (xae.errorCode != XAException.XAER_OUTSIDE)
throw xae;
}
conn.commit();
xar.start(xid3, XAResource.TMNOFLAGS);
// DERBY-341 - client skip XAConnection with active local xact
if (usingEmbedded()) {
try {
xac.getConnection();
fail("FAIL: getConnection with active global xact");
} catch (SQLException sqle) {
assertSQLState("XJ059", sqle);
}
}
/*
* select * from foo; delete from foo;
*
* xa_end xa_fail 3; xa_rollback 3; -- local connection again
* xa_getconnection; select * from global_xactTable where gxid is not
* null order by gxid,username; select * from foo;
*/
s = conn.createStatement();
s.executeUpdate("delete from APP.fooMorph");
rs = s.executeQuery("select * from APP.fooMorph where A >= 2000");
JDBC.assertEmpty(rs);
rs.close();
try {
xar.end(xid3, XAResource.TMFAIL);
} catch (XAException e) {
if (e.errorCode != XAException.XA_RBROLLBACK)
throw e;
}
xar.rollback(xid3);
conn = xac.getConnection();
s = conn.createStatement();
rs = s.executeQuery("select * from APP.fooMorph where A >= 2000");
expectedRows = new String[][] { { "2001" }, { "2002" }, { "2003" }, { "2005" }, { "2007" } };
JDBC.assertFullResultSet(rs, expectedRows);
rs.close();
s.close();
conn.close();
}
Aggregations