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);
}
}
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());
}
}
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);
}
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);
}
}
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);
}
Aggregations