001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.transaction;
019    
020    import java.io.Serializable;
021    import javax.transaction.HeuristicMixedException;
022    import javax.transaction.HeuristicRollbackException;
023    import javax.transaction.NotSupportedException;
024    import javax.transaction.RollbackException;
025    import javax.transaction.SystemException;
026    import javax.transaction.TransactionManager;
027    import javax.transaction.UserTransaction;
028    
029    public final class GeronimoUserTransaction implements UserTransaction, Serializable {
030        private static final long serialVersionUID = -7524804683512228998L;
031        private transient TransactionManager transactionManager;
032    
033        public GeronimoUserTransaction(TransactionManager transactionManager) {
034            this.transactionManager = transactionManager;
035        }
036    
037        boolean isActive() {
038            return transactionManager != null;
039        }
040    
041        public void setTransactionManager(TransactionManager transactionManager) {
042            if (this.transactionManager == null) {
043                this.transactionManager = transactionManager;
044            } else if (this.transactionManager != transactionManager) {
045                throw new IllegalStateException("This user transaction is already associated with another transaction manager");
046            }
047        }
048    
049    
050        public int getStatus() throws SystemException {
051            return transactionManager.getStatus();
052        }
053    
054        public void setRollbackOnly() throws IllegalStateException, SystemException {
055            transactionManager.setRollbackOnly();
056        }
057    
058        public void setTransactionTimeout(int seconds) throws SystemException {
059            if (seconds < 0) {
060                throw new SystemException("transaction timeout must be positive or 0, not " + seconds);
061            }
062            transactionManager.setTransactionTimeout(seconds);
063        }
064    
065        public void begin() throws NotSupportedException, SystemException {
066            transactionManager.begin();
067        }
068    
069        public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
070            transactionManager.commit();
071        }
072    
073        public void rollback() throws IllegalStateException, SecurityException, SystemException {
074            transactionManager.rollback();
075        }
076    }