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
018package org.apache.activemq;
019
020import javax.jms.JMSException;
021import javax.jms.QueueSession;
022import javax.jms.Session;
023import javax.jms.TopicSession;
024import javax.jms.TransactionInProgressException;
025import javax.jms.XAQueueSession;
026import javax.jms.XATopicSession;
027import javax.transaction.xa.XAResource;
028
029import 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 */
065public 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        checkClosed();
073        return getTransactionContext().isInXATransaction();
074    }
075
076    public void rollback() throws JMSException {
077        checkClosed();
078        throw new TransactionInProgressException("Cannot rollback() inside an XASession");
079    }
080
081    public void commit() throws JMSException {
082        checkClosed();
083        throw new TransactionInProgressException("Cannot commit() inside an XASession");
084    }
085
086    public Session getSession() throws JMSException {
087        return this;
088    }
089
090    public XAResource getXAResource() {
091        return getTransactionContext();
092    }
093
094    public QueueSession getQueueSession() throws JMSException {
095        return new ActiveMQQueueSession(this);
096    }
097
098    public TopicSession getTopicSession() throws JMSException {
099        return new ActiveMQTopicSession(this);
100    }
101
102    /*
103     * when there is no XA transaction it is auto ack
104     */
105    public boolean isAutoAcknowledge() {
106      return true;
107    }
108    
109    protected void doStartTransaction() throws JMSException {
110        // allow non transactional auto ack work on an XASession
111        // Seems ok by the spec that an XAConnection can be used without an XA tx
112    }
113
114}