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 javax.transaction.HeuristicMixedException;
020import javax.transaction.HeuristicRollbackException;
021import javax.transaction.NotSupportedException;
022import javax.transaction.RollbackException;
023import javax.transaction.SystemException;
024import javax.transaction.TransactionManager;
025import javax.transaction.UserTransaction;
026
027import org.springframework.util.Assert;
028
029/**
030 * Adapter for a JTA UserTransaction handle, taking a JTA
031 * {@link javax.transaction.TransactionManager} reference and creating
032 * a JTA {@link javax.transaction.UserTransaction} handle for it.
033 *
034 * <p>The JTA UserTransaction interface is an exact subset of the JTA
035 * TransactionManager interface. Unfortunately, it does not serve as
036 * super-interface of TransactionManager, though, which requires an
037 * adapter such as this class to be used when intending to talk to
038 * a TransactionManager handle through the UserTransaction interface.
039 *
040 * <p>Used internally by Spring's {@link JtaTransactionManager} for certain
041 * scenarios. Not intended for direct use in application code.
042 *
043 * @author Juergen Hoeller
044 * @since 1.1.5
045 */
046public class UserTransactionAdapter implements UserTransaction {
047
048        private final TransactionManager transactionManager;
049
050
051        /**
052         * Create a new UserTransactionAdapter for the given TransactionManager.
053         * @param transactionManager the JTA TransactionManager to wrap
054         */
055        public UserTransactionAdapter(TransactionManager transactionManager) {
056                Assert.notNull(transactionManager, "TransactionManager must not be null");
057                this.transactionManager = transactionManager;
058        }
059
060        /**
061         * Return the JTA TransactionManager that this adapter delegates to.
062         */
063        public final TransactionManager getTransactionManager() {
064                return this.transactionManager;
065        }
066
067
068        @Override
069        public void setTransactionTimeout(int timeout) throws SystemException {
070                this.transactionManager.setTransactionTimeout(timeout);
071        }
072
073        @Override
074        public void begin() throws NotSupportedException, SystemException {
075                this.transactionManager.begin();
076        }
077
078        @Override
079        public void commit()
080                        throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
081                        SecurityException, SystemException {
082                this.transactionManager.commit();
083        }
084
085        @Override
086        public void rollback() throws SecurityException, SystemException {
087                this.transactionManager.rollback();
088        }
089
090        @Override
091        public void setRollbackOnly() throws SystemException {
092                this.transactionManager.setRollbackOnly();
093        }
094
095        @Override
096        public int getStatus() throws SystemException {
097                return this.transactionManager.getStatus();
098        }
099
100}