001/*
002 * Copyright 2002-2016 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.Status;
020import javax.transaction.SystemException;
021import javax.transaction.UserTransaction;
022
023import org.springframework.transaction.TransactionSystemException;
024import org.springframework.transaction.support.SmartTransactionObject;
025import org.springframework.transaction.support.TransactionSynchronizationUtils;
026
027/**
028 * JTA transaction object, representing a {@link javax.transaction.UserTransaction}.
029 * Used as transaction object by Spring's {@link JtaTransactionManager}.
030 *
031 * <p>Note: This is an SPI class, not intended to be used by applications.
032 *
033 * @author Juergen Hoeller
034 * @since 1.1
035 * @see JtaTransactionManager
036 * @see javax.transaction.UserTransaction
037 */
038public class JtaTransactionObject implements SmartTransactionObject {
039
040        private final UserTransaction userTransaction;
041
042        boolean resetTransactionTimeout = false;
043
044
045        /**
046         * Create a new JtaTransactionObject for the given JTA UserTransaction.
047         * @param userTransaction the JTA UserTransaction for the current transaction
048         * (either a shared object or retrieved through a fresh per-transaction lookuip)
049         */
050        public JtaTransactionObject(UserTransaction userTransaction) {
051                this.userTransaction = userTransaction;
052        }
053
054        /**
055         * Return the JTA UserTransaction object for the current transaction.
056         */
057        public final UserTransaction getUserTransaction() {
058                return this.userTransaction;
059        }
060
061
062        /**
063         * This implementation checks the UserTransaction's rollback-only flag.
064         */
065        @Override
066        public boolean isRollbackOnly() {
067                if (this.userTransaction == null) {
068                        return false;
069                }
070                try {
071                        int jtaStatus = this.userTransaction.getStatus();
072                        return (jtaStatus == Status.STATUS_MARKED_ROLLBACK || jtaStatus == Status.STATUS_ROLLEDBACK);
073                }
074                catch (SystemException ex) {
075                        throw new TransactionSystemException("JTA failure on getStatus", ex);
076                }
077        }
078
079        /**
080         * This implementation triggers flush callbacks,
081         * assuming that they will flush all affected ORM sessions.
082         * @see org.springframework.transaction.support.TransactionSynchronization#flush()
083         */
084        @Override
085        public void flush() {
086                TransactionSynchronizationUtils.triggerFlush();
087        }
088
089}