Search in sources :

Example 81 with TransactionManager

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndAdapter.

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

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(tm).begin();
    verify(tm).commit();
    verify(tm).resume(tx);
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(javax.transaction.Transaction) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) UserTransaction(javax.transaction.UserTransaction) 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 82 with TransactionManager

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new SystemException()).given(tm).suspend();
    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 TransactionSystemException");
    } catch (TransactionSystemException ex) {
    // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) 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 83 with TransactionManager

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

the class TransactionBuilder method getValue.

@Override
public TransactionConfiguration getValue() {
    TransactionManager tm = this.tm.getOptionalValue();
    this.builder.transactionManagerLookup((tm != null) ? new TransactionManagerProvider(tm) : null);
    TransactionSynchronizationRegistry tsr = this.tsr.getOptionalValue();
    this.builder.transactionSynchronizationRegistryLookup((tsr != null) ? new TransactionSynchronizationRegistryProvider(tsr) : null);
    return this.builder.create();
}
Also used : TransactionSynchronizationRegistryProvider(org.jboss.as.clustering.infinispan.TransactionSynchronizationRegistryProvider) DummyTransactionManager(org.infinispan.transaction.tm.DummyTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) TransactionManagerProvider(org.jboss.as.clustering.infinispan.TransactionManagerProvider)

Example 84 with TransactionManager

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

the class MessageEndpointInvocationHandler method beforeDelivery.

@Override
public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException {
    // JCA 1.6 FR 13.5.6
    // The application server must set the thread context class loader to the endpoint
    // application class loader during the beforeDelivery call.
    previousClassLoader = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(getApplicationClassLoader());
    try {
        final TransactionManager tm = getTransactionManager();
        // TODO: in violation of JCA 1.6 FR 13.5.9?
        previousTx = tm.suspend();
        boolean isTransacted = service.isDeliveryTransacted(method);
        if (isTransacted) {
            tm.begin();
            currentTx = tm.getTransaction();
            if (xaRes != null)
                currentTx.enlistResource(xaRes);
        }
    } catch (Throwable t) {
        throw new ApplicationServerInternalException(t);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) ApplicationServerInternalException(javax.resource.spi.ApplicationServerInternalException)

Example 85 with TransactionManager

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

the class BMTInterceptor method processInvocation.

@Override
public Object processInvocation(final InterceptorContext context) throws Exception {
    TransactionManager tm = component.getTransactionManager();
    int oldTimeout = getCurrentTransactionTimeout(component);
    try {
        Transaction oldTx = tm.suspend();
        try {
            return handleInvocation(context);
        } finally {
            if (oldTx != null)
                tm.resume(oldTx);
        }
    } finally {
        tm.setTransactionTimeout(oldTimeout == -1 ? 0 : oldTimeout);
    }
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager)

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