Search in sources :

Example 11 with TransactionInterceptor

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

the class DelayerParserTests method adviceChainSubElement.

// INT-2649
@Test
public void adviceChainSubElement() {
    Object endpoint = context.getBean("delayerWithAdviceChain");
    DelayHandler delayHandler = TestUtils.getPropertyValue(endpoint, "handler", DelayHandler.class);
    List<?> adviceChain = TestUtils.getPropertyValue(delayHandler, "delayedAdviceChain", List.class);
    assertEquals(2, adviceChain.size());
    assertSame(context.getBean("testAdviceBean"), adviceChain.get(0));
    Object txAdvice = adviceChain.get(1);
    assertEquals(TransactionInterceptor.class, txAdvice.getClass());
    TransactionAttributeSource transactionAttributeSource = ((TransactionInterceptor) txAdvice).getTransactionAttributeSource();
    assertEquals(NameMatchTransactionAttributeSource.class, transactionAttributeSource.getClass());
    HashMap<?, ?> nameMap = TestUtils.getPropertyValue(transactionAttributeSource, "nameMap", HashMap.class);
    assertEquals("{*=PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT,readOnly}", nameMap.toString());
}
Also used : NameMatchTransactionAttributeSource(org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource) TransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource) MatchAlwaysTransactionAttributeSource(org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource) DelayHandler(org.springframework.integration.handler.DelayHandler) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) Test(org.junit.Test)

Example 12 with TransactionInterceptor

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

the class IntegrationNamespaceUtils method configureTransactionAttributes.

/**
 * Parse a "transactional" element and configure a {@link TransactionInterceptor}
 * or {@link TransactionHandleMessageAdvice}
 * with "transactionManager" and other "transactionDefinition" properties.
 * For example, this advisor will be applied on the Polling Task proxy.
 * @param txElement The transactional element.
 * @param handleMessageAdvice flag if to use {@link TransactionHandleMessageAdvice}
 * or regular {@link TransactionInterceptor}
 * @return The bean definition.
 * @see AbstractPollingEndpoint
 */
public static BeanDefinition configureTransactionAttributes(Element txElement, boolean handleMessageAdvice) {
    BeanDefinition txDefinition = configureTransactionDefinition(txElement);
    BeanDefinitionBuilder attributeSourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MatchAlwaysTransactionAttributeSource.class);
    attributeSourceBuilder.addPropertyValue("transactionAttribute", txDefinition);
    BeanDefinitionBuilder txInterceptorBuilder = BeanDefinitionBuilder.genericBeanDefinition(handleMessageAdvice ? TransactionHandleMessageAdvice.class : TransactionInterceptor.class);
    txInterceptorBuilder.addPropertyReference("transactionManager", txElement.getAttribute("transaction-manager"));
    txInterceptorBuilder.addPropertyValue("transactionAttributeSource", attributeSourceBuilder.getBeanDefinition());
    return txInterceptorBuilder.getBeanDefinition();
}
Also used : TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) TransactionHandleMessageAdvice(org.springframework.integration.transaction.TransactionHandleMessageAdvice) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 13 with TransactionInterceptor

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

the class PollerSpec method transactional.

/**
 * Specify a {@link TransactionInterceptor} {@link Advice} with default {@code PlatformTransactionManager}
 * and {@link DefaultTransactionAttribute} for the {@code pollingTask}.
 * @return the spec.
 */
public PollerSpec transactional() {
    TransactionInterceptor transactionInterceptor = new TransactionInterceptorBuilder().build();
    this.componentsToRegister.put(transactionInterceptor, null);
    return transactional(transactionInterceptor);
}
Also used : TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) TransactionInterceptorBuilder(org.springframework.integration.transaction.TransactionInterceptorBuilder)

Example 14 with TransactionInterceptor

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

the class TxNamespaceHandlerTests method rollbackRules.

@Test
public void rollbackRules() {
    TransactionInterceptor txInterceptor = (TransactionInterceptor) context.getBean("txRollbackAdvice");
    TransactionAttributeSource txAttrSource = txInterceptor.getTransactionAttributeSource();
    TransactionAttribute txAttr = txAttrSource.getTransactionAttribute(getAgeMethod, ITestBean.class);
    assertThat(txAttr.rollbackOn(new Exception())).as("should be configured to rollback on Exception").isTrue();
    txAttr = txAttrSource.getTransactionAttribute(setAgeMethod, ITestBean.class);
    assertThat(txAttr.rollbackOn(new RuntimeException())).as("should not rollback on RuntimeException").isFalse();
}
Also used : TransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) Test(org.junit.jupiter.api.Test)

Example 15 with TransactionInterceptor

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

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