Search in sources :

Example 51 with Transaction

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

the class CMTTxInterceptor method requiresNew.

protected Object requiresNew(InterceptorContext invocation, final EJBComponent component, final int timeout) throws Exception {
    final TransactionManager tm = component.getTransactionManager();
    if (timeout != -1) {
        tm.setTransactionTimeout(timeout);
    }
    Transaction tx = tm.getTransaction();
    if (tx != null) {
        tm.suspend();
        try {
            return invokeInOurTx(invocation, tm, component);
        } finally {
            tm.resume(tx);
        }
    } else {
        return invokeInOurTx(invocation, tm, component);
    }
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager)

Example 52 with Transaction

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

the class InfinispanBatcherTestCase method resumeBatch.

@Test
public void resumeBatch() throws Exception {
    TransactionBatch batch = mock(TransactionBatch.class);
    Transaction tx = mock(Transaction.class);
    when(batch.getTransaction()).thenReturn(tx);
    try (BatchContext context = this.batcher.resumeBatch(batch)) {
        verify(this.tm, never()).suspend();
        verify(this.tm).resume(tx);
        reset(this.tm);
        assertSame(batch, InfinispanBatcher.CURRENT_BATCH.get());
    }
    verify(this.tm).suspend();
    verify(this.tm, never()).resume(any());
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) BatchContext(org.wildfly.clustering.ee.BatchContext) Test(org.junit.Test)

Example 53 with Transaction

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

the class InfinispanBatcher method resumeBatch.

@Override
public BatchContext resumeBatch(TransactionBatch batch) {
    TransactionBatch existingBatch = CURRENT_BATCH.get();
    // Trivial case - nothing to suspend/resume
    if (batch == existingBatch)
        return PASSIVE_BATCH_CONTEXT;
    Transaction tx = (batch != null) ? batch.getTransaction() : null;
    // Non-tx case, just swap thread local
    if ((batch == null) || (tx == null)) {
        CURRENT_BATCH.set(batch);
        return () -> {
            CURRENT_BATCH.set(existingBatch);
        };
    }
    try {
        if (existingBatch != null) {
            Transaction existingTx = this.tm.suspend();
            if (existingBatch.getTransaction() != existingTx) {
                throw new IllegalStateException();
            }
        }
        this.tm.resume(tx);
        CURRENT_BATCH.set(batch);
        return () -> {
            try {
                this.tm.suspend();
                if (existingBatch != null) {
                    try {
                        this.tm.resume(existingBatch.getTransaction());
                        CURRENT_BATCH.set(existingBatch);
                    } catch (InvalidTransactionException e) {
                        throw new CacheException(e);
                    }
                } else {
                    CURRENT_BATCH.remove();
                }
            } catch (SystemException e) {
                throw new CacheException(e);
            }
        };
    } catch (SystemException | InvalidTransactionException e) {
        throw new CacheException(e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) CacheException(org.infinispan.commons.CacheException) InvalidTransactionException(javax.transaction.InvalidTransactionException)

Example 54 with Transaction

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

the class InfinispanBatcher method createBatch.

@Override
public TransactionBatch createBatch() {
    if (this.tm == null)
        return NON_TX_BATCH;
    TransactionBatch batch = CURRENT_BATCH.get();
    if (batch != null) {
        return batch.interpose();
    }
    try {
        this.tm.suspend();
        this.tm.begin();
        Transaction tx = this.tm.getTransaction();
        tx.registerSynchronization(CURRENT_BATCH_REMOVER);
        batch = new InfinispanBatch(tx);
        CURRENT_BATCH.set(batch);
        return batch;
    } catch (RollbackException | SystemException | NotSupportedException e) {
        throw new CacheException(e);
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) CacheException(org.infinispan.commons.CacheException) RollbackException(javax.transaction.RollbackException) NotSupportedException(javax.transaction.NotSupportedException)

Example 55 with Transaction

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

the class InfinispanBatcherTestCase method createBatchDiscard.

@Test
public void createBatchDiscard() 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());
        batch.discard();
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_ROLLEDBACK);
    }
    verify(tx, never()).commit();
    verify(tx).rollback();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Aggregations

Transaction (javax.transaction.Transaction)160 SystemException (javax.transaction.SystemException)55 Test (org.junit.Test)42 RollbackException (javax.transaction.RollbackException)26 TransactionManager (javax.transaction.TransactionManager)24 UserTransaction (javax.transaction.UserTransaction)19 NotInTransactionException (org.neo4j.graphdb.NotInTransactionException)14 NotSupportedException (javax.transaction.NotSupportedException)13 Synchronization (javax.transaction.Synchronization)10 XAResource (javax.transaction.xa.XAResource)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)8 InvalidTransactionException (javax.transaction.InvalidTransactionException)7 TransactionContext (com.hazelcast.transaction.TransactionContext)6 RemoteException (java.rmi.RemoteException)6 ResourceException (javax.resource.ResourceException)6 ManagedConnection (javax.resource.spi.ManagedConnection)6 SQLException (java.sql.SQLException)5 HeuristicMixedException (javax.transaction.HeuristicMixedException)5 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)5