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 org.aopalliance.intercept.MethodInvocation;
018    
019    import org.springframework.transaction.TransactionStatus;
020    import org.springframework.transaction.support.TransactionCallback;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class CounterCallbackPreferringTransactionExecutor
026            extends CallbackPreferringTransactionExecutor {
027    
028            @Override
029            protected TransactionCallback<Object> createTransactionCallback(
030                    TransactionAttributeAdapter transactionAttributeAdapter,
031                    MethodInvocation methodInvocation) {
032    
033                    return new CounterCallbackPreferringTransactionCallback(
034                            transactionAttributeAdapter, methodInvocation);
035            }
036    
037            private static class CounterCallbackPreferringTransactionCallback
038                    implements TransactionCallback<Object> {
039    
040                    @Override
041                    public Object doInTransaction(TransactionStatus transactionStatus) {
042                            try {
043                                    return _methodInvocation.proceed();
044                            }
045                            catch (Throwable throwable) {
046                                    if (_transactionAttributeAdapter.rollbackOn(throwable)) {
047                                            if (throwable instanceof RuntimeException) {
048                                                    throw (RuntimeException)throwable;
049                                            }
050                                            else {
051                                                    throw new ThrowableHolderException(throwable);
052                                            }
053                                    }
054                                    else {
055                                            return new ThrowableHolder(throwable);
056                                    }
057                            }
058                    }
059    
060                    private CounterCallbackPreferringTransactionCallback(
061                            TransactionAttributeAdapter transactionAttributeAdapter,
062                            MethodInvocation methodInvocation) {
063    
064                            _transactionAttributeAdapter = transactionAttributeAdapter;
065                            _methodInvocation = methodInvocation;
066                    }
067    
068                    private final MethodInvocation _methodInvocation;
069                    private final TransactionAttributeAdapter _transactionAttributeAdapter;
070    
071            }
072    
073    }