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