Search in sources :

Example 1 with CallCountingTransactionManager

use of cn.taketoday.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

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((PlatformTransactionManager) ptm, tas);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean1.class);
    proxyFactory.addAdvice(ti);
    proxyFactory.setTarget(tb);
    ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
    proxy.getAge();
    assertThat(ptm.commits).isEqualTo(1);
    ITestBean1 serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    serializedProxy.getAge();
    Advised advised = (Advised) serializedProxy;
    TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
    CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager();
    assertThat(serializedPtm.commits).isEqualTo(2);
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 2 with CallCountingTransactionManager

use of cn.taketoday.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

the class EnableTransactionManagementTests method spr14322FindsOnInterfaceWithInterfaceProxy.

@Test
public void spr14322FindsOnInterfaceWithInterfaceProxy() {
    StandardApplicationContext ctx = new StandardApplicationContext(Spr14322ConfigA.class);
    TransactionalTestInterface bean = ctx.getBean(TransactionalTestInterface.class);
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    bean.saveFoo();
    bean.saveBar();
    assertThat(txManager.begun).isEqualTo(2);
    assertThat(txManager.commits).isEqualTo(2);
    assertThat(txManager.rollbacks).isEqualTo(0);
    ctx.close();
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 3 with CallCountingTransactionManager

use of cn.taketoday.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

the class EnableTransactionManagementTests method txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent.

@Test
public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
    StandardApplicationContext ctx = new StandardApplicationContext(EnableTxConfig.class, MultiTxManagerConfig.class);
    assertThat(ctx.getBeansOfType(PlatformTransactionManager.class)).hasSize(2);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);
    CallCountingTransactionManager txManager2 = ctx.getBean("txManager2", CallCountingTransactionManager.class);
    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
    assertThat(txManager.begun).isEqualTo(0);
    assertThat(txManager.commits).isEqualTo(0);
    assertThat(txManager.rollbacks).isEqualTo(0);
    assertThat(txManager2.begun).isEqualTo(1);
    assertThat(txManager2.commits).isEqualTo(1);
    assertThat(txManager2.rollbacks).isEqualTo(0);
    ctx.close();
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 4 with CallCountingTransactionManager

use of cn.taketoday.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

the class EnableTransactionManagementTests method spr14322FindsOnInterfaceWithCglibProxy.

@Test
public void spr14322FindsOnInterfaceWithCglibProxy() {
    StandardApplicationContext ctx = new StandardApplicationContext(Spr14322ConfigB.class);
    TransactionalTestInterface bean = ctx.getBean(TransactionalTestInterface.class);
    CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
    bean.saveFoo();
    bean.saveBar();
    assertThat(txManager.begun).isEqualTo(2);
    assertThat(txManager.commits).isEqualTo(2);
    assertThat(txManager.rollbacks).isEqualTo(0);
    ctx.close();
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 5 with CallCountingTransactionManager

use of cn.taketoday.transaction.CallCountingTransactionManager in project today-infrastructure by TAKETODAY.

the class EnableTransactionManagementTests method txManagerIsResolvedOnInvocationOfTransactionalMethod.

@Test
public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
    StandardApplicationContext ctx = new StandardApplicationContext(EnableTxConfig.class, TxManagerConfig.class);
    TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
    CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);
    // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
    bean.findAllFoos();
    assertThat(txManager.begun).isEqualTo(1);
    assertThat(txManager.commits).isEqualTo(1);
    assertThat(txManager.rollbacks).isEqualTo(0);
    assertThat(txManager.lastDefinition.isReadOnly()).isTrue();
    assertThat(txManager.lastDefinition.getTimeout()).isEqualTo(5);
    assertThat(((TransactionAttribute) txManager.lastDefinition).getLabels()).contains("LABEL");
    ctx.close();
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) TransactionAttribute(cn.taketoday.transaction.interceptor.TransactionAttribute) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

CallCountingTransactionManager (cn.taketoday.transaction.CallCountingTransactionManager)24 Test (org.junit.jupiter.api.Test)24 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)16 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)4 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)4 Advised (cn.taketoday.aop.framework.Advised)2 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)2 HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)2 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)2 TransactionAttribute (cn.taketoday.transaction.interceptor.TransactionAttribute)2 TransactionInterceptor (cn.taketoday.transaction.interceptor.TransactionInterceptor)2 HashSet (java.util.HashSet)2