Search in sources :

Example 6 with CallCountingTransactionManager

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

the class TxNamespaceHandlerTests method invokeTransactional.

@Test
public void invokeTransactional() throws Exception {
    ITestBean testBean = getTestBean();
    CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
    // try with transactional
    assertEquals("Should not have any started transactions", 0, ptm.begun);
    testBean.getName();
    assertTrue(ptm.lastDefinition.isReadOnly());
    assertEquals("Should have 1 started transaction", 1, ptm.begun);
    assertEquals("Should have 1 committed transaction", 1, ptm.commits);
    // try with non-transaction
    testBean.haveBirthday();
    assertEquals("Should not have started another transaction", 1, ptm.begun);
    // try with exceptional
    try {
        testBean.exceptional(new IllegalArgumentException("foo"));
        fail("Should NEVER get here");
    } catch (Throwable throwable) {
        assertEquals("Should have another started transaction", 2, ptm.begun);
        assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 7 with CallCountingTransactionManager

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

the class SimpleTransactionScopeTests method getWithTransactionManager.

@Test
public void getWithTransactionManager() throws Exception {
    try (GenericApplicationContext context = new GenericApplicationContext()) {
        context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
        GenericBeanDefinition bd1 = new GenericBeanDefinition();
        bd1.setBeanClass(TestBean.class);
        bd1.setScope("tx");
        bd1.setPrimary(true);
        context.registerBeanDefinition("txScopedObject1", bd1);
        GenericBeanDefinition bd2 = new GenericBeanDefinition();
        bd2.setBeanClass(DerivedTestBean.class);
        bd2.setScope("tx");
        context.registerBeanDefinition("txScopedObject2", bd2);
        context.refresh();
        CallCountingTransactionManager tm = new CallCountingTransactionManager();
        TransactionTemplate tt = new TransactionTemplate(tm);
        Set<DerivedTestBean> finallyDestroy = new HashSet<>();
        tt.execute(status -> {
            TestBean bean1 = context.getBean(TestBean.class);
            assertSame(bean1, context.getBean(TestBean.class));
            DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
            assertSame(bean2, context.getBean(DerivedTestBean.class));
            context.getBeanFactory().destroyScopedBean("txScopedObject2");
            assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
            assertTrue(bean2.wasDestroyed());
            DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
            assertSame(bean2a, context.getBean(DerivedTestBean.class));
            assertNotSame(bean2, bean2a);
            context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
            assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
            assertFalse(bean2a.wasDestroyed());
            DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
            finallyDestroy.add(bean2b);
            assertSame(bean2b, context.getBean(DerivedTestBean.class));
            assertNotSame(bean2, bean2b);
            assertNotSame(bean2a, bean2b);
            Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
            TransactionTemplate tt2 = new TransactionTemplate(tm);
            tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
            tt2.execute(status2 -> {
                DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
                immediatelyDestroy.add(bean2c);
                assertSame(bean2c, context.getBean(DerivedTestBean.class));
                assertNotSame(bean2, bean2c);
                assertNotSame(bean2a, bean2c);
                assertNotSame(bean2b, bean2c);
                return null;
            });
            assertTrue(immediatelyDestroy.iterator().next().wasDestroyed());
            assertFalse(bean2b.wasDestroyed());
            return null;
        });
        assertTrue(finallyDestroy.iterator().next().wasDestroyed());
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with CallCountingTransactionManager

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

the class AnnotationTransactionAttributeSourceTests method serializable.

@Test
public void serializable() throws Exception {
    TestBean1 tb = new TestBean1();
    CallCountingTransactionManager ptm = new CallCountingTransactionManager();
    AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
    TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    proxyFactory.addAdvice(ti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    assertEquals(1, ptm.commits);
    ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
    serializedProxy.getAge();
    Advised advised = (Advised) serializedProxy;
    TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
    CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager();
    assertEquals(2, serializedPtm.commits);
}
Also used : TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 9 with CallCountingTransactionManager

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

the class AnnotationTransactionNamespaceHandlerTests method invokeTransactional.

@Test
public void invokeTransactional() throws Exception {
    TransactionalTestBean testBean = getTestBean();
    CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
    // try with transactional
    assertEquals("Should not have any started transactions", 0, ptm.begun);
    testBean.findAllFoos();
    assertEquals("Should have 1 started transaction", 1, ptm.begun);
    assertEquals("Should have 1 committed transaction", 1, ptm.commits);
    // try with non-transaction
    testBean.doSomething();
    assertEquals("Should not have started another transaction", 1, ptm.begun);
    // try with exceptional
    try {
        testBean.exceptional(new IllegalArgumentException("foo"));
        fail("Should NEVER get here");
    } catch (Throwable throwable) {
        assertEquals("Should have another started transaction", 2, ptm.begun);
        assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
    }
}
Also used : CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 10 with CallCountingTransactionManager

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

the class BeanFactoryTransactionTests method testCglibTransactionProxyImplementsNoInterfaces.

@Test
public void testCglibTransactionProxyImplementsNoInterfaces() throws NoSuchMethodException {
    ImplementsNoInterfaces ini = (ImplementsNoInterfaces) factory.getBean("cglibNoInterfaces");
    assertTrue("testBean is CGLIB advised", AopUtils.isCglibProxy(ini));
    assertTrue(ini instanceof TransactionalProxy);
    String newName = "Gordon";
    // Install facade
    CallCountingTransactionManager ptm = new CallCountingTransactionManager();
    PlatformTransactionManagerFacade.delegate = ptm;
    ini.setName(newName);
    assertEquals(newName, ini.getName());
    assertEquals(2, ptm.commits);
}
Also used : CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Aggregations

CallCountingTransactionManager (org.springframework.tests.transaction.CallCountingTransactionManager)20 Test (org.junit.Test)18 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)7 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)3 ServletException (javax.servlet.ServletException)2 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Ignore (org.junit.Ignore)1 Advised (org.springframework.aop.framework.Advised)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 HotSwappableTargetSource (org.springframework.aop.target.HotSwappableTargetSource)1 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)1 Bean (org.springframework.context.annotation.Bean)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 NoTransactionException (org.springframework.transaction.NoTransactionException)1 TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)1