Example usage for org.springframework.transaction.interceptor RuleBasedTransactionAttribute setRollbackRules

List of usage examples for org.springframework.transaction.interceptor RuleBasedTransactionAttribute setRollbackRules

Introduction

In this page you can find the example usage for org.springframework.transaction.interceptor RuleBasedTransactionAttribute setRollbackRules.

Prototype

public void setRollbackRules(List<RollbackRuleAttribute> rollbackRules) 

Source Link

Document

Set the list of RollbackRuleAttribute objects (and/or NoRollbackRuleAttribute objects) to apply.

Usage

From source file:eap.config.TxAdviceBeanDefinitionParser.java

private RootBeanDefinition parseAttributeSource(Element attrEle, ParserContext parserContext) {
    List<Element> methods = DomUtils.getChildElementsByTagName(attrEle, METHOD_ELEMENT);
    ManagedMap<TypedStringValue, RuleBasedTransactionAttribute> transactionAttributeMap = new ManagedMap<TypedStringValue, RuleBasedTransactionAttribute>(
            methods.size());//from   w  ww .j  a v a 2  s. c o m
    transactionAttributeMap.setSource(parserContext.extractSource(attrEle));

    for (Element methodEle : methods) {
        String name = methodEle.getAttribute(METHOD_NAME_ATTRIBUTE);
        TypedStringValue nameHolder = new TypedStringValue(name);
        nameHolder.setSource(parserContext.extractSource(methodEle));

        RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
        String propagation = methodEle.getAttribute(PROPAGATION_ATTRIBUTE);
        String isolation = methodEle.getAttribute(ISOLATION_ATTRIBUTE);
        String timeout = methodEle.getAttribute(TIMEOUT_ATTRIBUTE);
        String readOnly = methodEle.getAttribute(READ_ONLY_ATTRIBUTE);
        if (StringUtils.hasText(propagation)) {
            attribute
                    .setPropagationBehaviorName(RuleBasedTransactionAttribute.PREFIX_PROPAGATION + propagation);
        }
        if (StringUtils.hasText(isolation)) {
            attribute.setIsolationLevelName(RuleBasedTransactionAttribute.PREFIX_ISOLATION + isolation);
        }
        if (StringUtils.hasText(timeout)) {
            try {
                attribute.setTimeout(Integer.parseInt(timeout));
            } catch (NumberFormatException ex) {
                parserContext.getReaderContext().error("Timeout must be an integer value: [" + timeout + "]",
                        methodEle);
            }
        }
        if (StringUtils.hasText(readOnly)) {
            attribute.setReadOnly(Boolean.valueOf(methodEle.getAttribute(READ_ONLY_ATTRIBUTE)));
        }

        List<RollbackRuleAttribute> rollbackRules = new LinkedList<RollbackRuleAttribute>();
        if (methodEle.hasAttribute(ROLLBACK_FOR_ATTRIBUTE)) {
            String rollbackForValue = methodEle.getAttribute(ROLLBACK_FOR_ATTRIBUTE);
            addRollbackRuleAttributesTo(rollbackRules, rollbackForValue);
        }
        if (methodEle.hasAttribute(NO_ROLLBACK_FOR_ATTRIBUTE)) {
            String noRollbackForValue = methodEle.getAttribute(NO_ROLLBACK_FOR_ATTRIBUTE);
            addNoRollbackRuleAttributesTo(rollbackRules, noRollbackForValue);
        }
        attribute.setRollbackRules(rollbackRules);

        transactionAttributeMap.put(nameHolder, attribute);
    }

    RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(
            NameMatchTransactionAttributeSource.class);
    attributeSourceDefinition.setSource(parserContext.extractSource(attrEle));
    attributeSourceDefinition.getPropertyValues().add("nameMap", transactionAttributeMap);
    return attributeSourceDefinition;
}

From source file:org.springframework.transaction.interceptor.AttributesTransactionAttributeSource.java

/**
 * Return the transaction attribute, given this set of attributes
 * attached to a method or class./*  w  ww.j a  v a 2 s.  c o  m*/
 * Protected rather than private as subclasses may want to customize
 * how this is done: for example, returning a TransactionAttribute
 * affected by the values of other attributes.
 * This implementation takes into account RollbackRuleAttributes, if
 * the TransactionAttribute is a RuleBasedTransactionAttribute.
 * Return null if it's not transactional. 
 * @param atts attributes attached to a method or class. May
 * be null, in which case a null TransactionAttribute will be returned.
 * @return TransactionAttribute configured transaction attribute, or null
 * if none was found
 */
protected TransactionAttribute findTransactionAttribute(Collection atts) {
    if (atts == null)
        return null;
    TransactionAttribute txAttribute = null;
    // Check there is a transaction attribute
    for (Iterator itr = atts.iterator(); itr.hasNext() && txAttribute == null;) {
        Object att = itr.next();
        if (att instanceof TransactionAttribute) {
            txAttribute = (TransactionAttribute) att;
        }
    }

    if (txAttribute instanceof RuleBasedTransactionAttribute) {
        RuleBasedTransactionAttribute rbta = (RuleBasedTransactionAttribute) txAttribute;
        // We really want value: bit of a hack
        List l = new LinkedList();
        for (Iterator itr = atts.iterator(); itr.hasNext();) {
            Object att = itr.next();
            if (att instanceof RollbackRuleAttribute) {
                if (logger.isDebugEnabled())
                    logger.debug("Found RollbackRule " + att);
                l.add(att);
            }
        }
        // Repeatedly setting this isn't elegant, but it works
        rbta.setRollbackRules(l);
    }

    return txAttribute;
}