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.activemq;
019    
020    import javax.jms.JMSException;
021    import javax.jms.QueueSession;
022    import javax.jms.Session;
023    import javax.jms.TopicSession;
024    import javax.jms.TransactionInProgressException;
025    import javax.jms.XAQueueSession;
026    import javax.jms.XATopicSession;
027    import javax.transaction.xa.XAResource;
028    
029    import org.apache.activemq.command.SessionId;
030    
031    /**
032     * The XASession interface extends the capability of Session by adding access
033     * to a JMS provider's support for the  Java Transaction API (JTA) (optional).
034     * This support takes the form of a javax.transaction.xa.XAResource object.
035     * The functionality of this object closely resembles that defined by the
036     * standard X/Open XA Resource interface.
037     * <p/>
038     * An application server controls the transactional assignment of an XASession
039     * by obtaining its XAResource. It uses the XAResource to assign the session
040     * to a transaction, prepare and commit work on the transaction, and so on.
041     * <p/>
042     * An XAResource provides some fairly sophisticated facilities for
043     * interleaving work on multiple transactions, recovering a list of
044     * transactions in progress, and so on. A JTA aware JMS provider must fully
045     * implement this functionality. This could be done by using the services of a
046     * database that supports XA, or a JMS provider may choose to implement this
047     * functionality from scratch.
048     * <p/>
049     * A client of the application server is given what it thinks is a regular
050     * JMS Session. Behind the scenes, the application server controls the
051     * transaction management of the underlying XASession.
052     * <p/>
053     * The XASession interface is optional. JMS providers are not required to
054     * support this interface. This interface is for use by JMS providers to
055     * support transactional environments. Client programs are strongly encouraged
056     * to use the transactional support  available in their environment, rather
057     * than use these XA  interfaces directly.
058     *
059     * 
060     * @see javax.jms.Session
061     * @see javax.jms.QueueSession
062     * @see javax.jms.TopicSession
063     * @see javax.jms.XASession
064     */
065    public class ActiveMQXASession extends ActiveMQSession implements QueueSession, TopicSession, XAQueueSession, XATopicSession {
066    
067        public ActiveMQXASession(ActiveMQXAConnection connection, SessionId sessionId, int theAcknowlegeMode, boolean dispatchAsync) throws JMSException {
068            super(connection, sessionId, theAcknowlegeMode, dispatchAsync);
069        }
070    
071        public boolean getTransacted() throws JMSException {
072            return getTransactionContext().isInXATransaction();
073        }
074    
075        public void rollback() throws JMSException {
076            throw new TransactionInProgressException("Cannot rollback() inside an XASession");
077        }
078    
079        public void commit() throws JMSException {
080            throw new TransactionInProgressException("Cannot commit() inside an XASession");
081        }
082    
083        public Session getSession() throws JMSException {
084            return this;
085        }
086    
087        public XAResource getXAResource() {
088            return getTransactionContext();
089        }
090    
091        public QueueSession getQueueSession() throws JMSException {
092            return new ActiveMQQueueSession(this);
093        }
094    
095        public TopicSession getTopicSession() throws JMSException {
096            return new ActiveMQTopicSession(this);
097        }
098    
099        /*
100         * when there is no XA transaction it is auto ack
101         */
102        public boolean isAutoAcknowledge() {
103          return true;
104        }
105        
106        protected void doStartTransaction() throws JMSException {
107            // allow non transactional auto ack work on an XASession
108            // Seems ok by the spec that an XAConnection can be used without an XA tx
109        }
110    
111    }