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 (throwable != null) {
070                                    fireTransactionRollbackedEvent(
071                                            transactionAttribute, transactionStatus, throwable);
072                            }
073                            else {
074                                    fireTransactionCommittedEvent(
075                                            transactionAttribute, transactionStatus);
076                            }
077                    }
078            }
079    
080            @Override
081            public Object execute(
082                            PlatformTransactionManager platformTransactionManager,
083                            TransactionAttribute transactionAttribute,
084                            MethodInvocation methodInvocation)
085                    throws Throwable {
086    
087                    TransactionStatus transactionStatus = start(
088                            platformTransactionManager, transactionAttribute);
089    
090                    Object returnValue = null;
091    
092                    try {
093                            returnValue = methodInvocation.proceed();
094                    }
095                    catch (Throwable throwable) {
096                            rollback(
097                                    platformTransactionManager, throwable, transactionAttribute,
098                                    transactionStatus);
099                    }
100    
101                    commit(
102                            platformTransactionManager, transactionAttribute,
103                            transactionStatus);
104    
105                    return returnValue;
106            }
107    
108            @Override
109            public void rollback(
110                            PlatformTransactionManager platformTransactionManager,
111                            Throwable throwable, TransactionAttribute transactionAttribute,
112                            TransactionStatus transactionStatus)
113                    throws Throwable {
114    
115                    if (transactionAttribute.rollbackOn(throwable)) {
116                            try {
117                                    platformTransactionManager.rollback(transactionStatus);
118                            }
119                            catch (TransactionSystemException tse) {
120                                    _log.error(
121                                            "Application exception overridden by rollback exception",
122                                            tse);
123    
124                                    throw tse;
125                            }
126                            catch (RuntimeException re) {
127                                    _log.error(
128                                            "Application exception overridden by rollback exception",
129                                            re);
130    
131                                    throw re;
132                            }
133                            catch (Error e) {
134                                    _log.error(
135                                            "Application exception overridden by rollback error", e);
136    
137                                    throw e;
138                            }
139                            finally {
140                                    fireTransactionRollbackedEvent(
141                                            transactionAttribute, transactionStatus, throwable);
142                            }
143                    }
144                    else {
145                            commit(
146                                    platformTransactionManager, transactionAttribute,
147                                    transactionStatus);
148                    }
149    
150                    throw throwable;
151            }
152    
153            @Override
154            public TransactionStatus start(
155                    PlatformTransactionManager platformTransactionManager,
156                    TransactionAttribute transactionAttribute) {
157    
158                    TransactionStatus transactionStatus =
159                            platformTransactionManager.getTransaction(transactionAttribute);
160    
161                    fireTransactionCreatedEvent(transactionAttribute, transactionStatus);
162    
163                    return transactionStatus;
164            }
165    
166            private static final Log _log = LogFactoryUtil.getLog(
167                    DefaultTransactionExecutor.class);
168    
169    }