Search in sources :

Example 6 with RuleBasedTransactionAttribute

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

the class SpringTransactionAnnotationParser method parseTransactionAnnotation.

protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    Propagation propagation = attributes.getEnum("propagation");
    rbta.setPropagationBehavior(propagation.value());
    Isolation isolation = attributes.getEnum("isolation");
    rbta.setIsolationLevel(isolation.value());
    rbta.setTimeout(attributes.getNumber("timeout").intValue());
    String timeoutString = attributes.getString("timeoutString");
    Assert.isTrue(!StringUtils.hasText(timeoutString) || rbta.getTimeout() < 0, "Specify 'timeout' or 'timeoutString', not both");
    rbta.setTimeoutString(timeoutString);
    rbta.setReadOnly(attributes.getBoolean("readOnly"));
    rbta.setQualifier(attributes.getString("value"));
    rbta.setLabels(Arrays.asList(attributes.getStringArray("label")));
    List<RollbackRuleAttribute> rollbackRules = new ArrayList<>();
    for (Class<?> rbRule : attributes.getClassArray("rollbackFor")) {
        rollbackRules.add(new RollbackRuleAttribute(rbRule));
    }
    for (String rbRule : attributes.getStringArray("rollbackForClassName")) {
        rollbackRules.add(new RollbackRuleAttribute(rbRule));
    }
    for (Class<?> rbRule : attributes.getClassArray("noRollbackFor")) {
        rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
    }
    for (String rbRule : attributes.getStringArray("noRollbackForClassName")) {
        rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
    }
    rbta.setRollbackRules(rollbackRules);
    return rbta;
}
Also used : RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) ArrayList(java.util.ArrayList) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute)

Example 7 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method defaultsToClassTransactionAttribute.

/**
 * Test that transaction attribute is inherited from class
 * if not specified on method.
 */
@Test
public void defaultsToClassTransactionAttribute() throws Exception {
    Method method = TestBean4.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean4.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 8 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method customClassAttributeWithReadOnlyOverrideOnInterface.

@Test
public void customClassAttributeWithReadOnlyOverrideOnInterface() throws Exception {
    Method method = TestInterface9.class.getMethod("getAge");
    Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
    assertThat(annotation).as("AnnotationUtils.findAnnotation should not find @Transactional for TestBean9.getAge()").isNull();
    annotation = AnnotationUtils.findAnnotation(TestBean9.class, Transactional.class);
    assertThat(annotation).as("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean9").isNotNull();
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean9.class);
    assertThat(actual).as("Failed to retrieve TransactionAttribute for TestBean9.getAge()").isNotNull();
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
    assertThat(actual.isReadOnly()).isTrue();
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 9 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method transactionAttributeDeclaredOnInterfaceMethodOnly.

/**
 * Test case where attribute is on the interface method.
 */
@Test
public void transactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception {
    Method interfaceMethod = ITestBean2.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean2.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 10 with RuleBasedTransactionAttribute

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

the class AnnotationTransactionAttributeSourceTests method customMethodAttributeDetected.

@Test
public void customMethodAttributeDetected() throws Exception {
    Method method = TestBean6.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean6.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertThat(((RuleBasedTransactionAttribute) actual).getRollbackRules()).isEqualTo(rbta.getRollbackRules());
}
Also used : TransactionAttribute(org.springframework.transaction.interceptor.TransactionAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) Method(java.lang.reflect.Method) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

RuleBasedTransactionAttribute (org.springframework.transaction.interceptor.RuleBasedTransactionAttribute)15 NoRollbackRuleAttribute (org.springframework.transaction.interceptor.NoRollbackRuleAttribute)14 RollbackRuleAttribute (org.springframework.transaction.interceptor.RollbackRuleAttribute)14 Method (java.lang.reflect.Method)12 Test (org.junit.jupiter.api.Test)12 TransactionAttribute (org.springframework.transaction.interceptor.TransactionAttribute)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)3 GroovyObject (groovy.lang.GroovyObject)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 Element (org.w3c.dom.Element)1