use of com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple in project narayana by jbosstm.
the class TransactionImpleUnitTest method testXidCreation.
@Test
public void testXidCreation() throws Exception {
ThreadActionData.purgeActions();
Class[] parameterTypes = new Class[3];
TransactionImple tx = new TransactionImple(0);
parameterTypes[0] = boolean.class;
parameterTypes[1] = XAModifier.class;
parameterTypes[2] = XAResource.class;
Method m = tx.getClass().getDeclaredMethod("createXid", parameterTypes);
m.setAccessible(true);
Object[] parameters = new Object[3];
parameters[0] = false;
parameters[1] = new DummyXAModifier();
parameters[2] = new DummyXA(false);
Xid res = (Xid) m.invoke(tx, parameters);
assertTrue(res != null);
tx.rollback();
}
use of com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple in project narayana by jbosstm.
the class SynchronizationUnitTest method testSynchronizationFailure.
@Test
public void testSynchronizationFailure() throws Exception {
TransactionImple tx = new TransactionImple(0);
DummyXA res = new DummyXA(false) {
public void rollback(Xid xid) throws XAException {
super.rollback(xid);
throw new XAException(XAException.XA_RETRY);
}
};
tx.enlistResource(res);
final String exceptionError = "intentional testing exception";
tx.registerSynchronization(new javax.transaction.Synchronization() {
@Override
public void beforeCompletion() {
throw new RuntimeException(exceptionError);
}
@Override
public void afterCompletion(int status) {
}
});
try {
tx.commit();
} catch (Exception e) {
Throwable exceptionToCheck = e;
while (exceptionToCheck != null) {
if (exceptionToCheck.getMessage().equals(exceptionError))
return;
exceptionToCheck = exceptionToCheck.getCause();
}
throw e;
}
}
use of com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple in project narayana by jbosstm.
the class FailAfterPrepareBase method generateCMRRecord.
protected Uid generateCMRRecord(final DataSource dataSource) throws Exception {
((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
// 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
Vector recoveryModules = manager.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
recoveryModule = (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) {
final Uid[] uids = new Uid[1];
Thread foo = 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)");
uids[0] = ((TransactionImple) tm.getTransaction()).get_uid();
tm.commit();
} catch (ExecuteException t) {
} catch (Exception t) {
t.printStackTrace();
failed = true;
} catch (Error t) {
}
}
});
foo.start();
foo.join();
assertFalse(failed);
return uids[0];
}
use of com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple in project narayana by jbosstm.
the class WorkUnitTest method testWorkSynchronization.
@Test
public void testWorkSynchronization() throws Exception {
Transaction tx = new TransactionImple(0);
Synchronization ws = new WorkSynchronization(tx);
DummyWork work = new DummyWork();
TxWorkManager.addWork(work, tx);
try {
ws.beforeCompletion();
fail();
} catch (final IllegalStateException ex) {
}
ws.afterCompletion(Status.STATUS_COMMITTED);
}
use of com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple in project narayana by jbosstm.
the class CrashRecoveryCommitReturnsXA_RETRYHeuristicRollback method testHeuristicRollback.
@Test
public void testHeuristicRollback() throws Exception {
// this test is supposed to leave a record around in the log store
// during a commit long enough
// that the periodic recovery thread runs and detects it. rather than
// rely on delays to make
// this happen (placing us at the mercy of the scheduler) we use a
// byteman script to enforce
// the thread sequence we need
RecoveryEnvironmentBean recoveryEnvironmentBean = BeanPopulator.getDefaultInstance(RecoveryEnvironmentBean.class);
// JBTM-1354 we need to make sure that a full scan has gone off
recoveryEnvironmentBean.setRecoveryBackoffPeriod(1);
recoveryEnvironmentBean.setPeriodicRecoveryPeriod(Integer.MAX_VALUE);
List<String> recoveryModuleClassNames = new ArrayList<String>();
recoveryModuleClassNames.add("com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule");
recoveryModuleClassNames.add("com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule");
recoveryEnvironmentBean.setRecoveryModuleClassNames(recoveryModuleClassNames);
List<String> expiryScannerClassNames = new ArrayList<String>();
expiryScannerClassNames.add("com.arjuna.ats.internal.arjuna.recovery.ExpiredTransactionStatusManagerScanner");
recoveryEnvironmentBean.setExpiryScannerClassNames(expiryScannerClassNames);
recoveryEnvironmentBean.setRecoveryActivators(null);
// start the recovery manager
RecoveryManager.manager().initialize();
XARecoveryModule xaRecoveryModule = null;
for (RecoveryModule recoveryModule : ((Vector<RecoveryModule>) RecoveryManager.manager().getModules())) {
if (recoveryModule instanceof XARecoveryModule) {
xaRecoveryModule = (XARecoveryModule) recoveryModule;
break;
}
}
if (xaRecoveryModule == null) {
throw new Exception("No XARM");
}
// JBTM-1354 Run a scan to make sure that the recovery thread has completed a full run before starting the test
// The important thing is that replayCompletion is allowed to do a scan of the transactions
RecoveryManager.manager().scan();
XAResource firstResource = new SimpleResource();
Object toWakeUp = new Object();
final SimpleResourceXA_RETRYHeuristicRollback secondResource = new SimpleResourceXA_RETRYHeuristicRollback();
xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
@Override
public boolean initialise(String p) throws Exception {
// TODO Auto-generated method stub
return true;
}
@Override
public XAResource[] getXAResources() throws Exception {
// TODO Auto-generated method stub
return new XAResource[] { secondResource };
}
});
// ok, now drive a TX to completion. the script should ensure that the
// recovery
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
javax.transaction.Transaction theTransaction = tm.getTransaction();
Uid txUid = ((TransactionImple) theTransaction).get_uid();
theTransaction.enlistResource(firstResource);
theTransaction.enlistResource(secondResource);
assertFalse(secondResource.wasCommitted());
tm.commit();
InputObjectState uids = new InputObjectState();
String type = new AtomicAction().type();
StoreManager.getRecoveryStore().allObjUids(type, uids);
boolean moreUids = true;
boolean found = false;
while (moreUids) {
Uid theUid = UidHelper.unpackFrom(uids);
if (theUid.equals(txUid)) {
found = true;
Field heuristicListField = BasicAction.class.getDeclaredField("heuristicList");
heuristicListField.setAccessible(true);
ActionStatusService ass = new ActionStatusService();
{
int theStatus = ass.getTransactionStatus(type, theUid.stringForm());
assertTrue(theStatus == ActionStatus.COMMITTED);
RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(theUid, theStatus);
theStatus = rcvAtomicAction.status();
rcvAtomicAction.replayPhase2();
assertTrue(theStatus == ActionStatus.COMMITTED);
assertTrue(secondResource.wasCommitted());
RecordList heuristicList = (RecordList) heuristicListField.get(rcvAtomicAction);
assertTrue("Expected 1 heuristics: " + heuristicList.size(), heuristicList.size() == 1);
}
{
int theStatus = ass.getTransactionStatus(type, theUid.stringForm());
assertTrue(theStatus == ActionStatus.COMMITTED);
RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(theUid, theStatus);
theStatus = rcvAtomicAction.status();
assertTrue(theStatus == ActionStatus.COMMITTED);
RecordList heuristicList = (RecordList) heuristicListField.get(rcvAtomicAction);
assertTrue("Expected 1 heuristics: " + heuristicList.size(), heuristicList.size() == 1);
assertTrue(secondResource.wasCommitted());
}
} else if (theUid.equals(Uid.nullUid())) {
moreUids = false;
}
}
if (!found) {
throw new Exception("Could not locate the Uid");
}
}
Aggregations