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;
020import javax.transaction.Status;
021import javax.transaction.Synchronization;
022
023import org.springframework.transaction.support.TransactionSynchronization;
024import org.springframework.transaction.support.TransactionSynchronizationUtils;
025
026/**
027 * Adapter for a JTA Synchronization, invoking the {@code afterCommit} /
028 * {@code afterCompletion} callbacks of Spring {@link TransactionSynchronization}
029 * objects callbacks after the outer JTA transaction has completed.
030 * Applied when participating in an existing (non-Spring) JTA transaction.
031 *
032 * @author Juergen Hoeller
033 * @since 2.0
034 * @see TransactionSynchronization#afterCommit
035 * @see TransactionSynchronization#afterCompletion
036 */
037public class JtaAfterCompletionSynchronization implements Synchronization {
038
039        private final List<TransactionSynchronization> synchronizations;
040
041
042        /**
043         * Create a new JtaAfterCompletionSynchronization for the given synchronization objects.
044         * @param synchronizations the List of TransactionSynchronization objects
045         * @see org.springframework.transaction.support.TransactionSynchronization
046         */
047        public JtaAfterCompletionSynchronization(List<TransactionSynchronization> synchronizations) {
048                this.synchronizations = synchronizations;
049        }
050
051
052        @Override
053        public void beforeCompletion() {
054        }
055
056        @Override
057        public void afterCompletion(int status) {
058                switch (status) {
059                        case Status.STATUS_COMMITTED:
060                                try {
061                                        TransactionSynchronizationUtils.invokeAfterCommit(this.synchronizations);
062                                }
063                                finally {
064                                        TransactionSynchronizationUtils.invokeAfterCompletion(
065                                                        this.synchronizations, TransactionSynchronization.STATUS_COMMITTED);
066                                }
067                                break;
068                        case Status.STATUS_ROLLEDBACK:
069                                TransactionSynchronizationUtils.invokeAfterCompletion(
070                                                this.synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
071                                break;
072                        default:
073                                TransactionSynchronizationUtils.invokeAfterCompletion(
074                                                this.synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
075                }
076        }
077}