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 Shuyang Zhou
029     */
030    public class CounterTransactionExecutor extends BaseTransactionExecutor {
031    
032            @Override
033            public Object execute(
034                            PlatformTransactionManager platformTransactionManager,
035                            TransactionAttribute transactionAttribute,
036                            MethodInvocation methodInvocation)
037                    throws Throwable {
038    
039                    TransactionStatus transactionStatus =
040                            platformTransactionManager.getTransaction(transactionAttribute);
041    
042                    Object returnValue = null;
043    
044                    try {
045                            returnValue = methodInvocation.proceed();
046                    }
047                    catch (Throwable throwable) {
048                            processThrowable(
049                                    platformTransactionManager, throwable, transactionAttribute,
050                                    transactionStatus);
051                    }
052    
053                    processCommit(platformTransactionManager, transactionStatus);
054    
055                    return returnValue;
056            }
057    
058            protected void processCommit(
059                    PlatformTransactionManager platformTransactionManager,
060                    TransactionStatus transactionStatus) {
061    
062                    try {
063                            platformTransactionManager.commit(transactionStatus);
064                    }
065                    catch (TransactionSystemException tse) {
066                            _log.error(
067                                    "Application exception overridden by commit exception", tse);
068    
069                            throw tse;
070                    }
071                    catch (RuntimeException re) {
072                            _log.error(
073                                    "Application exception overridden by commit exception", re);
074    
075                            throw re;
076                    }
077                    catch (Error e) {
078                            _log.error("Application exception overridden by commit error", e);
079    
080                            throw e;
081                    }
082            }
083    
084            protected void processThrowable(
085                            PlatformTransactionManager platformTransactionManager,
086                            Throwable throwable, TransactionAttribute transactionAttribute,
087                            TransactionStatus transactionStatus)
088                    throws Throwable {
089    
090                    if (transactionAttribute.rollbackOn(throwable)) {
091                            try {
092                                    platformTransactionManager.rollback(transactionStatus);
093                            }
094                            catch (TransactionSystemException tse) {
095                                    _log.error(
096                                            "Application exception overridden by rollback exception",
097                                            tse);
098    
099                                    throw tse;
100                            }
101                            catch (RuntimeException re) {
102                                    _log.error(
103                                            "Application exception overridden by rollback exception",
104                                            re);
105    
106                                    throw re;
107                            }
108                            catch (Error e) {
109                                    _log.error(
110                                            "Application exception overridden by rollback error", e);
111    
112                                    throw e;
113                            }
114                    }
115                    else {
116                            processCommit(platformTransactionManager, transactionStatus);
117                    }
118    
119                    throw throwable;
120            }
121    
122            private static final Log _log = LogFactoryUtil.getLog(
123                    CounterTransactionExecutor.class);
124    
125    }