1 16 17 package org.springframework.transaction.support; 18 19 import java.io.Serializable ; 20 21 import org.springframework.core.Constants; 22 import org.springframework.transaction.TransactionDefinition; 23 24 36 public class DefaultTransactionDefinition implements TransactionDefinition, Serializable { 37 38 39 public static final String PREFIX_PROPAGATION = "PROPAGATION_"; 40 41 42 public static final String PREFIX_ISOLATION = "ISOLATION_"; 43 44 45 public static final String PREFIX_TIMEOUT = "timeout_"; 46 47 48 public static final String READ_ONLY_MARKER = "readOnly"; 49 50 51 52 private static final Constants constants = new Constants(TransactionDefinition.class); 53 54 private int propagationBehavior = PROPAGATION_REQUIRED; 55 56 private int isolationLevel = ISOLATION_DEFAULT; 57 58 private int timeout = TIMEOUT_DEFAULT; 59 60 private boolean readOnly = false; 61 62 private String name; 63 64 65 74 public DefaultTransactionDefinition() { 75 } 76 77 85 public DefaultTransactionDefinition(TransactionDefinition other) { 86 this.propagationBehavior = other.getPropagationBehavior(); 87 this.isolationLevel = other.getIsolationLevel(); 88 this.timeout = other.getTimeout(); 89 this.readOnly = other.isReadOnly(); 90 this.name = other.getName(); 91 } 92 93 102 public DefaultTransactionDefinition(int propagationBehavior) { 103 this.propagationBehavior = propagationBehavior; 104 } 105 106 107 116 public final void setPropagationBehaviorName(String constantName) throws IllegalArgumentException { 117 if (constantName == null || !constantName.startsWith(PREFIX_PROPAGATION)) { 118 throw new IllegalArgumentException ("Only propagation constants allowed"); 119 } 120 setPropagationBehavior(constants.asNumber(constantName).intValue()); 121 } 122 123 130 public final void setPropagationBehavior(int propagationBehavior) { 131 if (!constants.getValues(PREFIX_PROPAGATION).contains(new Integer (propagationBehavior))) { 132 throw new IllegalArgumentException ("Only values of propagation constants allowed"); 133 } 134 this.propagationBehavior = propagationBehavior; 135 } 136 137 public final int getPropagationBehavior() { 138 return this.propagationBehavior; 139 } 140 141 150 public final void setIsolationLevelName(String constantName) throws IllegalArgumentException { 151 if (constantName == null || !constantName.startsWith(PREFIX_ISOLATION)) { 152 throw new IllegalArgumentException ("Only isolation constants allowed"); 153 } 154 setIsolationLevel(constants.asNumber(constantName).intValue()); 155 } 156 157 164 public final void setIsolationLevel(int isolationLevel) { 165 if (!constants.getValues(PREFIX_ISOLATION).contains(new Integer (isolationLevel))) { 166 throw new IllegalArgumentException ("Only values of isolation constants allowed"); 167 } 168 this.isolationLevel = isolationLevel; 169 } 170 171 public final int getIsolationLevel() { 172 return this.isolationLevel; 173 } 174 175 180 public final void setTimeout(int timeout) { 181 if (timeout < TIMEOUT_DEFAULT) { 182 throw new IllegalArgumentException ("Timeout must be a positive integer or TIMEOUT_DEFAULT"); 183 } 184 this.timeout = timeout; 185 } 186 187 public final int getTimeout() { 188 return this.timeout; 189 } 190 191 195 public final void setReadOnly(boolean readOnly) { 196 this.readOnly = readOnly; 197 } 198 199 public final boolean isReadOnly() { 200 return this.readOnly; 201 } 202 203 208 public final void setName(String name) { 209 this.name = name; 210 } 211 212 public final String getName() { 213 return this.name; 214 } 215 216 217 221 public boolean equals(Object other) { 222 return (other instanceof TransactionDefinition && toString().equals(other.toString())); 223 } 224 225 229 public int hashCode() { 230 return toString().hashCode(); 231 } 232 233 245 public String toString() { 246 return getDefinitionDescription().toString(); 247 } 248 249 253 protected final StringBuffer getDefinitionDescription() { 254 StringBuffer desc = new StringBuffer (); 255 desc.append(constants.toCode(new Integer (this.propagationBehavior), PREFIX_PROPAGATION)); 256 desc.append(','); 257 desc.append(constants.toCode(new Integer (this.isolationLevel), PREFIX_ISOLATION)); 258 if (this.timeout != TIMEOUT_DEFAULT) { 259 desc.append(','); 260 desc.append(PREFIX_TIMEOUT + this.timeout); 261 } 262 if (this.readOnly) { 263 desc.append(','); 264 desc.append(READ_ONLY_MARKER); 265 } 266 return desc; 267 } 268 269 } 270 | Popular Tags |