001    /** 
002     * 
003     * Copyright 2004 Hiram Chirino
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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.activemq.service;
019    
020    import org.activemq.broker.BrokerClient;
021    import org.activemq.message.ActiveMQXid;
022    import org.activemq.service.impl.AutoCommitTransaction;
023    
024    import javax.jms.JMSException;
025    import javax.transaction.xa.XAException;
026    
027    /**
028     * A Transaction keeps track of all the tasks
029     * that must be run before and after transactional events.
030     *
031     * @version $Revision: 1.1.1.1 $
032     */
033    public abstract class TransactionManager implements Service {
034    
035        // Keeps track of the current transaction
036        static private final ThreadLocal contextTx = new ThreadLocal();
037      
038        static public void setContexTransaction(Transaction tx) {
039            contextTx.set(tx);
040        }
041    
042        static public Transaction getContexTransaction() {
043            Transaction tx = (Transaction) contextTx.get();
044            if( tx == null ) {
045                return AutoCommitTransaction.AUTO_COMMIT_TRANSACTION;
046            }
047            return tx;
048        }
049       
050        /**
051         * @return true if there is a current transaction
052         */
053        static public boolean isCurrentTransaction(){
054            return contextTx.get() != null;
055        }
056        
057    
058        abstract public Transaction createLocalTransaction(BrokerClient client, String txid) throws JMSException;
059    
060        abstract public Transaction getLocalTransaction(String txid) throws JMSException;
061    
062        abstract public Transaction createXATransaction(BrokerClient client, ActiveMQXid xid) throws XAException;
063    
064        abstract public Transaction getXATransaction(ActiveMQXid xid) throws XAException;
065    
066        abstract public ActiveMQXid[] getPreparedXATransactions() throws XAException;
067    
068        /**
069         * A hint to the TransactionManager that an BrokerClient has stopped
070         * This enables the TransactionManager to rollback in progess transactions
071         * that the client created.
072         *
073         * @param client
074         */
075        abstract public void cleanUpClient(BrokerClient client) throws JMSException;
076    
077       /**
078        * Called on restart when recovering prepared transactions to reload
079        * a transaction from persistent store
080        */
081        abstract  public void recover(Transaction transaction);
082    
083    }