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