use of javax.sql.XAConnection in project aries by apache.
the class TransactionLogTest method testRequiredRecoverable.
@Test
public void testRequiredRecoverable() throws Exception {
XAConnection xaConn = dataSource.getXAConnection();
try {
txControl.required(() -> {
txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), "foo");
Connection conn = xaConn.getConnection();
return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
});
} finally {
xaConn.close();
}
try (Connection conn = dataSource.getConnection()) {
ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
assertEquals("Hello World!", rs.getString(1));
}
}
use of javax.sql.XAConnection in project aries by apache.
the class TransactionLogTest method testRequiredRecoveryRequiredPrePrepare.
@Test
public void testRequiredRecoveryRequiredPrePrepare() throws Exception {
doRecoveryRequired((good, poison) -> {
txControl.getCurrentContext().registerXAResource(poison, null);
txControl.getCurrentContext().registerXAResource(good, "foo");
}, TransactionStatus.ROLLED_BACK);
boolean success = false;
XAConnection conn = dataSource.getXAConnection();
for (int i = 0; i < 5; i++) {
if (conn.getXAResource().recover(XAResource.TMSTARTRSCAN).length == 0) {
success = true;
break;
} else {
// Wait for recovery to happen!
Thread.sleep(500);
}
}
assertTrue("No recovery in time", success);
}
use of javax.sql.XAConnection in project aries by apache.
the class TransactionLogTest method testRequired2PCNoRecovery.
@Test
public void testRequired2PCNoRecovery() throws Exception {
XAConnection xaConn = dataSource.getXAConnection();
XAConnection xaConn2 = dataSource.getXAConnection();
try {
txControl.required(() -> {
txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), null);
txControl.getCurrentContext().registerXAResource(xaConn2.getXAResource(), null);
Connection conn = xaConn.getConnection();
// conn.setAutoCommit(false);
Connection conn2 = xaConn2.getConnection();
conn2.setAutoCommit(false);
conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
return conn2.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World 2!' )");
});
} finally {
xaConn.close();
}
try (Connection conn = dataSource.getConnection()) {
ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE order by message DESC");
rs.next();
assertEquals("Hello World!", rs.getString(1));
rs.next();
assertEquals("Hello World 2!", rs.getString(1));
assertFalse(rs.next());
}
}
use of javax.sql.XAConnection in project geode by apache.
the class GemFireTransactionDataSource method connectionClosed.
/**
* Implementation of call back function from ConnectionEventListener interface. This callback will
* be invoked on connection close event.
*
* @param event Connection event object
*/
public void connectionClosed(ConnectionEvent event) {
if (isActive) {
try {
XAConnection conn = (XAConnection) event.getSource();
XAResource xar = (XAResource) xaResourcesMap.get(conn);
xaResourcesMap.remove(conn);
Transaction txn = transManager.getTransaction();
if (txn != null && xar != null)
txn.delistResource(xar, XAResource.TMSUCCESS);
provider.returnConnection(conn);
} catch (Exception e) {
String exception = "GemFireTransactionDataSource::connectionClosed: Exception occurred due to " + e;
if (logger.isDebugEnabled()) {
logger.debug(exception, e);
}
}
}
}
use of javax.sql.XAConnection in project geode by apache.
the class AbstractPoolCacheJUnitTest method testEffectOfBlockingTimeoutOnXAConnection.
/**
* Tests if an XAresource obtained from an XAConnection which is already closed , can return null
* or not.
*/
@Ignore("TODO: test used to eat its own exception and it fails")
@Test
public void testEffectOfBlockingTimeoutOnXAConnection() throws Exception {
Map map = new HashMap();
map.put("init-pool-size", "2");
map.put("jndi-name", "TestXAPooledDataSource");
map.put("max-pool-size", "7");
map.put("idle-timeout-seconds", "20");
map.put("blocking-timeout-seconds", "2");
map.put("login-timeout-seconds", "5");
// map.put("xa-datasource-class","org.apache.derby.jdbc.EmbeddedXADataSource");
map.put("jdbc-driver-class", "org.apache.derby.jdbc.EmbeddedDriver");
map.put("user-name", "mitul");
map.put("password", "83f0069202c571faf1ae6c42b4ad46030e4e31c17409e19a");
map.put("connection-url", "jdbc:derby:newDB;create=true");
List props = new ArrayList();
props.add(new ConfigProperty("databaseName", "newDB", "java.lang.String"));
GemFireBasicDataSource gbds = (GemFireBasicDataSource) DataSourceFactory.getSimpleDataSource(map, props);
map.put("xa-datasource-class", "org.apache.derby.jdbc.EmbeddedXADataSource");
map.put("connection-url", "jdbc:derby:newDB;create=true");
GemFireTransactionDataSource gtds = (GemFireTransactionDataSource) DataSourceFactory.getTranxDataSource(map, props);
XAConnection xaconn = (XAConnection) gtds.provider.borrowConnection();
try {
Thread.sleep(4);
} catch (InterruptedException e) {
fail("interrupted");
}
for (int i = 0; i < 1000; ++i) {
XAResource xar = xaconn.getXAResource();
System.out.println("XAResource=" + xar);
assertNotNull(xar);
}
}
Aggregations