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 */
017package org.apache.activemq;
018
019import javax.jms.JMSException;
020import javax.jms.Session;
021import javax.jms.XAConnection;
022import javax.jms.XAQueueConnection;
023import javax.jms.XAQueueSession;
024import javax.jms.XASession;
025import javax.jms.XATopicConnection;
026import javax.jms.XATopicSession;
027
028import org.apache.activemq.management.JMSStatsImpl;
029import org.apache.activemq.transport.Transport;
030import org.apache.activemq.util.IdGenerator;
031
032/**
033 * The XAConnection interface extends the capability of Connection by providing
034 * an XASession (optional).
035 * <p/>
036 * The XAConnection interface is optional. JMS providers are not required to
037 * support this interface. This interface is for use by JMS providers to
038 * support transactional environments. Client programs are strongly encouraged
039 * to use the transactional support  available in their environment, rather
040 * than use these XA  interfaces directly.
041 *
042 * 
043 * @see javax.jms.Connection
044 * @see javax.jms.ConnectionFactory
045 * @see javax.jms.QueueConnection
046 * @see javax.jms.TopicConnection
047 * @see javax.jms.TopicConnectionFactory
048 * @see javax.jms.QueueConnection
049 * @see javax.jms.QueueConnectionFactory
050 */
051public class ActiveMQXAConnection extends ActiveMQConnection implements XATopicConnection, XAQueueConnection, XAConnection {
052
053    private int xaAckMode;
054
055    protected ActiveMQXAConnection(Transport transport, IdGenerator clientIdGenerator,
056                                   IdGenerator connectionIdGenerator, JMSStatsImpl factoryStats) throws Exception {
057        super(transport, clientIdGenerator, connectionIdGenerator, factoryStats);
058    }
059
060    public XASession createXASession() throws JMSException {
061        return (XASession) createSession(true, Session.SESSION_TRANSACTED);
062    }
063
064    public XATopicSession createXATopicSession() throws JMSException {
065        return (XATopicSession) createSession(true, Session.SESSION_TRANSACTED);
066    }
067
068    public XAQueueSession createXAQueueSession() throws JMSException {
069        return (XAQueueSession) createSession(true, Session.SESSION_TRANSACTED);
070    }
071
072    public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException {
073        checkClosedOrFailed();
074        ensureConnectionInfoSent();
075        return new ActiveMQXASession(this, getNextSessionId(), getAckMode(), isDispatchAsync());
076    }
077
078    private int getAckMode() {
079        return xaAckMode > 0 ? xaAckMode : Session.SESSION_TRANSACTED;
080    }
081
082    public void setXaAckMode(int xaAckMode) {
083        this.xaAckMode = xaAckMode;
084    }
085
086    public int getXaAckMode() {
087        return xaAckMode;
088    }
089}