1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.MuleManager;
14 import org.mule.config.i18n.CoreMessages;
15 import org.mule.umo.TransactionException;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import javax.transaction.HeuristicRollbackException;
21 import javax.transaction.RollbackException;
22 import javax.transaction.SystemException;
23 import javax.transaction.Transaction;
24 import javax.transaction.TransactionManager;
25
26
27
28
29 public class XaTransaction extends AbstractTransaction
30 {
31
32
33
34 private Transaction transaction = null;
35
36
37
38
39 private Map resources = null;
40
41
42
43
44 public XaTransaction()
45 {
46 super();
47 }
48
49 protected void doBegin() throws TransactionException
50 {
51 TransactionManager txManager = MuleManager.getInstance().getTransactionManager();
52
53 if (txManager == null)
54 {
55 throw new IllegalStateException(
56 CoreMessages.objectNotRegisteredWithManager("Transaction Manager").getMessage());
57 }
58
59 try
60 {
61 txManager.begin();
62 synchronized (this)
63 {
64 transaction = txManager.getTransaction();
65 }
66 }
67 catch (Exception e)
68 {
69 throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
70 }
71 }
72
73 protected void doCommit() throws TransactionException
74 {
75 try
76 {
77 synchronized (this)
78 {
79 transaction.commit();
80 }
81 }
82 catch (RollbackException e)
83 {
84 throw new TransactionRollbackException(CoreMessages.transactionMarkedForRollback(), e);
85 }
86 catch (HeuristicRollbackException e)
87 {
88 throw new TransactionRollbackException(CoreMessages.transactionMarkedForRollback(), e);
89 }
90 catch (Exception e)
91 {
92 throw new IllegalTransactionStateException(CoreMessages.transactionCommitFailed(), e);
93 }
94 }
95
96 protected void doRollback() throws TransactionRollbackException
97 {
98 try
99 {
100 synchronized (this)
101 {
102 transaction.rollback();
103 }
104 }
105 catch (SystemException e)
106 {
107 throw new TransactionRollbackException(e);
108 }
109
110 }
111
112 public int getStatus() throws TransactionStatusException
113 {
114 synchronized (this)
115 {
116 if (transaction == null)
117 {
118 return STATUS_NO_TRANSACTION;
119 }
120
121 try
122 {
123 return transaction.getStatus();
124 }
125 catch (SystemException e)
126 {
127 throw new TransactionStatusException(e);
128 }
129 }
130 }
131
132 public void setRollbackOnly()
133 {
134 try
135 {
136 synchronized (this)
137 {
138 transaction.setRollbackOnly();
139 }
140 }
141 catch (SystemException e)
142 {
143 throw (IllegalStateException) new IllegalStateException(
144 "Failed to set transaction to rollback only: " + e.getMessage()
145 ).initCause(e);
146 }
147 }
148
149 public Object getResource(Object key)
150 {
151 synchronized (this)
152 {
153 return resources == null ? null : resources.get(key);
154 }
155 }
156
157 public boolean hasResource(Object key)
158 {
159 synchronized (this)
160 {
161 return resources != null && resources.containsKey(key);
162 }
163 }
164
165 public void bindResource(Object key, Object resource) throws TransactionException
166 {
167 synchronized (this)
168 {
169 if (resources == null)
170 {
171 resources = new HashMap();
172 }
173
174 if (resources.containsKey(key))
175 {
176 throw new IllegalTransactionStateException(
177 CoreMessages.transactionResourceAlreadyListedForKey(key));
178 }
179
180 resources.put(key, resource);
181 }
182 }
183 }