1
14
15 package com.liferay.portal.spring.transaction;
16
17 import com.liferay.portal.cache.transactional.TransactionalPortalCacheHelper;
18 import com.liferay.portal.kernel.log.Log;
19 import com.liferay.portal.kernel.log.LogFactoryUtil;
20 import com.liferay.portal.kernel.util.StringPool;
21
22 import java.lang.reflect.Method;
23
24 import org.aopalliance.intercept.MethodInterceptor;
25 import org.aopalliance.intercept.MethodInvocation;
26
27 import org.springframework.transaction.TransactionStatus;
28 import org.springframework.transaction.interceptor.TransactionAspectSupport;
29 import org.springframework.transaction.interceptor.TransactionAttribute;
30 import org.springframework.transaction.interceptor.TransactionAttributeSource;
31
32
37 public class TransactionInterceptor
38 extends TransactionAspectSupport implements MethodInterceptor {
39
40 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
41 Method method = methodInvocation.getMethod();
42
43 Class<?> targetClass = null;
44
45 Object thisObject = methodInvocation.getThis();
46
47 if (thisObject != null) {
48 targetClass = thisObject.getClass();
49 }
50
51 TransactionAttributeSource transactionAttributeSource =
52 getTransactionAttributeSource();
53
54 TransactionAttribute transactionAttribute =
55 transactionAttributeSource.getTransactionAttribute(
56 method, targetClass);
57
58 if (transactionAttribute == null) {
59 return methodInvocation.proceed();
60 }
61
62 Class<?> declaringClass = method.getDeclaringClass();
63
64 String joinPointIdentification = StringPool.BLANK;
65
66 if (_log.isDebugEnabled()) {
67 joinPointIdentification =
68 declaringClass.getName().concat(StringPool.PERIOD).concat(
69 method.getName());
70 }
71
72 TransactionInfo transactionInfo = createTransactionIfNecessary(
73 transactionAttribute, joinPointIdentification);
74
75 TransactionStatus transactionStatus =
76 transactionInfo.getTransactionStatus();
77
78 boolean newTransaction = transactionStatus.isNewTransaction();
79
80 if (newTransaction) {
81 TransactionalPortalCacheHelper.begin();
82 }
83
84 Object returnValue = null;
85
86 try {
87 returnValue = methodInvocation.proceed();
88 }
89 catch (Throwable throwable) {
90 if (newTransaction) {
91 TransactionalPortalCacheHelper.rollback();
92 }
93
94 completeTransactionAfterThrowing(transactionInfo, throwable);
95
96 throw throwable;
97 }
98 finally {
99 cleanupTransactionInfo(transactionInfo);
100 }
101
102 commitTransactionAfterReturning(transactionInfo);
103
104 if (newTransaction) {
105 TransactionalPortalCacheHelper.commit();
106 }
107
108 return returnValue;
109 }
110
111 private static Log _log = LogFactoryUtil.getLog(
112 TransactionInterceptor.class);
113
114 }