Source code: org/activemq/ActiveMQXASession.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq;
19
20 import javax.jms.JMSException;
21 import javax.jms.QueueSession;
22 import javax.jms.Session;
23 import javax.jms.TopicSession;
24 import javax.jms.TransactionInProgressException;
25 import javax.jms.XAQueueSession;
26 import javax.jms.XATopicSession;
27 import javax.transaction.xa.XAResource;
28
29 /**
30 * The XASession interface extends the capability of Session by adding access
31 * to a JMS provider's support for the Java Transaction API (JTA) (optional).
32 * This support takes the form of a javax.transaction.xa.XAResource object.
33 * The functionality of this object closely resembles that defined by the
34 * standard X/Open XA Resource interface.
35 * <p/>
36 * An application server controls the transactional assignment of an XASession
37 * by obtaining its XAResource. It uses the XAResource to assign the session
38 * to a transaction, prepare and commit work on the transaction, and so on.
39 * <p/>
40 * An XAResource provides some fairly sophisticated facilities for
41 * interleaving work on multiple transactions, recovering a list of
42 * transactions in progress, and so on. A JTA aware JMS provider must fully
43 * implement this functionality. This could be done by using the services of a
44 * database that supports XA, or a JMS provider may choose to implement this
45 * functionality from scratch.
46 * <p/>
47 * A client of the application server is given what it thinks is a regular
48 * JMS Session. Behind the scenes, the application server controls the
49 * transaction management of the underlying XASession.
50 * <p/>
51 * The XASession interface is optional. JMS providers are not required to
52 * support this interface. This interface is for use by JMS providers to
53 * support transactional environments. Client programs are strongly encouraged
54 * to use the transactional support available in their environment, rather
55 * than use these XA interfaces directly.
56 *
57 * @version $Revision: 1.1.1.1 $
58 * @see javax.jms.Session
59 * @see javax.jms.QueueSession
60 * @see javax.jms.TopicSession
61 * @see javax.jms.XASession
62 */
63 public class ActiveMQXASession extends ActiveMQSession implements QueueSession, TopicSession, XAQueueSession, XATopicSession {
64
65 public ActiveMQXASession(ActiveMQXAConnection theConnection, int theAcknowlegeMode) throws JMSException {
66 super(theConnection, theAcknowlegeMode);
67 }
68
69 public boolean getTransacted() throws JMSException {
70 return true;
71 }
72
73 public void rollback() throws JMSException {
74 throw new TransactionInProgressException("Cannot rollback() inside an XASession");
75 }
76
77 public void commit() throws JMSException {
78 throw new TransactionInProgressException("Cannot commit() inside an XASession");
79 }
80
81 public Session getSession() throws JMSException {
82 return this;
83 }
84
85 public XAResource getXAResource() {
86 return getTransactionContext();
87 }
88
89 public QueueSession getQueueSession() throws JMSException {
90 return new ActiveMQQueueSession(this);
91 }
92
93 public TopicSession getTopicSession() throws JMSException {
94 return new ActiveMQTopicSession(this);
95 }
96
97 /**
98 * This is called before transacted work is done by
99 * the session. XA Work can only be done when this
100 * XA resource is associated with an Xid.
101 *
102 * @throws JMSException not associated with an Xid
103 */
104 protected void doStartTransaction() throws JMSException {
105 if (!getTransactionContext().isInXATransaction()) {
106 throw new JMSException("Session's XAResource has not been enlisted in a distributed transaction.");
107 }
108 }
109
110 }