Search in sources :

Example 1 with NoRollbackRuleAttribute

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

the class AnnotationTransactionAttributeSourceTests method customClassAttributeDetected.

@Test
public void customClassAttributeDetected() throws Exception {
    Method method = TestBean5.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean5.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).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.Test)

Example 2 with NoRollbackRuleAttribute

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

the class AnnotationTransactionAttributeSourceTests method rollbackRulesAreApplied.

@Test
public void rollbackRulesAreApplied() throws Exception {
    Method method = TestBean3.class.getMethod("getAge");
    AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
    TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean3.class);
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute("java.lang.Exception"));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
    assertTrue(actual.rollbackOn(new Exception()));
    assertFalse(actual.rollbackOn(new IOException()));
    actual = atas.getTransactionAttribute(method, method.getDeclaringClass());
    rbta = new RuleBasedTransactionAttribute();
    rbta.getRollbackRules().add(new RollbackRuleAttribute("java.lang.Exception"));
    rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
    assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
    assertTrue(actual.rollbackOn(new Exception()));
    assertFalse(actual.rollbackOn(new IOException()));
}
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.Test)

Example 3 with NoRollbackRuleAttribute

use of org.springframework.transaction.interceptor.NoRollbackRuleAttribute in project grails-core by grails.

the class GrailsTransactionAttribute method rollbackOn.

@Override
public boolean rollbackOn(Throwable ex) {
    if (log.isTraceEnabled()) {
        log.trace("Applying rules to determine whether transaction should rollback on $ex");
    }
    RollbackRuleAttribute winner = null;
    int deepest = Integer.MAX_VALUE;
    List<RollbackRuleAttribute> rollbackRules = getRollbackRules();
    if (rollbackRules != null) {
        for (RollbackRuleAttribute rule : rollbackRules) {
            int depth = rule.getDepth(ex);
            if (depth >= 0 && depth < deepest) {
                deepest = depth;
                winner = rule;
            }
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("Winning rollback rule is: $winner");
    }
    // User superclass behavior (rollback on unchecked) if no rule matches.
    if (winner == null) {
        log.trace("No relevant rollback rule found: applying default rules");
        // always rollback regardless if it is a checked or unchecked exception since Groovy doesn't differentiate those
        return true;
    }
    return !(winner instanceof NoRollbackRuleAttribute);
}
Also used : NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute)

Example 4 with NoRollbackRuleAttribute

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

the class JtaTransactionAnnotationParser method parseTransactionAnnotation.

protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
    rbta.setPropagationBehaviorName(RuleBasedTransactionAttribute.PREFIX_PROPAGATION + attributes.getEnum("value").toString());
    ArrayList<RollbackRuleAttribute> rollBackRules = new ArrayList<>();
    Class<?>[] rbf = attributes.getClassArray("rollbackOn");
    for (Class<?> rbRule : rbf) {
        RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    Class<?>[] nrbf = attributes.getClassArray("dontRollbackOn");
    for (Class<?> rbRule : nrbf) {
        NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    rbta.getRollbackRules().addAll(rollBackRules);
    return rbta;
}
Also used : NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) ArrayList(java.util.ArrayList) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute)

Example 5 with NoRollbackRuleAttribute

use of org.springframework.transaction.interceptor.NoRollbackRuleAttribute 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());
    rbta.setReadOnly(attributes.getBoolean("readOnly"));
    rbta.setQualifier(attributes.getString("value"));
    ArrayList<RollbackRuleAttribute> rollBackRules = new ArrayList<>();
    Class<?>[] rbf = attributes.getClassArray("rollbackFor");
    for (Class<?> rbRule : rbf) {
        RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    String[] rbfc = attributes.getStringArray("rollbackForClassName");
    for (String rbRule : rbfc) {
        RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    Class<?>[] nrbf = attributes.getClassArray("noRollbackFor");
    for (Class<?> rbRule : nrbf) {
        NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    String[] nrbfc = attributes.getStringArray("noRollbackForClassName");
    for (String rbRule : nrbfc) {
        NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    rbta.getRollbackRules().addAll(rollBackRules);
    return rbta;
}
Also used : NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute) RollbackRuleAttribute(org.springframework.transaction.interceptor.RollbackRuleAttribute) RuleBasedTransactionAttribute(org.springframework.transaction.interceptor.RuleBasedTransactionAttribute) ArrayList(java.util.ArrayList) NoRollbackRuleAttribute(org.springframework.transaction.interceptor.NoRollbackRuleAttribute)

Aggregations

NoRollbackRuleAttribute (org.springframework.transaction.interceptor.NoRollbackRuleAttribute)12 RollbackRuleAttribute (org.springframework.transaction.interceptor.RollbackRuleAttribute)12 RuleBasedTransactionAttribute (org.springframework.transaction.interceptor.RuleBasedTransactionAttribute)11 IOException (java.io.IOException)9 Method (java.lang.reflect.Method)9 Test (org.junit.Test)9 TransactionAttribute (org.springframework.transaction.interceptor.TransactionAttribute)9 ArrayList (java.util.ArrayList)2