Search in sources :

Example 6 with TransactionInterceptor

use of org.springframework.transaction.interceptor.TransactionInterceptor in project spring-framework by spring-projects.

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(org.springframework.transaction.interceptor.TransactionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 7 with TransactionInterceptor

use of org.springframework.transaction.interceptor.TransactionInterceptor 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((TransactionManager) 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(org.springframework.transaction.interceptor.TransactionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) CallCountingTransactionManager(org.springframework.transaction.testfixture.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 8 with TransactionInterceptor

use of org.springframework.transaction.interceptor.TransactionInterceptor in project spring-framework by spring-projects.

the class ProxyTransactionManagementConfiguration method transactionInterceptor.

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
    TransactionInterceptor interceptor = new TransactionInterceptor();
    interceptor.setTransactionAttributeSource(transactionAttributeSource());
    if (this.txManager != null) {
        interceptor.setTransactionManager(this.txManager);
    }
    return interceptor;
}
Also used : TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) Role(org.springframework.context.annotation.Role) Bean(org.springframework.context.annotation.Bean)

Example 9 with TransactionInterceptor

use of org.springframework.transaction.interceptor.TransactionInterceptor in project spring-data-commons by spring-projects.

the class TransactionalRepositoryProxyPostProcessor method postProcess.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.core.support.RepositoryProxyPostProcessor#postProcess(org.springframework.aop.framework.ProxyFactory, org.springframework.data.repository.core.RepositoryInformation)
	 */
public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
    CustomAnnotationTransactionAttributeSource transactionAttributeSource = new CustomAnnotationTransactionAttributeSource();
    transactionAttributeSource.setRepositoryInformation(repositoryInformation);
    transactionAttributeSource.setEnableDefaultTransactions(enableDefaultTransactions);
    // TODO: Remove
    @SuppressWarnings("null") TransactionInterceptor transactionInterceptor = new TransactionInterceptor(null, transactionAttributeSource);
    transactionInterceptor.setTransactionManagerBeanName(transactionManagerName);
    transactionInterceptor.setBeanFactory(beanFactory);
    transactionInterceptor.afterPropertiesSet();
    factory.addAdvice(transactionInterceptor);
}
Also used : TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor)

Example 10 with TransactionInterceptor

use of org.springframework.transaction.interceptor.TransactionInterceptor in project spring-integration by spring-projects.

the class DelayerParserTests method transactionalSubElement.

// INT-2649
@Test
public void transactionalSubElement() throws Exception {
    Object endpoint = context.getBean("delayerWithTransactional");
    DelayHandler delayHandler = TestUtils.getPropertyValue(endpoint, "handler", DelayHandler.class);
    List<?> adviceChain = TestUtils.getPropertyValue(delayHandler, "delayedAdviceChain", List.class);
    assertEquals(1, adviceChain.size());
    Object advice = adviceChain.get(0);
    assertTrue(advice instanceof TransactionInterceptor);
    TransactionAttributeSource transactionAttributeSource = ((TransactionInterceptor) advice).getTransactionAttributeSource();
    assertTrue(transactionAttributeSource instanceof MatchAlwaysTransactionAttributeSource);
    Method method = MessageHandler.class.getMethod("handleMessage", Message.class);
    TransactionDefinition definition = transactionAttributeSource.getTransactionAttribute(method, null);
    assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, definition.getPropagationBehavior());
    assertEquals(TransactionDefinition.ISOLATION_DEFAULT, definition.getIsolationLevel());
    assertEquals(TransactionDefinition.TIMEOUT_DEFAULT, definition.getTimeout());
    assertFalse(definition.isReadOnly());
}
Also used : NameMatchTransactionAttributeSource(org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource) TransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource) MatchAlwaysTransactionAttributeSource(org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource) TransactionDefinition(org.springframework.transaction.TransactionDefinition) DelayHandler(org.springframework.integration.handler.DelayHandler) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) Method(java.lang.reflect.Method) MatchAlwaysTransactionAttributeSource(org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource) Test(org.junit.Test)

Aggregations

TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)19 Test (org.junit.jupiter.api.Test)8 ProxyFactory (org.springframework.aop.framework.ProxyFactory)7 Test (org.junit.Test)4 TransactionAttributeSource (org.springframework.transaction.interceptor.TransactionAttributeSource)4 TransactionInterceptorBuilder (org.springframework.integration.transaction.TransactionInterceptorBuilder)3 NameMatchTransactionAttributeSource (org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource)3 Advised (org.springframework.aop.framework.Advised)2 Bean (org.springframework.context.annotation.Bean)2 Role (org.springframework.context.annotation.Role)2 DelayHandler (org.springframework.integration.handler.DelayHandler)2 MatchAlwaysTransactionAttributeSource (org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Advice (org.aopalliance.aop.Advice)1 Advisor (org.springframework.aop.Advisor)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1