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