use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.
the class FailAfterCommitBase method doTest.
protected void doTest(final DataSource dataSource) throws Exception {
// Test code
Utils.createTables(dataSource.getConnection());
// We can't just instantiate one as we need to be using the
// same one as
// the transaction
// manager would have used to mark the transaction for GC
CommitMarkableResourceRecordRecoveryModule commitMarkableResourceRecoveryModule = null;
Vector recoveryModules = manager.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
commitMarkableResourceRecoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
} else if (m instanceof XARecoveryModule) {
XARecoveryModule xarm = (XARecoveryModule) m;
xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
public boolean initialise(String p) throws Exception {
return true;
}
public XAResource[] getXAResources() throws Exception {
return new XAResource[] { xaResource };
}
});
}
}
}
// final Object o = new Object();
// synchronized (o) {
Thread background = new Thread(new Runnable() {
public void run() {
try {
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
Connection localJDBCConnection = dataSource.getConnection();
localJDBCConnection.setAutoCommit(false);
nonXAResource = new JDBCConnectableResource(localJDBCConnection);
tm.getTransaction().enlistResource(nonXAResource);
xaResource = new SimpleXAResource();
tm.getTransaction().enlistResource(xaResource);
localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
tm.commit();
} catch (ExecuteException t) {
} catch (Exception e) {
e.printStackTrace();
failed = true;
} catch (Error e) {
}
}
});
background.start();
background.join();
assertFalse(failed);
Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
assertNotNull(committed);
// The recovery module has to perform lookups
new InitialContext().rebind("commitmarkableresource", dataSource);
// Check if the item is still in the db
commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
commitMarkableResourceRecoveryModule.periodicWorkSecondPass();
// Now we need to correctly complete the transaction
manager.scan();
// Make sure the item is no longer in the DB, it will need two scans as
// second phase of the CommitMarkableResourceRecoveryModule will have
// executed before the AtomicActionRecoveryModule has been able to GC
// it
assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
manager.scan();
commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
commitMarkableResourceRecoveryModule.periodicWorkSecondPass();
assertTrue(xaResource.wasCommitted());
assertFalse(xaResource.wasRolledback());
// Make sure that the resource was GC'd by the CRRRM
assertFalse(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
}
use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.
the class XATerminatorImpleUnitTest method testXARMERR.
@Test
public void testXARMERR() throws Exception {
Uid uid = new Uid();
XidImple xid = new XidImple(uid);
TransactionImporter imp = SubordinationManager.getTransactionImporter();
SubordinateTransaction subordinateTransaction = imp.importTransaction(xid);
Uid savingUid = getImportedSubordinateTransactionUid(subordinateTransaction);
subordinateTransaction.enlistResource(new TestXAResource() {
@Override
public void commit(Xid xid, boolean b) throws XAException {
this.xid = null;
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public void rollback(Xid xid) throws XAException {
fail("Resource was rolled back");
}
});
subordinateTransaction.enlistResource(new TestXAResource() {
@Override
public void commit(Xid xid, boolean b) throws XAException {
throw new XAException(XAException.XA_HEURHAZ);
}
@Override
public int prepare(Xid xid) throws XAException {
failedResourceXid = xid;
return 0;
}
@Override
public void rollback(Xid xid) throws XAException {
fail("Resource was rolled back");
}
});
XATerminatorImple xa = new XATerminatorImple();
xa.prepare(xid);
try {
xa.commit(xid, false);
fail("Expecting heuristic mixed exception being thrown on commit");
} catch (final XAException ex) {
assertEquals(XAException.XA_HEURMIX, ex.errorCode);
}
try {
xa.commit(xid, false);
} catch (XAException e) {
assertEquals(XAException.XA_RETRY, e.errorCode);
}
ObjStoreBrowser osb = new ObjStoreBrowser();
osb.viewSubordinateAtomicActions(true);
osb.setExposeAllRecordsAsMBeans(true);
osb.start();
osb.probe();
Set<ObjectName> participants = JMXServer.getAgent().queryNames("jboss.jta:type=ObjectStore,itype=" + com.arjuna.ats.internal.jta.transaction.jts.subordinate.jca.coordinator.ServerTransaction.getType().substring(1) + ",uid=" + savingUid.stringForm().replaceAll(":", "_") + ",puid=*", null);
assertEquals(1, participants.size());
JMXServer.getAgent().getServer().invoke(participants.iterator().next(), "clearHeuristic", null, null);
xa.recover(XAResource.TMSTARTRSCAN);
xa.recover(XAResource.TMENDRSCAN);
Set<ObjectName> xaResourceRecords = JMXServer.getAgent().queryNames("jboss.jta:type=ObjectStore,itype=" + XAResourceRecord.typeName().substring(1) + ",uid=*", null);
for (ObjectName xaResourceRecord : xaResourceRecords) {
Object getGlobalTransactionId = JMXServer.getAgent().getServer().getAttribute(xaResourceRecord, "GlobalTransactionId");
Object getBranchQualifier = JMXServer.getAgent().getServer().getAttribute(xaResourceRecord, "BranchQualifier");
if (Arrays.equals(failedResourceXid.getGlobalTransactionId(), (byte[]) getGlobalTransactionId) && Arrays.equals(failedResourceXid.getBranchQualifier(), (byte[]) getBranchQualifier)) {
Object getHeuristicValue = JMXServer.getAgent().getServer().getAttribute(xaResourceRecord, "HeuristicValue");
assertTrue(getHeuristicValue.equals(new Integer(6)));
JMXServer.getAgent().getServer().invoke(xaResourceRecord, "clearHeuristic", null, null);
}
}
XARecoveryModule xaRecoveryModule = new XARecoveryModule();
xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
@Override
public boolean initialise(String p) throws Exception {
return false;
}
@Override
public XAResource[] getXAResources() throws Exception {
return new XAResource[] { new TestXAResource() {
public Xid[] recover(int var) throws XAException {
if (var == XAResource.TMSTARTRSCAN) {
if (failedResourceXid != null) {
return new Xid[] { failedResourceXid };
}
}
return new Xid[0];
}
@Override
public void commit(Xid xid, boolean b) throws XAException {
failedResourceXid = null;
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public void rollback(Xid xid) throws XAException {
fail("Resource was rolled back");
}
} };
}
});
xaRecoveryModule.periodicWorkFirstPass();
Field safetyIntervalMillis = RecoveryXids.class.getDeclaredField("safetyIntervalMillis");
safetyIntervalMillis.setAccessible(true);
Object o1 = safetyIntervalMillis.get(null);
safetyIntervalMillis.set(null, 0);
try {
xaRecoveryModule.periodicWorkSecondPass();
} finally {
safetyIntervalMillis.set(null, o1);
}
xa.recover(XAResource.TMSTARTRSCAN);
try {
xa.commit(xid, false);
Assert.fail("Expecting XAException being thrown indicating more recover to be called");
} catch (XAException expected) {
Assert.assertTrue("On commit XAException error code indicating more recover call is expected but it's " + expected.errorCode, XAException.XA_RETRY == expected.errorCode || XAException.XAER_RMFAIL == expected.errorCode);
} finally {
xa.recover(XAResource.TMENDRSCAN);
}
assertNull(failedResourceXid);
}
use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.
the class UnserializableSerializableXAResourceTest method test.
@Test
public void test() throws Exception {
XAResource res1 = new XAResource() {
@Override
public void start(Xid xid, int flags) throws XAException {
// TODO Auto-generated method stub
}
@Override
public void end(Xid xid, int flags) throws XAException {
// TODO Auto-generated method stub
}
@Override
public int prepare(Xid xid) throws XAException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
// TODO Auto-generated method stub
}
@Override
public void rollback(Xid xid) throws XAException {
// TODO Auto-generated method stub
}
@Override
public void forget(Xid xid) throws XAException {
// TODO Auto-generated method stub
}
@Override
public Xid[] recover(int flag) throws XAException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isSameRM(XAResource xaRes) throws XAException {
// TODO Auto-generated method stub
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean setTransactionTimeout(int seconds) throws XAException {
// TODO Auto-generated method stub
return false;
}
};
UnserializableSerializableXAResource res2 = new UnserializableSerializableXAResource(true);
final javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.getStatus();
tm.begin();
javax.transaction.Transaction theTransaction = tm.getTransaction();
assertTrue(theTransaction.enlistResource(res1));
assertTrue(theTransaction.enlistResource(res2));
tm.commit();
XARecoveryModule xaRecoveryModule = null;
for (RecoveryModule recoveryModule : ((Vector<RecoveryModule>) recoveryManager.getModules())) {
if (recoveryModule instanceof XARecoveryModule) {
xaRecoveryModule = (XARecoveryModule) recoveryModule;
break;
}
}
final Xid xid = res2.getXid();
xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
@Override
public boolean initialise(String p) throws Exception {
// TODO Auto-generated method stub
return false;
}
@Override
public XAResource[] getXAResources() throws Exception {
return new XAResource[] { new XAResource() {
@Override
public void start(Xid xid, int flags) throws XAException {
// TODO Auto-generated method stub
}
@Override
public void end(Xid xid, int flags) throws XAException {
// TODO Auto-generated method stub
}
@Override
public int prepare(Xid xid) throws XAException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
committed = true;
}
@Override
public void rollback(Xid xid) throws XAException {
// TODO Auto-generated method stub
}
@Override
public void forget(Xid xid) throws XAException {
// TODO Auto-generated method stub
}
@Override
public Xid[] recover(int flag) throws XAException {
if (committed) {
return null;
} else {
return new Xid[] { xid };
}
}
@Override
public boolean isSameRM(XAResource xaRes) throws XAException {
// TODO Auto-generated method stub
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean setTransactionTimeout(int seconds) throws XAException {
// TODO Auto-generated method stub
return false;
}
} };
}
});
recoveryManager.scan();
assertTrue(committed);
}
use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project spring-boot by spring-projects.
the class NarayanaXAConnectionFactoryWrapper method wrapConnectionFactory.
@Override
public ConnectionFactory wrapConnectionFactory(XAConnectionFactory xaConnectionFactory) {
XAResourceRecoveryHelper recoveryHelper = getRecoveryHelper(xaConnectionFactory);
this.recoveryManager.registerXAResourceRecoveryHelper(recoveryHelper);
return new ConnectionFactoryProxy(xaConnectionFactory, new TransactionHelperImpl(this.transactionManager));
}
use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project spring-boot by spring-projects.
the class NarayanaXADataSourceWrapper method wrapDataSource.
@Override
public DataSource wrapDataSource(XADataSource dataSource) {
XAResourceRecoveryHelper recoveryHelper = getRecoveryHelper(dataSource);
this.recoveryManager.registerXAResourceRecoveryHelper(recoveryHelper);
return new NarayanaDataSourceBean(dataSource);
}
Aggregations