Search in sources :

Example 26 with Synchronization

use of javax.transaction.Synchronization in project neo4j-mobile-android by neo4j-contrib.

the class TransactionImpl method doBeforeCompletion.

synchronized void doBeforeCompletion() {
    beforeCompletionRunning = true;
    try {
        for (Synchronization s : syncHooks) {
            try {
                s.beforeCompletion();
            } catch (Throwable t) {
                log.log(Level.WARNING, "Caught exception from tx syncronization[" + s + "] beforeCompletion()", t);
            }
        }
        // execute any hooks added since we entered doBeforeCompletion
        while (!syncHooksAdded.isEmpty()) {
            List<Synchronization> addedHooks = syncHooksAdded;
            syncHooksAdded = new ArrayList<Synchronization>();
            for (Synchronization s : addedHooks) {
                s.beforeCompletion();
                syncHooks.add(s);
            }
        }
    } finally {
        beforeCompletionRunning = false;
    }
}
Also used : Synchronization(javax.transaction.Synchronization)

Example 27 with Synchronization

use of javax.transaction.Synchronization in project wildfly by wildfly.

the class SFSB1 method createEmployeeWaitForTxTimeout.

@TransactionTimeout(value = 1, unit = TimeUnit.SECONDS)
public void createEmployeeWaitForTxTimeout(boolean registerTxSync, String name, String address, int id) {
    LOGGER.trace("org.jboss.as.test.integration.jpa.mockprovider.txtimeout.createEmployeeWaitForTxTimeout " + "entered, will wait for tx time out to occur");
    Employee emp = new Employee();
    emp.setId(id);
    emp.setAddress(address);
    emp.setName(name);
    entityManager.persist(emp);
    boolean done = false;
    if (registerTxSync) {
        transactionSynchronizationRegistry.registerInterposedSynchronization(new Synchronization() {

            @Override
            public void beforeCompletion() {
            }

            @Override
            public void afterCompletion(int status) {
                afterCompletionCalledByTMTimeoutThread = TxUtils.isTransactionManagerTimeoutThread();
            }
        });
    }
    while (!done) {
        try {
            Thread.sleep(250);
            entityManager.find(Employee.class, id);
            int status = transactionSynchronizationRegistry.getTransactionStatus();
            switch(status) {
                case Status.STATUS_COMMITTED:
                    throw new RuntimeException("transaction was committed.");
                case Status.STATUS_ROLLEDBACK:
                    LOGGER.trace("tx timed out and rolled back as expected, success case reached.");
                    done = true;
                    break;
                case Status.STATUS_ACTIVE:
                    LOGGER.trace("tx is still active, sleep for 250ms and check tx status again.");
                    break;
                default:
                    LOGGER.trace("tx status = " + status + ", sleep for 250ms and check tx status again.");
                    break;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return;
        }
        LOGGER.trace("org.jboss.as.test.integration.jpa.mockprovider.txtimeout.createEmployeeWaitForTxTimeout waiting for tx to timeout");
    }
}
Also used : Synchronization(javax.transaction.Synchronization) TransactionTimeout(org.jboss.ejb3.annotation.TransactionTimeout)

Example 28 with Synchronization

use of javax.transaction.Synchronization in project wildfly by wildfly.

the class InfinispanBatcherTestCase method createBatchClose.

@Test
public void createBatchClose() throws Exception {
    Transaction tx = mock(Transaction.class);
    ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
    when(this.tm.getTransaction()).thenReturn(tx);
    try (TransactionBatch batch = this.batcher.createBatch()) {
        verify(this.tm).begin();
        verify(tx).registerSynchronization(capturedSync.capture());
        assertSame(tx, batch.getTransaction());
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
    }
    verify(tx).commit();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 29 with Synchronization

use of javax.transaction.Synchronization in project wildfly by wildfly.

the class InfinispanBatcherTestCase method createNestedBatchClose.

@Test
public void createNestedBatchClose() throws Exception {
    Transaction tx = mock(Transaction.class);
    ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
    when(this.tm.getTransaction()).thenReturn(tx);
    try (TransactionBatch outerBatch = this.batcher.createBatch()) {
        verify(this.tm).begin();
        verify(tx).registerSynchronization(capturedSync.capture());
        reset(this.tm);
        assertSame(tx, outerBatch.getTransaction());
        when(this.tm.getTransaction()).thenReturn(tx);
        try (TransactionBatch innerBatch = this.batcher.createBatch()) {
            verify(this.tm, never()).begin();
            verify(this.tm, never()).suspend();
        }
        verify(tx, never()).rollback();
        verify(tx, never()).commit();
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
    }
    verify(tx, never()).rollback();
    verify(tx).commit();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 30 with Synchronization

use of javax.transaction.Synchronization in project wildfly by wildfly.

the class InfinispanBatcherTestCase method createOverlappingBatchClose.

@SuppressWarnings("resource")
@Test
public void createOverlappingBatchClose() throws Exception {
    Transaction tx = mock(Transaction.class);
    ArgumentCaptor<Synchronization> capturedSync = ArgumentCaptor.forClass(Synchronization.class);
    when(this.tm.getTransaction()).thenReturn(tx);
    TransactionBatch batch = this.batcher.createBatch();
    verify(this.tm).begin();
    verify(tx).registerSynchronization(capturedSync.capture());
    reset(this.tm);
    try {
        assertSame(tx, batch.getTransaction());
        when(this.tm.getTransaction()).thenReturn(tx);
        when(tx.getStatus()).thenReturn(Status.STATUS_ACTIVE);
        try (TransactionBatch innerBatch = this.batcher.createBatch()) {
            verify(this.tm, never()).begin();
            batch.close();
            verify(tx, never()).rollback();
            verify(tx, never()).commit();
        }
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_COMMITTED);
    }
    verify(tx, never()).rollback();
    verify(tx).commit();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Aggregations

Synchronization (javax.transaction.Synchronization)38 Test (org.junit.Test)20 Transaction (javax.transaction.Transaction)10 SQLException (java.sql.SQLException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 SystemException (javax.transaction.SystemException)7 RollbackException (javax.transaction.RollbackException)5 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 TransactionManager (javax.transaction.TransactionManager)3 UserTransaction (javax.transaction.UserTransaction)3 Map (java.util.Map)2 Set (java.util.Set)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EJBException (javax.ejb.EJBException)2 ThreadContext (org.apache.openejb.core.ThreadContext)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchObjectException (java.rmi.NoSuchObjectException)1 RemoteException (java.rmi.RemoteException)1 Connection (java.sql.Connection)1