1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.Serializable ; 23 import java.util.Properties ; 24 25 import org.aopalliance.intercept.MethodInterceptor; 26 import org.aopalliance.intercept.MethodInvocation; 27 28 import org.springframework.transaction.PlatformTransactionManager; 29 import org.springframework.transaction.TransactionStatus; 30 import org.springframework.transaction.support.CallbackPreferringPlatformTransactionManager; 31 import org.springframework.transaction.support.TransactionCallback; 32 33 52 public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable { 53 54 61 public TransactionInterceptor() { 62 } 63 64 71 public TransactionInterceptor(PlatformTransactionManager ptm, Properties attributes) { 72 setTransactionManager(ptm); 73 setTransactionAttributes(attributes); 74 } 75 76 83 public TransactionInterceptor(PlatformTransactionManager ptm, TransactionAttributeSource tas) { 84 setTransactionManager(ptm); 85 setTransactionAttributeSource(tas); 86 } 87 88 89 public Object invoke(final MethodInvocation invocation) throws Throwable { 90 Class targetClass = (invocation.getThis() != null ? invocation.getThis().getClass() : null); 94 95 final TransactionAttribute txAttr = 97 getTransactionAttributeSource().getTransactionAttribute(invocation.getMethod(), targetClass); 98 final String joinpointIdentification = methodIdentification(invocation.getMethod()); 99 100 if (txAttr == null || !(getTransactionManager() instanceof CallbackPreferringPlatformTransactionManager)) { 101 TransactionInfo txInfo = createTransactionIfNecessary(txAttr, joinpointIdentification); 103 Object retVal = null; 104 try { 105 retVal = invocation.proceed(); 108 } 109 catch (Throwable ex) { 110 completeTransactionAfterThrowing(txInfo, ex); 112 throw ex; 113 } 114 finally { 115 cleanupTransactionInfo(txInfo); 116 } 117 commitTransactionAfterReturning(txInfo); 118 return retVal; 119 } 120 121 else { 122 try { 124 Object result = ((CallbackPreferringPlatformTransactionManager) getTransactionManager()).execute(txAttr, 125 new TransactionCallback() { 126 public Object doInTransaction(TransactionStatus status) { 127 TransactionInfo txInfo = prepareTransactionInfo(txAttr, joinpointIdentification, status); 128 try { 129 return invocation.proceed(); 130 } 131 catch (Throwable ex) { 132 if (txAttr.rollbackOn(ex)) { 133 throw new ThrowableHolderException(ex); 135 } 136 else { 137 return new ThrowableHolder(ex); 139 } 140 } 141 finally { 142 cleanupTransactionInfo(txInfo); 143 } 144 } 145 }); 146 147 if (result instanceof ThrowableHolder) { 149 throw ((ThrowableHolder) result).getThrowable(); 150 } 151 else { 152 return result; 153 } 154 } 155 catch (ThrowableHolderException ex) { 156 throw ex.getThrowable(); 157 } 158 } 159 } 160 161 162 166 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 167 ois.defaultReadObject(); 169 170 setTransactionManager((PlatformTransactionManager) ois.readObject()); 174 setTransactionAttributeSource((TransactionAttributeSource) ois.readObject()); 175 } 176 177 private void writeObject(ObjectOutputStream oos) throws IOException { 178 oos.defaultWriteObject(); 180 181 oos.writeObject(getTransactionManager()); 183 oos.writeObject(getTransactionAttributeSource()); 184 } 185 186 187 191 private static class ThrowableHolder { 192 193 private final Throwable throwable; 194 195 public ThrowableHolder(Throwable throwable) { 196 this.throwable = throwable; 197 } 198 199 public Throwable getThrowable() { 200 return throwable; 201 } 202 } 203 204 205 209 private static class ThrowableHolderException extends RuntimeException { 210 211 private final Throwable throwable; 212 213 public ThrowableHolderException(Throwable throwable) { 214 super(throwable.toString()); 215 this.throwable = throwable; 216 } 217 218 public Throwable getThrowable() { 219 return throwable; 220 } 221 222 public String toString() { 223 return this.throwable.toString(); 224 } 225 } 226 227 } 228 | Popular Tags |