1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.jms;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.transaction.TransactionException;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.transaction.AbstractSingleResourceTransaction;
17 import org.mule.transaction.IllegalTransactionStateException;
18 import org.mule.transport.jms.i18n.JmsMessages;
19
20 import javax.jms.Connection;
21 import javax.jms.JMSException;
22 import javax.jms.Session;
23
24
25
26
27
28
29 public class JmsTransaction extends AbstractSingleResourceTransaction
30 {
31
32 public JmsTransaction(MuleContext muleContext)
33 {
34 super(muleContext);
35 }
36
37 public void bindResource(Object key, Object resource) throws TransactionException
38 {
39 if (!(key instanceof Connection) || !(resource instanceof Session))
40 {
41 throw new IllegalTransactionStateException(
42 CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
43 }
44
45 Session session = (Session)resource;
46 try
47 {
48 if (!session.getTransacted())
49 {
50 throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted());
51 }
52 }
53 catch (JMSException e)
54 {
55 throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
56 }
57
58 super.bindResource(key, resource);
59 }
60
61 protected void doBegin() throws TransactionException
62 {
63
64 }
65
66 protected void doCommit() throws TransactionException
67 {
68 if (resource == null)
69 {
70 logger.warn(CoreMessages.commitTxButNoResource(this));
71 return;
72 }
73
74 try
75 {
76 ((Session)resource).commit();
77 }
78 catch (JMSException e)
79 {
80 throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
81 }
82 }
83
84 protected void doRollback() throws TransactionException
85 {
86 if (resource == null)
87 {
88 logger.warn(CoreMessages.rollbackTxButNoResource(this));
89 return;
90 }
91
92 try
93 {
94 if (logger.isDebugEnabled())
95 {
96 logger.debug("Rolling back transaction: " + getId());
97 }
98 ((Session)resource).rollback();
99 }
100 catch (JMSException e)
101 {
102 throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
103 }
104 }
105
106 }