1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util.xa; |
8 | |
|
9 | |
import javax.transaction.Status; |
10 | |
import javax.transaction.xa.XAException; |
11 | |
import javax.transaction.xa.XAResource; |
12 | |
import javax.transaction.xa.Xid; |
13 | |
|
14 | |
import org.apache.commons.logging.Log; |
15 | |
import org.apache.commons.logging.LogFactory; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class DefaultXASession implements XAResource |
21 | |
{ |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
27 | |
|
28 | |
protected AbstractTransactionContext localContext; |
29 | |
protected Xid localXid; |
30 | |
protected AbstractXAResourceManager resourceManager; |
31 | |
|
32 | |
public DefaultXASession(AbstractXAResourceManager resourceManager) |
33 | 0 | { |
34 | 0 | this.localContext = null; |
35 | 0 | this.localXid = null; |
36 | 0 | this.resourceManager = resourceManager; |
37 | 0 | } |
38 | |
|
39 | |
public XAResource getXAResource() |
40 | |
{ |
41 | 0 | return this; |
42 | |
} |
43 | |
|
44 | |
public Object getResourceManager() |
45 | |
{ |
46 | 0 | return resourceManager; |
47 | |
} |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public void begin() throws ResourceManagerException |
53 | |
{ |
54 | 0 | if (localXid != null) |
55 | |
{ |
56 | 0 | throw new IllegalStateException( |
57 | |
"Cannot start local transaction. An XA transaction is already in progress."); |
58 | |
} |
59 | 0 | if (localContext != null) |
60 | |
{ |
61 | 0 | throw new IllegalStateException( |
62 | |
"Cannot start local transaction. A local transaction already in progress."); |
63 | |
} |
64 | 0 | localContext = resourceManager.createTransactionContext(this); |
65 | 0 | resourceManager.beginTransaction(localContext); |
66 | 0 | } |
67 | |
|
68 | |
public void commit() throws ResourceManagerException |
69 | |
{ |
70 | 0 | if (localXid != null) |
71 | |
{ |
72 | 0 | throw new IllegalStateException( |
73 | |
"Cannot commit local transaction as an XA transaction is in progress."); |
74 | |
} |
75 | 0 | if (localContext == null) |
76 | |
{ |
77 | 0 | throw new IllegalStateException("Cannot commit local transaction as no transaction was begun"); |
78 | |
} |
79 | 0 | resourceManager.commitTransaction(localContext); |
80 | 0 | localContext = null; |
81 | 0 | } |
82 | |
|
83 | |
public void rollback() throws ResourceManagerException |
84 | |
{ |
85 | 0 | if (localXid != null) |
86 | |
{ |
87 | 0 | throw new IllegalStateException( |
88 | |
"Cannot rollback local transaction as an XA transaction is in progress."); |
89 | |
} |
90 | 0 | if (localContext == null) |
91 | |
{ |
92 | 0 | throw new IllegalStateException("Cannot commit local transaction as no transaction was begun"); |
93 | |
} |
94 | 0 | resourceManager.rollbackTransaction(localContext); |
95 | 0 | localContext = null; |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public boolean isSameRM(XAResource xares) throws XAException |
103 | |
{ |
104 | 0 | return xares instanceof DefaultXASession |
105 | |
&& ((DefaultXASession) xares).getResourceManager().equals(resourceManager); |
106 | |
} |
107 | |
|
108 | |
public Xid[] recover(int flag) throws XAException |
109 | |
{ |
110 | 0 | return null; |
111 | |
} |
112 | |
|
113 | |
public void start(Xid xid, int flags) throws XAException |
114 | |
{ |
115 | 0 | if (logger.isDebugEnabled()) |
116 | |
{ |
117 | 0 | logger.debug(new StringBuffer(128).append("Thread ").append(Thread.currentThread()).append( |
118 | |
flags == TMNOFLAGS ? " starts" : flags == TMJOIN ? " joins" : " resumes").append( |
119 | |
" work on behalf of transaction branch ").append(xid).toString()); |
120 | |
} |
121 | |
|
122 | 0 | if (this.localContext != null) |
123 | |
{ |
124 | 0 | throw new XAException(XAException.XAER_PROTO); |
125 | |
} |
126 | |
|
127 | 0 | if (this.localXid != null) |
128 | |
{ |
129 | 0 | throw new XAException(XAException.XAER_PROTO); |
130 | |
} |
131 | 0 | switch (flags) |
132 | |
{ |
133 | |
|
134 | |
case TMNOFLAGS : |
135 | |
case TMJOIN : |
136 | |
default : |
137 | |
try |
138 | |
{ |
139 | 0 | localContext = resourceManager.createTransactionContext(this); |
140 | 0 | resourceManager.beginTransaction(localContext); |
141 | |
} |
142 | 0 | catch (Exception e) |
143 | |
{ |
144 | |
|
145 | 0 | logger.error("Could not create new transactional resource", e); |
146 | 0 | throw (XAException) new XAException(e.getMessage()).initCause(e); |
147 | 0 | } |
148 | |
break; |
149 | |
case TMRESUME : |
150 | 0 | localContext = resourceManager.getSuspendedTransactionalResource(xid); |
151 | 0 | if (localContext == null) |
152 | |
{ |
153 | 0 | throw new XAException(XAException.XAER_NOTA); |
154 | |
} |
155 | |
|
156 | 0 | resourceManager.removeSuspendedTransactionalResource(xid); |
157 | |
break; |
158 | |
} |
159 | 0 | localXid = xid; |
160 | 0 | resourceManager.addActiveTransactionalResource(localXid, localContext); |
161 | 0 | } |
162 | |
|
163 | |
public void end(Xid xid, int flags) throws XAException |
164 | |
{ |
165 | 0 | if (logger.isDebugEnabled()) |
166 | |
{ |
167 | 0 | logger.debug(new StringBuffer(128).append("Thread ").append(Thread.currentThread()).append( |
168 | |
flags == TMSUSPEND ? " suspends" : flags == TMFAIL ? " fails" : " ends").append( |
169 | |
" work on behalf of transaction branch ").append(xid).toString()); |
170 | |
} |
171 | |
|
172 | 0 | if (localContext == null) |
173 | |
{ |
174 | 0 | throw new XAException(XAException.XAER_NOTA); |
175 | |
} |
176 | |
|
177 | 0 | if (localXid == null || !localXid.equals(xid)) |
178 | |
{ |
179 | 0 | throw new XAException(XAException.XAER_PROTO); |
180 | |
} |
181 | |
|
182 | |
try |
183 | |
{ |
184 | 0 | switch (flags) |
185 | |
{ |
186 | |
case TMSUSPEND : |
187 | |
|
188 | 0 | resourceManager.addSuspendedTransactionalResource(localXid, localContext); |
189 | 0 | resourceManager.removeActiveTransactionalResource(localXid); |
190 | 0 | break; |
191 | |
case TMFAIL : |
192 | 0 | resourceManager.setTransactionRollbackOnly(localContext); |
193 | 0 | break; |
194 | |
case TMSUCCESS : |
195 | |
default : |
196 | |
break; |
197 | |
} |
198 | |
} |
199 | 0 | catch (ResourceManagerException e) |
200 | |
{ |
201 | 0 | throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e); |
202 | 0 | } |
203 | 0 | localXid = null; |
204 | 0 | localContext = null; |
205 | 0 | } |
206 | |
|
207 | |
public void commit(Xid xid, boolean onePhase) throws XAException |
208 | |
{ |
209 | 0 | if (xid == null) |
210 | |
{ |
211 | 0 | throw new XAException(XAException.XAER_PROTO); |
212 | |
} |
213 | 0 | AbstractTransactionContext context = resourceManager.getActiveTransactionalResource(xid); |
214 | 0 | if (context == null) |
215 | |
{ |
216 | 0 | throw new XAException(XAException.XAER_NOTA); |
217 | |
} |
218 | 0 | if (logger.isDebugEnabled()) |
219 | |
{ |
220 | 0 | logger.debug("Committing transaction branch " + xid); |
221 | |
} |
222 | 0 | if (context.status == Status.STATUS_MARKED_ROLLBACK) |
223 | |
{ |
224 | 0 | throw new XAException(XAException.XA_RBROLLBACK); |
225 | |
} |
226 | |
|
227 | |
try |
228 | |
{ |
229 | 0 | if (context.status != Status.STATUS_PREPARED) |
230 | |
{ |
231 | 0 | if (onePhase) |
232 | |
{ |
233 | 0 | resourceManager.prepareTransaction(context); |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | throw new XAException(XAException.XAER_PROTO); |
238 | |
} |
239 | |
} |
240 | 0 | resourceManager.commitTransaction(context); |
241 | |
} |
242 | 0 | catch (ResourceManagerException e) |
243 | |
{ |
244 | 0 | throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e); |
245 | 0 | } |
246 | 0 | resourceManager.removeActiveTransactionalResource(xid); |
247 | 0 | resourceManager.removeSuspendedTransactionalResource(xid); |
248 | 0 | } |
249 | |
|
250 | |
public void rollback(Xid xid) throws XAException |
251 | |
{ |
252 | 0 | if (xid == null) |
253 | |
{ |
254 | 0 | throw new XAException(XAException.XAER_PROTO); |
255 | |
} |
256 | 0 | AbstractTransactionContext context = resourceManager.getActiveTransactionalResource(xid); |
257 | 0 | if (context == null) |
258 | |
{ |
259 | 0 | throw new XAException(XAException.XAER_NOTA); |
260 | |
} |
261 | 0 | if (logger.isDebugEnabled()) |
262 | |
{ |
263 | 0 | logger.debug("Rolling back transaction branch " + xid); |
264 | |
} |
265 | |
try |
266 | |
{ |
267 | 0 | resourceManager.rollbackTransaction(context); |
268 | |
} |
269 | 0 | catch (ResourceManagerException e) |
270 | |
{ |
271 | 0 | throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e); |
272 | 0 | } |
273 | 0 | resourceManager.removeActiveTransactionalResource(xid); |
274 | 0 | resourceManager.removeSuspendedTransactionalResource(xid); |
275 | 0 | } |
276 | |
|
277 | |
public int prepare(Xid xid) throws XAException |
278 | |
{ |
279 | 0 | if (xid == null) |
280 | |
{ |
281 | 0 | throw new XAException(XAException.XAER_PROTO); |
282 | |
} |
283 | |
|
284 | 0 | AbstractTransactionContext context = resourceManager.getTransactionalResource(xid); |
285 | 0 | if (context == null) |
286 | |
{ |
287 | 0 | throw new XAException(XAException.XAER_NOTA); |
288 | |
} |
289 | |
|
290 | 0 | if (logger.isDebugEnabled()) |
291 | |
{ |
292 | 0 | logger.debug("Preparing transaction branch " + xid); |
293 | |
} |
294 | |
|
295 | 0 | if (context.status == Status.STATUS_MARKED_ROLLBACK) |
296 | |
{ |
297 | 0 | throw new XAException(XAException.XA_RBROLLBACK); |
298 | |
} |
299 | |
|
300 | |
try |
301 | |
{ |
302 | 0 | return resourceManager.prepareTransaction(context); |
303 | |
} |
304 | 0 | catch (ResourceManagerException e) |
305 | |
{ |
306 | 0 | throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e); |
307 | |
} |
308 | |
} |
309 | |
|
310 | |
public void forget(Xid xid) throws XAException |
311 | |
{ |
312 | 0 | if (logger.isDebugEnabled()) |
313 | |
{ |
314 | 0 | logger.debug("Forgetting transaction branch " + xid); |
315 | |
} |
316 | 0 | AbstractTransactionContext context = resourceManager.getTransactionalResource(xid); |
317 | 0 | if (context == null) |
318 | |
{ |
319 | 0 | throw new XAException(XAException.XAER_NOTA); |
320 | |
} |
321 | 0 | resourceManager.removeActiveTransactionalResource(xid); |
322 | 0 | resourceManager.removeSuspendedTransactionalResource(xid); |
323 | 0 | } |
324 | |
|
325 | |
public int getTransactionTimeout() throws XAException |
326 | |
{ |
327 | 0 | return (int)(resourceManager.getDefaultTransactionTimeout() / 1000); |
328 | |
} |
329 | |
|
330 | |
public boolean setTransactionTimeout(int timeout) throws XAException |
331 | |
{ |
332 | 0 | resourceManager.setDefaultTransactionTimeout(timeout * 1000); |
333 | 0 | return false; |
334 | |
} |
335 | |
|
336 | |
} |