1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 41 public class RuleBasedTransactionAttribute extends DefaultTransactionAttribute implements Serializable { 42 43 44 public static final String PREFIX_ROLLBACK_RULE = "-"; 45 46 47 public static final String PREFIX_COMMIT_RULE = "+"; 48 49 50 51 private static final Log logger = LogFactory.getLog(RuleBasedTransactionAttribute.class); 52 53 private List rollbackRules; 54 55 56 66 public RuleBasedTransactionAttribute() { 67 super(); 68 } 69 70 79 public RuleBasedTransactionAttribute(RuleBasedTransactionAttribute other) { 80 super(other); 81 this.rollbackRules = new ArrayList (other.rollbackRules); 82 } 83 84 94 public RuleBasedTransactionAttribute(int propagationBehavior, List rollbackRules) { 95 super(propagationBehavior); 96 this.rollbackRules = rollbackRules; 97 } 98 99 100 106 public void setRollbackRules(List rollbackRules) { 107 this.rollbackRules = rollbackRules; 108 } 109 110 114 public List getRollbackRules() { 115 if (this.rollbackRules == null) { 116 this.rollbackRules = new LinkedList (); 117 } 118 return this.rollbackRules; 119 } 120 121 122 128 public boolean rollbackOn(Throwable ex) { 129 if (logger.isDebugEnabled()) { 130 logger.debug("Applying rules to determine whether transaction should rollback on " + ex); 131 } 132 133 RollbackRuleAttribute winner = null; 134 int deepest = Integer.MAX_VALUE; 135 136 if (this.rollbackRules != null) { 137 for (Iterator it = this.rollbackRules.iterator(); it.hasNext();) { 138 RollbackRuleAttribute rule = (RollbackRuleAttribute) it.next(); 139 int depth = rule.getDepth(ex); 140 if (depth >= 0 && depth < deepest) { 141 deepest = depth; 142 winner = rule; 143 } 144 } 145 } 146 147 if (logger.isDebugEnabled()) { 148 logger.debug("Winning rollback rule is: " + winner); 149 } 150 151 if (winner == null) { 153 logger.debug("No relevant rollback rule found: applying superclass default"); 154 return super.rollbackOn(ex); 155 } 156 157 return !(winner instanceof NoRollbackRuleAttribute); 158 } 159 160 161 public String toString() { 162 StringBuffer result = getDefinitionDescription(); 163 if (this.rollbackRules != null) { 164 for (Iterator it = this.rollbackRules.iterator(); it.hasNext();) { 165 RollbackRuleAttribute rule = (RollbackRuleAttribute) it.next(); 166 String sign = (rule instanceof NoRollbackRuleAttribute ? PREFIX_COMMIT_RULE : PREFIX_ROLLBACK_RULE); 167 result.append(',').append(sign).append(rule.getExceptionName()); 168 } 169 } 170 return result.toString(); 171 } 172 173 } 174 | Popular Tags |