001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.transaction.jta;
018
019import java.util.List;
020
021import javax.transaction.Status;
022import javax.transaction.Synchronization;
023
024import org.springframework.transaction.support.TransactionSynchronization;
025import org.springframework.transaction.support.TransactionSynchronizationUtils;
026
027/**
028 * Adapter for a JTA Synchronization, invoking the {@code afterCommit} /
029 * {@code afterCompletion} callbacks of Spring {@link TransactionSynchronization}
030 * objects callbacks after the outer JTA transaction has completed.
031 * Applied when participating in an existing (non-Spring) JTA transaction.
032 *
033 * @author Juergen Hoeller
034 * @since 2.0
035 * @see TransactionSynchronization#afterCommit
036 * @see TransactionSynchronization#afterCompletion
037 */
038public class JtaAfterCompletionSynchronization implements Synchronization {
039
040        private final List<TransactionSynchronization> synchronizations;
041
042
043        /**
044         * Create a new JtaAfterCompletionSynchronization for the given synchronization objects.
045         * @param synchronizations the List of TransactionSynchronization objects
046         * @see org.springframework.transaction.support.TransactionSynchronization
047         */
048        public JtaAfterCompletionSynchronization(List<TransactionSynchronization> synchronizations) {
049                this.synchronizations = synchronizations;
050        }
051
052
053        @Override
054        public void beforeCompletion() {
055        }
056
057        @Override
058        public void afterCompletion(int status) {
059                switch (status) {
060                        case Status.STATUS_COMMITTED:
061                                try {
062                                        TransactionSynchronizationUtils.invokeAfterCommit(this.synchronizations);
063                                }
064                                finally {
065                                        TransactionSynchronizationUtils.invokeAfterCompletion(
066                                                        this.synchronizations, TransactionSynchronization.STATUS_COMMITTED);
067                                }
068                                break;
069                        case Status.STATUS_ROLLEDBACK:
070                                TransactionSynchronizationUtils.invokeAfterCompletion(
071                                                this.synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
072                                break;
073                        default:
074                                TransactionSynchronizationUtils.invokeAfterCompletion(
075                                                this.synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
076                }
077        }
078}