Search in sources :

Example 11 with PlatformTransactionManager

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

the class TransactionInterceptorTests method serializableWithCompositeSource.

@Test
public void serializableWithCompositeSource() throws Exception {
    NameMatchTransactionAttributeSource tas1 = new NameMatchTransactionAttributeSource();
    Properties props = new Properties();
    props.setProperty("methodName", "PROPAGATION_REQUIRED");
    tas1.setProperties(props);
    NameMatchTransactionAttributeSource tas2 = new NameMatchTransactionAttributeSource();
    props = new Properties();
    props.setProperty("otherMethodName", "PROPAGATION_REQUIRES_NEW");
    tas2.setProperties(props);
    TransactionInterceptor ti = new TransactionInterceptor();
    ti.setTransactionAttributeSources(new TransactionAttributeSource[] { tas1, tas2 });
    PlatformTransactionManager ptm = new SerializableTransactionManager();
    ti.setTransactionManager(ptm);
    ti = (TransactionInterceptor) SerializationTestUtils.serializeAndDeserialize(ti);
    assertTrue(ti.getTransactionManager() instanceof SerializableTransactionManager);
    assertTrue(ti.getTransactionAttributeSource() instanceof CompositeTransactionAttributeSource);
    CompositeTransactionAttributeSource ctas = (CompositeTransactionAttributeSource) ti.getTransactionAttributeSource();
    assertTrue(ctas.getTransactionAttributeSources()[0] instanceof NameMatchTransactionAttributeSource);
    assertTrue(ctas.getTransactionAttributeSources()[1] instanceof NameMatchTransactionAttributeSource);
}
Also used : Properties(java.util.Properties) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 12 with PlatformTransactionManager

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

the class TransactionInterceptorTests method determineTransactionManagerWithQualifierAndDefault.

@Test
public void determineTransactionManagerWithQualifierAndDefault() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
    TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, beanFactory);
    PlatformTransactionManager fooTransactionManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("fooTransactionManager");
    assertSame(fooTransactionManager, ti.determineTransactionManager(attribute));
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 13 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method noTransaction.

@Test
public void noTransaction() throws Exception {
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    TestBean tb = new TestBean();
    TransactionAttributeSource tas = new MapTransactionAttributeSource();
    // All the methods in this class use the advised() template method
    // to obtain a transaction object, configured with the given PlatformTransactionManager
    // and transaction attribute source
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);
    // expect no calls
    verifyZeroInteractions(ptm);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 14 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method doTestRollbackOnException.

/**
	 * Check that the given exception thrown by the target can produce the
	 * desired behavior with the appropriate transaction attribute.
	 * @param ex exception to be thrown by the target
	 * @param shouldRollback whether this should cause a transaction rollback
	 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute() {

        @Override
        public boolean rollbackOn(Throwable t) {
            assertTrue(t == ex);
            return shouldRollback;
        }
    };
    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Gets additional call(s) from TransactionControl
    given(ptm.getTransaction(txatt)).willReturn(status);
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
        if (shouldRollback) {
            willThrow(tex).given(ptm).rollback(status);
        } else {
            willThrow(tex).given(ptm).commit(status);
        }
    }
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.exceptional(ex);
        fail("Should have thrown exception");
    } catch (Throwable t) {
        if (rollbackException) {
            assertEquals("Caught wrong exception", tex, t);
        } else {
            assertEquals("Caught wrong exception", ex, t);
        }
    }
    if (!rollbackException) {
        if (shouldRollback) {
            verify(ptm).rollback(status);
        } else {
            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) TransactionSystemException(org.springframework.transaction.TransactionSystemException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 15 with PlatformTransactionManager

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

the class AbstractTransactionAspectTests method enclosingTransactionWithNonTransactionMethodOnAdvisedInside.

@Test
public void enclosingTransactionWithNonTransactionMethodOnAdvisedInside() throws Throwable {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(exceptionalMethod, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Expect a transaction
    given(ptm.getTransaction(txatt)).willReturn(status);
    final String spouseName = "innerName";
    TestBean outer = new TestBean() {

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

        @Override
        public String getName() {
            // Assert that we're in the inner proxy
            TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
            assertFalse(ti.hasTransaction());
            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(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) TransactionInfo(org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

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