001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.transaction;
016    
017    import com.liferay.portal.cache.transactional.TransactionalPortalCacheHelper;
018    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
019    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.spring.hibernate.LastSessionRecorderUtil;
023    
024    import org.aopalliance.intercept.MethodInvocation;
025    
026    import org.springframework.transaction.PlatformTransactionManager;
027    import org.springframework.transaction.TransactionStatus;
028    import org.springframework.transaction.TransactionSystemException;
029    import org.springframework.transaction.interceptor.TransactionAttribute;
030    
031    /**
032     * @author Michael C. Han
033     * @author Shuyang Zhou
034     */
035    public class DefaultTransactionExecutor extends BaseTransactionExecutor {
036    
037            public Object execute(
038                            PlatformTransactionManager platformTransactionManager,
039                            TransactionAttribute transactionAttribute,
040                            MethodInvocation methodInvocation)
041                    throws Throwable {
042    
043                    TransactionStatus transactionStatus =
044                            platformTransactionManager.getTransaction(transactionAttribute);
045    
046                    boolean newTransaction = transactionStatus.isNewTransaction();
047    
048                    if (newTransaction) {
049                            TransactionalPortalCacheHelper.begin();
050    
051                            TransactionCommitCallbackUtil.pushCallbackList();
052                    }
053    
054                    Object returnValue = null;
055    
056                    try {
057                            if (newTransaction) {
058                                    LastSessionRecorderUtil.syncLastSessionState();
059                            }
060    
061                            returnValue = methodInvocation.proceed();
062                    }
063                    catch (Throwable throwable) {
064                            processThrowable(
065                                    platformTransactionManager, throwable, transactionAttribute,
066                                    transactionStatus);
067                    }
068    
069                    processCommit(platformTransactionManager, transactionStatus);
070    
071                    return returnValue;
072            }
073    
074            protected void processCommit(
075                    PlatformTransactionManager platformTransactionManager,
076                    TransactionStatus transactionStatus) {
077    
078                    boolean hasError = false;
079    
080                    try {
081                            platformTransactionManager.commit(transactionStatus);
082                    }
083                    catch (TransactionSystemException tse) {
084                            _log.error(
085                                    "Application exception overridden by commit exception", tse);
086    
087                            hasError = true;
088    
089                            throw tse;
090                    }
091                    catch (RuntimeException re) {
092                            _log.error(
093                                    "Application exception overridden by commit exception", re);
094    
095                            hasError = true;
096    
097                            throw re;
098                    }
099                    catch (Error e) {
100                            _log.error("Application exception overridden by commit error", e);
101    
102                            hasError = true;
103    
104                            throw e;
105                    }
106                    finally {
107                            if (transactionStatus.isNewTransaction()) {
108                                    if (hasError) {
109                                            TransactionalPortalCacheHelper.rollback();
110    
111                                            TransactionCommitCallbackUtil.popCallbackList();
112    
113                                            EntityCacheUtil.clearLocalCache();
114                                            FinderCacheUtil.clearLocalCache();
115                                    }
116                                    else {
117                                            TransactionalPortalCacheHelper.commit();
118    
119                                            invokeCallbacks();
120                                    }
121                            }
122                    }
123            }
124    
125            protected void processThrowable(
126                            PlatformTransactionManager platformTransactionManager,
127                            Throwable throwable, TransactionAttribute transactionAttribute,
128                            TransactionStatus transactionStatus)
129                    throws Throwable {
130    
131                    if (transactionAttribute.rollbackOn(throwable)) {
132                            try {
133                                    platformTransactionManager.rollback(transactionStatus);
134                            }
135                            catch (TransactionSystemException tse) {
136                                    _log.error(
137                                            "Application exception overridden by rollback exception",
138                                            tse);
139    
140                                    throw tse;
141                            }
142                            catch (RuntimeException re) {
143                                    _log.error(
144                                            "Application exception overridden by rollback exception",
145                                            re);
146    
147                                    throw re;
148                            }
149                            catch (Error e) {
150                                    _log.error(
151                                            "Application exception overridden by rollback error", e);
152    
153                                    throw e;
154                            }
155                            finally {
156                                    if (transactionStatus.isNewTransaction()) {
157                                            TransactionalPortalCacheHelper.rollback();
158    
159                                            TransactionCommitCallbackUtil.popCallbackList();
160    
161                                            EntityCacheUtil.clearLocalCache();
162                                            FinderCacheUtil.clearLocalCache();
163                                    }
164                            }
165                    }
166                    else {
167                            processCommit(platformTransactionManager, transactionStatus);
168                    }
169    
170                    throw throwable;
171            }
172    
173            private static Log _log = LogFactoryUtil.getLog(
174                    DefaultTransactionExecutor.class);
175    
176    }