Search in sources :

Example 16 with TransactionManager

use of javax.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    willThrow(new SystemException()).given(ut).begin();
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            }
        });
        fail("Should have thrown CannotCreateTransactionException");
    } catch (CannotCreateTransactionException ex) {
    // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(tm).resume(tx);
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(javax.transaction.Transaction) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) UserTransaction(javax.transaction.UserTransaction) SystemException(javax.transaction.SystemException) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 17 with TransactionManager

use of javax.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndJtaSynchronization.

@Test
public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction tx = new MockJtaTransaction();
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.getTransaction()).willReturn(tx);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNotNull(tx.getSynchronization());
    tx.getSynchronization().beforeCompletion();
    tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);
    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 18 with TransactionManager

use of javax.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerSerializationTests method serializable.

@Test
public void serializable() throws Exception {
    UserTransaction ut1 = mock(UserTransaction.class);
    UserTransaction ut2 = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    JtaTransactionManager jtam = new JtaTransactionManager();
    jtam.setUserTransaction(ut1);
    jtam.setTransactionManager(tm);
    jtam.setRollbackOnCommitFailure(true);
    jtam.afterPropertiesSet();
    SimpleNamingContextBuilder jndiEnv = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    jndiEnv.bind(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut2);
    JtaTransactionManager serializedJtatm = (JtaTransactionManager) SerializationTestUtils.serializeAndDeserialize(jtam);
    // should do client-side lookup
    assertNotNull("Logger must survive serialization", serializedJtatm.logger);
    assertTrue("UserTransaction looked up on client", serializedJtatm.getUserTransaction() == ut2);
    assertNull("TransactionManager didn't survive", serializedJtatm.getTransactionManager());
    assertEquals(true, serializedJtatm.isRollbackOnCommitFailure());
}
Also used : UserTransaction(javax.transaction.UserTransaction) SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) Test(org.junit.Test)

Example 19 with TransactionManager

use of javax.transaction.TransactionManager in project blueprints by tinkerpop.

the class Neo4jGraph method rollback.

public void rollback() {
    if (null == tx.get()) {
        return;
    }
    GraphDatabaseAPI graphDatabaseAPI = (GraphDatabaseAPI) getRawGraph();
    TransactionManager transactionManager = graphDatabaseAPI.getTxManager();
    try {
        javax.transaction.Transaction t = transactionManager.getTransaction();
        if (t == null || t.getStatus() == Status.STATUS_ROLLEDBACK) {
            return;
        }
        tx.get().failure();
    } catch (SystemException e) {
        throw new RuntimeException(e);
    } finally {
        tx.get().finish();
        tx.remove();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager)

Example 20 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class JTASessionContext method currentSession.

@Override
public Session currentSession() throws HibernateException {
    final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService(JtaPlatform.class);
    final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    if (transactionManager == null) {
        throw new HibernateException("No TransactionManagerLookup specified");
    }
    Transaction txn;
    try {
        txn = transactionManager.getTransaction();
        if (txn == null) {
            throw new HibernateException("Unable to locate current JTA transaction");
        }
        if (!JtaStatusHelper.isActive(txn.getStatus())) {
            // entries cleaned up (aside from spawning threads).
            throw new HibernateException("Current transaction is not in progress");
        }
    } catch (HibernateException e) {
        throw e;
    } catch (Throwable t) {
        throw new HibernateException("Problem locating/validating JTA transaction", t);
    }
    final Object txnIdentifier = jtaPlatform.getTransactionIdentifier(txn);
    Session currentSession = currentSessionMap.get(txnIdentifier);
    if (currentSession == null) {
        currentSession = buildOrObtainSession();
        try {
            txn.registerSynchronization(buildCleanupSynch(txnIdentifier));
        } catch (Throwable t) {
            try {
                currentSession.close();
            } catch (Throwable ignore) {
                LOG.debug("Unable to release generated current-session on failed synch registration", ignore);
            }
            throw new HibernateException("Unable to register cleanup Synchronization with TransactionManager");
        }
        currentSessionMap.put(txnIdentifier, currentSession);
    } else {
        validateExistingSession(currentSession);
    }
    return currentSession;
}
Also used : JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) HibernateException(org.hibernate.HibernateException) TransactionManager(javax.transaction.TransactionManager) Session(org.hibernate.Session)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5