Search in sources :

Example 1 with TransactionInterceptor

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

the class ProxyTransactionManagementConfiguration method transactionInterceptor.

@Component
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
    TransactionInterceptor interceptor = new TransactionInterceptor();
    interceptor.setTransactionAttributeSource(transactionAttributeSource);
    if (this.txManager != null) {
        interceptor.setTransactionManager(this.txManager);
    }
    return interceptor;
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) Role(cn.taketoday.context.annotation.Role) Component(cn.taketoday.lang.Component)

Example 2 with TransactionInterceptor

use of cn.taketoday.transaction.interceptor.TransactionInterceptor 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 3 with TransactionInterceptor

use of cn.taketoday.transaction.interceptor.TransactionInterceptor in project today-framework by TAKETODAY.

the class AnnotationTransactionInterceptorTests method withMonoRollback.

@Test
public void withMonoRollback() {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(new TestWithReactive());
    proxyFactory.addAdvice(new TransactionInterceptor(rtm, this.source));
    TestWithReactive proxy = (TestWithReactive) proxyFactory.getProxy();
    StepVerifier.withVirtualTime(proxy::monoSuccess).thenAwait(Duration.ofSeconds(1)).thenCancel().verify();
    assertReactiveGetTransactionAndRollbackCount(1);
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionInterceptor

use of cn.taketoday.transaction.interceptor.TransactionInterceptor in project today-framework by TAKETODAY.

the class AnnotationTransactionInterceptorTests method withMonoSuccess.

@Test
public void withMonoSuccess() {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(new TestWithReactive());
    proxyFactory.addAdvice(new TransactionInterceptor(rtm, this.source));
    TestWithReactive proxy = (TestWithReactive) proxyFactory.getProxy();
    StepVerifier.withVirtualTime(proxy::monoSuccess).thenAwait(Duration.ofSeconds(10)).verifyComplete();
    assertReactiveGetTransactionAndCommitCount(1);
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionInterceptor

use of cn.taketoday.transaction.interceptor.TransactionInterceptor in project today-framework by TAKETODAY.

the class AnnotationTransactionInterceptorTests method withMonoFailure.

@Test
public void withMonoFailure() {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(new TestWithReactive());
    proxyFactory.addAdvice(new TransactionInterceptor(rtm, this.source));
    TestWithReactive proxy = (TestWithReactive) proxyFactory.getProxy();
    proxy.monoFailure().as(StepVerifier::create).verifyError();
    assertReactiveGetTransactionAndRollbackCount(1);
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionInterceptor (cn.taketoday.transaction.interceptor.TransactionInterceptor)16 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)14 Test (org.junit.jupiter.api.Test)14 Advised (cn.taketoday.aop.framework.Advised)2 Role (cn.taketoday.context.annotation.Role)2 Component (cn.taketoday.lang.Component)2 CallCountingTransactionManager (cn.taketoday.transaction.CallCountingTransactionManager)2