Search in sources :

Example 56 with Transaction

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

the class InfinispanBatcherTestCase method resumeBatchExisting.

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

Example 57 with Transaction

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

the class InfinispanBatcherTestCase method createNestedBatchDiscard.

@Test
public void createNestedBatchDiscard() 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(tx.getStatus()).thenReturn(Status.STATUS_ACTIVE);
        when(this.tm.getTransaction()).thenReturn(tx);
        try (TransactionBatch innerBatch = this.batcher.createBatch()) {
            verify(this.tm, never()).begin();
            innerBatch.discard();
        }
        verify(tx, never()).commit();
        verify(tx, never()).rollback();
    } finally {
        capturedSync.getValue().afterCompletion(Status.STATUS_ROLLEDBACK);
    }
    verify(tx).rollback();
    verify(tx, never()).commit();
    assertNull(InfinispanBatcher.CURRENT_BATCH.get());
}
Also used : Transaction(javax.transaction.Transaction) Synchronization(javax.transaction.Synchronization) Test(org.junit.Test)

Example 58 with Transaction

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

the class InfinispanBatcherTestCase method createOverlappingBatchDiscard.

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

Example 59 with Transaction

use of javax.transaction.Transaction in project tomee by apache.

the class TransactionalTest method requiresNew.

@Test
public void requiresNew() {
    final AtomicReference<Transaction> tx2 = new AtomicReference<>();
    final Transaction tx1 = bean.defaultTx(new Runnable() {

        @Override
        public void run() {
            tx2.set(bean.newTx(new Runnable() {

                @Override
                public void run() {
                // no-op
                }
            }));
        }
    });
    assertNotSame(tx1, tx2.get());
}
Also used : Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 60 with Transaction

use of javax.transaction.Transaction in project tomee by apache.

the class ManagedConnectionBehaviorTest method run.

@Test
public void run() throws Exception {
    final GeronimoTransactionManager geronimoTransactionManager = new GeronimoTransactionManager((int) TimeUnit.MINUTES.toMillis(10));
    final TransactionManager mgr = new TransactionManagerWrapper(geronimoTransactionManager, "ManagedConnectionBehaviorTest", new GeronimoTransactionManagerFactory.GeronimoXAResourceWrapper());
    final MyDs myDs = new MyDs();
    final DataSource ds = new ManagedDataSource(myDs, geronimoTransactionManager, geronimoTransactionManager);
    {
        // no tx
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        connection.close();
        assertTrue(myDs.connections.iterator().next().closed);
        myDs.connections.clear();
    }
    {
        // tx
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        mgr.commit();
        assertTrue(myDs.connections.iterator().next().closed);
        assertTrue(myDs.connections.iterator().next().commit);
        assertFalse(myDs.connections.iterator().next().rollback);
        myDs.connections.clear();
    }
    {
        // tx already init
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        for (int i = 0; i < 5; i++) {
            // here the connection is already created, ensure we dont leak other connections
            connection.createBlob();
        }
        assertEquals(1, myDs.connections.size());
        mgr.commit();
        assertTrue(myDs.connections.iterator().next().closed);
        assertTrue(myDs.connections.iterator().next().commit);
        assertFalse(myDs.connections.iterator().next().rollback);
        myDs.connections.clear();
    }
    {
        // multiple tx
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        final Transaction previous = mgr.suspend();
        mgr.begin();
        final Connection connection2 = ds.getConnection();
        connection2.createBlob();
        assertEquals(2, myDs.connections.size());
        mgr.commit();
        mgr.resume(previous);
        mgr.commit();
        final Iterator<MyConn> iterator = myDs.connections.iterator();
        final MyConn first = iterator.next();
        assertTrue(first.closed);
        assertTrue(first.commit);
        assertTrue(myDs.connections.iterator().next().commit);
        myDs.connections.clear();
    }
}
Also used : Transaction(javax.transaction.Transaction) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionManagerWrapper(org.apache.openejb.resource.TransactionManagerWrapper) Connection(java.sql.Connection) Iterator(java.util.Iterator) GeronimoTransactionManagerFactory(org.apache.openejb.resource.GeronimoTransactionManagerFactory) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) DataSource(javax.sql.DataSource) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) 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