001    /**
002     * Copyright (c) 2000-present 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.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import org.aopalliance.intercept.MethodInvocation;
021    
022    import org.springframework.transaction.PlatformTransactionManager;
023    import org.springframework.transaction.TransactionStatus;
024    import org.springframework.transaction.TransactionSystemException;
025    import org.springframework.transaction.interceptor.TransactionAttribute;
026    
027    /**
028     * @author Michael C. Han
029     * @author Shuyang Zhou
030     */
031    public class DefaultTransactionExecutor
032            extends BaseTransactionExecutor implements TransactionHandler {
033    
034            @Override
035            public void commit(
036                    PlatformTransactionManager platformTransactionManager,
037                    TransactionAttribute transactionAttribute,
038                    TransactionStatus transactionStatus) {
039    
040                    Throwable throwable = null;
041    
042                    try {
043                            platformTransactionManager.commit(transactionStatus);
044                    }
045                    catch (TransactionSystemException tse) {
046                            _log.error(
047                                    "Application exception overridden by commit exception", tse);
048    
049                            throwable = tse;
050    
051                            throw tse;
052                    }
053                    catch (RuntimeException re) {
054                            _log.error(
055                                    "Application exception overridden by commit exception", re);
056    
057                            throwable = re;
058    
059                            throw re;
060                    }
061                    catch (Error e) {
062                            _log.error("Application exception overridden by commit error", e);
063    
064                            throwable = e;
065    
066                            throw e;
067                    }
068                    finally {
069                            if (transactionStatus.isNewTransaction()) {
070                                    if (throwable != null) {
071                                            fireTransactionRollbackedEvent(
072                                                    transactionAttribute, transactionStatus, throwable);
073                                    }
074                                    else {
075                                            fireTransactionCommittedEvent(
076                                                    transactionAttribute, transactionStatus);
077                                    }
078                            }
079                    }
080            }
081    
082            @Override
083            public Object execute(
084                            PlatformTransactionManager platformTransactionManager,
085                            TransactionAttribute transactionAttribute,
086                            MethodInvocation methodInvocation)
087                    throws Throwable {
088    
089                    TransactionStatus transactionStatus = start(
090                            platformTransactionManager, transactionAttribute);
091    
092                    Object returnValue = null;
093    
094                    try {
095                            returnValue = methodInvocation.proceed();
096                    }
097                    catch (Throwable throwable) {
098                            rollback(
099                                    platformTransactionManager, throwable, transactionAttribute,
100                                    transactionStatus);
101                    }
102    
103                    commit(
104                            platformTransactionManager, transactionAttribute,
105                            transactionStatus);
106    
107                    return returnValue;
108            }
109    
110            @Override
111            public void rollback(
112                            PlatformTransactionManager platformTransactionManager,
113                            Throwable throwable, TransactionAttribute transactionAttribute,
114                            TransactionStatus transactionStatus)
115                    throws Throwable {
116    
117                    if (transactionAttribute.rollbackOn(throwable)) {
118                            try {
119                                    platformTransactionManager.rollback(transactionStatus);
120                            }
121                            catch (TransactionSystemException tse) {
122                                    _log.error(
123                                            "Application exception overridden by rollback exception",
124                                            tse);
125    
126                                    throw tse;
127                            }
128                            catch (RuntimeException re) {
129                                    _log.error(
130                                            "Application exception overridden by rollback exception",
131                                            re);
132    
133                                    throw re;
134                            }
135                            catch (Error e) {
136                                    _log.error(
137                                            "Application exception overridden by rollback error", e);
138    
139                                    throw e;
140                            }
141                            finally {
142                                    if (transactionStatus.isNewTransaction()) {
143                                            fireTransactionRollbackedEvent(
144                                                    transactionAttribute, transactionStatus, throwable);
145                                    }
146                            }
147                    }
148                    else {
149                            commit(
150                                    platformTransactionManager, transactionAttribute,
151                                    transactionStatus);
152                    }
153    
154                    throw throwable;
155            }
156    
157            @Override
158            public TransactionStatus start(
159                    PlatformTransactionManager platformTransactionManager,
160                    TransactionAttribute transactionAttribute) {
161    
162                    TransactionStatus transactionStatus =
163                            platformTransactionManager.getTransaction(transactionAttribute);
164    
165                    if (transactionStatus.isNewTransaction()) {
166                            fireTransactionCreatedEvent(
167                                    transactionAttribute, transactionStatus);
168                    }
169    
170                    return transactionStatus;
171            }
172    
173            private static final Log _log = LogFactoryUtil.getLog(
174                    DefaultTransactionExecutor.class);
175    
176    }