use of javax.transaction.Synchronization in project narayana by jbosstm.
the class ConnectionProxyTests method shouldCloseConnectionWithTransaction.
@Test
public void shouldCloseConnectionWithTransaction() throws Exception {
when(transactionHelperMock.isTransactionAvailable()).thenReturn(true);
List<Synchronization> synchronizations = new ArrayList<>(1);
doAnswer(i -> synchronizations.add(i.getArgumentAt(0, Synchronization.class))).when(transactionHelperMock).registerSynchronization(any(Synchronization.class));
Connection connection = new ConnectionProxy(xaConnectionMock, transactionHelperMock);
connection.close();
// Will check if the correct connection was registered for closing
synchronizations.get(0).afterCompletion(0);
verify(transactionHelperMock, times(1)).isTransactionAvailable();
verify(transactionHelperMock, times(1)).registerSynchronization(any(Synchronization.class));
verify(xaConnectionMock, times(1)).close();
}
use of javax.transaction.Synchronization in project narayana by jbosstm.
the class ConnectionClosingSynchronizationTests method shouldCloseConnection.
@Test
public void shouldCloseConnection() throws JMSException {
Synchronization synchronization = new ConnectionClosingSynchronization(connectionMock);
synchronization.afterCompletion(Status.STATUS_COMMITTED);
verify(connectionMock, times(1)).close();
}
use of javax.transaction.Synchronization in project narayana by jbosstm.
the class EnlistResourceDuringCommit method testEnlistResourceDuringBeforeCompletion.
@Test
public void testEnlistResourceDuringBeforeCompletion() throws IllegalStateException, RollbackException, SystemException, SecurityException, HeuristicMixedException, HeuristicRollbackException {
final TransactionImple tx = new TransactionImple(0);
tx.registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
try {
tx.enlistResource(new XAResource() {
@Override
public void start(Xid arg0, int arg1) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public boolean setTransactionTimeout(int arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return false;
}
@Override
public void rollback(Xid arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public Xid[] recover(int arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return null;
}
@Override
public int prepare(Xid arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return 0;
}
@Override
public boolean isSameRM(XAResource arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
return 0;
}
@Override
public void forget(Xid arg0) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public void end(Xid arg0, int arg1) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
@Override
public void commit(Xid arg0, boolean arg1) throws XAException {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
});
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RollbackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void afterCompletion(int status) {
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
}
});
tx.commit();
}
use of javax.transaction.Synchronization 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 javax.transaction.Synchronization in project narayana by jbosstm.
the class TransactionSynchronizationRegistryTest method testTSRUseAfterCompletion.
@Test
public void testTSRUseAfterCompletion() throws Exception {
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
final CompletionCountLock ccl = new CompletionCountLock(2);
tm.begin();
final TransactionSynchronizationRegistry tsr = new TransactionSynchronizationRegistryImple();
tsr.registerInterposedSynchronization(new Synchronization() {
@Override
public void afterCompletion(int status) {
String key = "key";
Object value = new Object();
try {
tsr.getResource(key);
ccl.incrementFailedCount();
} catch (IllegalStateException ise) {
// Expected
ccl.incrementCount();
}
try {
tsr.putResource(key, value);
ccl.incrementFailedCount();
} catch (IllegalStateException ise) {
// Expected
ccl.incrementCount();
}
}
@Override
public void beforeCompletion() {
// TODO Auto-generated method stub
}
});
tm.commit();
assertTrue(ccl.waitForCompletion());
}
Aggregations