Search in sources :

Example 16 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.

the class AbstractTransactionAspectTests method enclosingTransactionWithNestedTransactionOnAdvisedInside.

@Test
public void enclosingTransactionWithNestedTransactionOnAdvisedInside() throws Throwable {
    final TransactionAttribute outerTxatt = new DefaultTransactionAttribute();
    final TransactionAttribute innerTxatt = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_NESTED);
    Method outerMethod = exceptionalMethod;
    Method innerMethod = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(outerMethod, outerTxatt);
    tas.register(innerMethod, innerTxatt);
    TransactionStatus outerStatus = mock(TransactionStatus.class);
    TransactionStatus innerStatus = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Expect a transaction
    given(ptm.getTransaction(outerTxatt)).willReturn(outerStatus);
    given(ptm.getTransaction(innerTxatt)).willReturn(innerStatus);
    final String spouseName = "innerName";
    TestBean outer = new TestBean() {

        @Override
        public void exceptional(Throwable t) throws Throwable {
            TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
            assertTrue(ti.hasTransaction());
            assertEquals(outerTxatt, ti.getTransactionAttribute());
            assertEquals(spouseName, getSpouse().getName());
        }
    };
    TestBean inner = new TestBean() {

        @Override
        public String getName() {
            // Assert that we're in the inner proxy
            TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
            // Has nested transaction
            assertTrue(ti.hasTransaction());
            assertEquals(innerTxatt, ti.getTransactionAttribute());
            return spouseName;
        }
    };
    ITestBean outerProxy = (ITestBean) advised(outer, ptm, tas);
    ITestBean innerProxy = (ITestBean) advised(inner, ptm, tas);
    outer.setSpouse(innerProxy);
    checkTransactionStatus(false);
    // Will invoke inner.getName, which is non-transactional
    outerProxy.exceptional(null);
    checkTransactionStatus(false);
    verify(ptm).commit(innerStatus);
    verify(ptm).commit(outerStatus);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionInfo(org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 17 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.

the class AbstractTransactionAspectTests method programmaticRollback.

/**
	 * Test that TransactionStatus.setRollbackOnly works.
	 */
@Test
public void programmaticRollback() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    given(ptm.getTransaction(txatt)).willReturn(status);
    final String name = "jenny";
    TestBean tb = new TestBean() {

        @Override
        public String getName() {
            TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus();
            txStatus.setRollbackOnly();
            return name;
        }
    };
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    // verification!?
    assertTrue(name.equals(itb.getName()));
    verify(ptm).commit(status);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) Method(java.lang.reflect.Method) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 18 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-boot by spring-projects.

the class TransactionAutoConfigurationTests method singleTransactionManager.

@Test
public void singleTransactionManager() {
    load(DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class);
    PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
    TransactionTemplate transactionTemplate = this.context.getBean(TransactionTemplate.class);
    assertThat(transactionTemplate.getTransactionManager()).isSameAs(transactionManager);
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 19 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.

the class ChainedTransactionManager method getTransaction.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
	 */
public MultiTransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
    MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
    if (!synchronizationManager.isSynchronizationActive() && canCreateTransaction(definition)) {
        synchronizationManager.initSynchronization();
        mts.setNewSynchonization();
    }
    try {
        for (PlatformTransactionManager transactionManager : transactionManagers) {
            mts.registerTransactionManager(definition, transactionManager);
        }
    } catch (Exception ex) {
        Map<PlatformTransactionManager, TransactionStatus> transactionStatuses = mts.getTransactionStatuses();
        for (PlatformTransactionManager transactionManager : transactionManagers) {
            try {
                if (transactionStatuses.get(transactionManager) != null) {
                    transactionManager.rollback(transactionStatuses.get(transactionManager));
                }
            } catch (Exception ex2) {
                LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex2.getMessage(), ex2);
            }
        }
        if (mts.isNewSynchonization()) {
            synchronizationManager.clearSynchronization();
        }
        throw new CannotCreateTransactionException(ex.getMessage(), ex);
    }
    return mts;
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Map(java.util.Map) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Example 20 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.

the class ChainedTransactionManager method rollback.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#rollback(org.springframework.transaction.TransactionStatus)
	 */
public void rollback(TransactionStatus status) throws TransactionException {
    Exception rollbackException = null;
    PlatformTransactionManager rollbackExceptionTransactionManager = null;
    MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
    for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
        try {
            multiTransactionStatus.rollback(transactionManager);
        } catch (Exception ex) {
            if (rollbackException == null) {
                rollbackException = ex;
                rollbackExceptionTransactionManager = transactionManager;
            } else {
                LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex.getMessage(), ex);
            }
        }
    }
    if (multiTransactionStatus.isNewSynchonization()) {
        synchronizationManager.clearSynchronization();
    }
    if (rollbackException != null) {
        throw new UnexpectedRollbackException("Rollback exception, originated at (" + rollbackExceptionTransactionManager + ") " + rollbackException.getMessage(), rollbackException);
    }
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Aggregations

PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)52 Test (org.junit.Test)32 TransactionStatus (org.springframework.transaction.TransactionStatus)13 ITestBean (org.springframework.tests.sample.beans.ITestBean)10 TestBean (org.springframework.tests.sample.beans.TestBean)10 BeanFactory (org.springframework.beans.factory.BeanFactory)7 Method (java.lang.reflect.Method)6 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)6 TestPlatformTransactionManager (org.grails.transaction.ChainedTransactionManagerTests.TestPlatformTransactionManager)5 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)5 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)4 TransactionException (org.springframework.transaction.TransactionException)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 HashMap (java.util.HashMap)3 DataSource (javax.sql.DataSource)3 Before (org.junit.Before)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 Connection (java.sql.Connection)2 Properties (java.util.Properties)2