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.NotSupportedException;
020import javax.transaction.SystemException;
021import javax.transaction.Transaction;
022import javax.transaction.TransactionManager;
023
024import org.springframework.util.Assert;
025
026/**
027 * Default implementation of the {@link TransactionFactory} strategy interface,
028 * simply wrapping a standard JTA {@link javax.transaction.TransactionManager}.
029 *
030 * <p>Does not support transaction names; simply ignores any specified name.
031 *
032 * @author Juergen Hoeller
033 * @since 2.5
034 * @see javax.transaction.TransactionManager#setTransactionTimeout(int)
035 * @see javax.transaction.TransactionManager#begin()
036 * @see javax.transaction.TransactionManager#getTransaction()
037 */
038public class SimpleTransactionFactory implements TransactionFactory {
039
040        private final TransactionManager transactionManager;
041
042
043        /**
044         * Create a new SimpleTransactionFactory for the given TransactionManager
045         * @param transactionManager the JTA TransactionManager to wrap
046         */
047        public SimpleTransactionFactory(TransactionManager transactionManager) {
048                Assert.notNull(transactionManager, "TransactionManager must not be null");
049                this.transactionManager = transactionManager;
050        }
051
052
053        @Override
054        public Transaction createTransaction(String name, int timeout) throws NotSupportedException, SystemException {
055                if (timeout >= 0) {
056                        this.transactionManager.setTransactionTimeout(timeout);
057                }
058                this.transactionManager.begin();
059                return new ManagedTransactionAdapter(this.transactionManager);
060        }
061
062        @Override
063        public boolean supportsResourceAdapterManagedTransactions() {
064                return false;
065        }
066
067}