Search in sources :

Example 1 with TransactionInfo

use of org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo 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)

Example 2 with TransactionInfo

use of org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo 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)

Aggregations

Test (org.junit.Test)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 TransactionInfo (org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo)2 Method (java.lang.reflect.Method)1