Search in sources :

Example 61 with Synchronization

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();
}
Also used : ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) XAConnection(javax.jms.XAConnection) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 62 with Synchronization

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();
}
Also used : Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 63 with Synchronization

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();
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) SystemException(javax.transaction.SystemException) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) Synchronization(javax.transaction.Synchronization) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Example 64 with Synchronization

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);
}
Also used : Transaction(javax.transaction.Transaction) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) WorkSynchronization(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.WorkSynchronization) Synchronization(javax.transaction.Synchronization) WorkSynchronization(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.WorkSynchronization) Test(org.junit.Test)

Example 65 with Synchronization

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());
}
Also used : TransactionSynchronizationRegistryImple(com.arjuna.ats.internal.jta.transaction.jts.TransactionSynchronizationRegistryImple) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Aggregations

Synchronization (javax.transaction.Synchronization)87 Test (org.junit.Test)42 Transaction (javax.transaction.Transaction)23 SystemException (javax.transaction.SystemException)21 RollbackException (javax.transaction.RollbackException)14 SQLException (java.sql.SQLException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)7 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 UserTransaction (javax.transaction.UserTransaction)5 XAResource (javax.transaction.xa.XAResource)5 EJBException (javax.ejb.EJBException)4 HeuristicMixedException (javax.transaction.HeuristicMixedException)4 TransactionImple (com.arjuna.ats.internal.jta.transaction.jts.TransactionImple)3 List (java.util.List)3 Xid (javax.transaction.xa.Xid)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3