KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > XATerminatorImpl


1 /*
2  * @(#) XATerminatorImpl.java 1.2 02/01/15
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  * This module was originally developed by
7  *
8  * - Bull S.A. as part of the JOnAS application server code released in
9  * July 1999 (www.bull.com)
10  *
11  * --------------------------------------------------------------------------
12  * The original code and portions created by Bull SA are
13  * Copyright (c) 1999 BULL SA
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are met:
18  *
19  * -Redistributions of source code must retain the above copyright notice, this
20  * list of conditions and the following disclaimer.
21  *
22  * -Redistributions in binary form must reproduce the above copyright notice,
23  * this list of conditions and the following disclaimer in the documentation
24  * and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  * --------------------------------------------------------------------------
38  *
39  * Contributor:
40  * 04/01/15 Tony Ortiz
41  *
42  * --------------------------------------------------------------------------
43  * $Id: XATerminatorImpl.java,v 1.5 2004/11/05 19:54:59 tonyortiz Exp $
44  * --------------------------------------------------------------------------
45  */

46 package org.objectweb.jotm;
47
48 import javax.transaction.xa.Xid JavaDoc;
49
50 import javax.resource.spi.XATerminator JavaDoc;
51
52 import javax.transaction.xa.XAException JavaDoc;
53
54 /**
55  * Implementation of the object that represents an inflow transaction.
56  *
57  */

58
59 public class XATerminatorImpl implements XATerminator JavaDoc {
60
61     // Must be init at null, for clients that do not get UserTransaction
62
private transient static XATerminatorImpl unique = null;
63
64     // ------------------------------------------------------------------
65
// Constructors
66
// ------------------------------------------------------------------
67

68     /**
69      * @serial
70      */

71     private TransactionImpl iftx = null;
72     /**
73      * @serial
74      */

75     private Xid xid;
76     /**
77      * @serial
78      */

79     private Log mylog;
80
81     // XAException values
82

83     private int errorCode;
84     private final static int XA_RBBASE = 100;
85     private final static int XA_RBROLLBACK = XA_RBBASE;
86     private final static int XA_RBCOMMFAIL = XA_RBBASE + 1;
87     private final static int XA_RBDEADLOCK = XA_RBBASE + 2;
88     private final static int XA_RBINTEGRITY = XA_RBBASE + 3;
89     private final static int XA_RBOTHER = XA_RBBASE + 4;
90     private final static int XA_RBPROTO = XA_RBBASE + 5;
91     private final static int XA_RBRBTIMEOUT = XA_RBBASE + 6;
92     private final static int XA_RBTRANSIENT = XA_RBBASE + 7;
93     private final static int XA_RBEND = XA_RBTRANSIENT;
94     private final static int XA_NOMIGRATE = 9;
95     private final static int XA_HEURHAZ = 8;
96     private final static int XA_HEURCOM = 7;
97     private final static int XA_HEURRB = 6;
98     private final static int XA_HEURMIX = 5;
99     private final static int XA_NOTUSED1 = 4;
100     private final static int XA_RDONLY = 3;
101     // Error Codes
102
private final static int XAER_RMERR = -3;
103     private final static int XAER_NOTA = -4;
104     private final static int XAER_INVAL = -5;
105     private final static int XAER_PROTO = -6;
106     private final static int XAER_RMFAIL = -7;
107     private final static int XAER_DUPID = -8;
108     private final static int XAER_OUTSIDE = -9;
109
110
111     // ---------------------------------------------------------------
112
// Constructors
113
// ---------------------------------------------------------------
114

115     /**
116      * Constructor for create
117      *
118      */

119
120     public XATerminatorImpl() throws XAException JavaDoc {
121
122         if (TraceTm.jotm.isDebugEnabled()) {
123             TraceTm.jotm.debug("create XATerminator");
124         }
125
126         unique = this;
127     }
128
129     // ------------------------------------------------------------------
130
// Inflow Transaction implementation
131
// ------------------------------------------------------------------
132

133     /**
134      * Gets a String that represents the inflow transaction name.
135      *
136      * @return Transaction Name
137      */

138
139     public String JavaDoc get_transaction_name () throws XAException JavaDoc {
140
141         TraceTm.jotm.debug("XATerminatorImpl.get_transaction_name()");
142
143         return xid.toString();
144     }
145
146     /**
147      * Commits the global transaction specified by xid.
148      *
149      * @param xid A global transaction identifier
150      * @param onePhase If true, the resource manager should use one-phase commit protocol
151      * to commit the work done on behalf of xid.
152      *
153      * @exception XAException An error has occurred. Possible XAExceptions
154      * are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
155      * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
156      *
157      * If the resouce manager did not commit the transaction and the parameter onePhase is set to
158      * true, the resource manager may throw one of the XA_RB* exceptions. Upon return, the
159      * resource manager has rolled back the branch's work and has released all help resources.
160      */

161
162     public void commit (Xid xid, boolean onePhase) throws XAException JavaDoc {
163
164         if (TraceTm.jotm.isDebugEnabled()) {
165             TraceTm.jotm.debug("commit xid="+ xid + "onePhase="+ onePhase);
166         }
167
168         // get the transaction associated with the passed Xid
169
// if the Xid passed was not corrected the commit will fail
170

171         XidImpl myxid = new XidImpl(xid);
172         iftx = Current.getCurrent().getTxByXid(myxid);
173
174         if (iftx == null) {
175             TraceTm.jotm.error("XATerminatorImpl.commit(): unknown xid " + xid);
176             throw new XAException JavaDoc(XAER_NOTA);
177         }
178
179         try {
180             iftx.commit();
181         } catch (Exception JavaDoc e) {
182             TraceTm.jotm.error("XATerminatorImpl.commit(): commit raised exception ", e);
183         }
184     }
185
186     /**
187      * Informs the resource manager to roll back work done on behalf of a transaction branch.
188      *
189      * @param xid A global transaction identifier.
190      *
191      * @exception XAException An error has occurred. Possible XAExceptions
192      * are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
193      * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
194      *
195      * If the transaction branch is already marked rollback-only the resource manager may throw
196      * one of the XA_RB* exceptions. Upon return, the resource manager has rolled back the
197      * branch's work and has released all held resources.
198      */

199
200     public void rollback (Xid xid) throws XAException JavaDoc {
201
202         if (TraceTm.jotm.isDebugEnabled()) {
203             TraceTm.jotm.debug("rollback xid="+ xid);
204         }
205
206         // get the transaction associated with the passed Xid
207
// if the Xid passed was not corrected the rollback will fail
208

209         XidImpl myxid = new XidImpl(xid);
210         iftx = Current.getCurrent().getTxByXid(myxid);
211
212         if (iftx == null) {
213             TraceTm.jotm.error("XATerminatorImpl.rollback(): unknown xid " + xid);
214             throw new XAException JavaDoc(XAER_NOTA);
215         }
216
217         try {
218             iftx.rollback();
219         } catch (Exception JavaDoc e) {
220             TraceTm.jotm.error("XATerminatorImpl.rollback(): rollback raised exception ", e);
221         }
222     }
223
224     /**
225      * Ask the resource manager to prepare for a transaction commit of the transaction specified in xid.
226      *
227      * @param xid A global transaction identifier.
228      *
229      * @exception XAException An error has occurred. Possible XAExceptions
230      * are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
231      * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
232      *
233      * @return A value indicating the resource manager's vote on the outcome of the transaction. The
234      * possible values are: XA_RDONLY or XA_OK. These constants are defined in
235      * javax.transaction.xa.XAResource interface. If the resource manager wants to roll back
236      * the transaction, it should do so by raising an appropriate XAException in the prepare
237      * method.
238      */

239
240     public int prepare (Xid xid) throws XAException JavaDoc {
241
242         int ret = 0;
243
244         if (TraceTm.jotm.isDebugEnabled()) {
245             TraceTm.jotm.debug("prepare xid="+ xid);
246         }
247
248         // get the transaction associated with the passed Xid
249
// if the Xid passed was not corrected the prepare will fail
250

251         XidImpl myxid = new XidImpl(xid);
252         iftx = Current.getCurrent().getTxByXid(myxid);
253
254         if (iftx == null) {
255             TraceTm.jotm.error("XATerminatorImpl.prepare(): unknown xid " + xid);
256             throw new XAException JavaDoc(XAER_NOTA);
257         }
258
259         try {
260             ret = iftx.prepare();
261         } catch (Exception JavaDoc e) {
262             TraceTm.jotm.error("XATerminatorImpl.prepare(): prepare raised exception ", e);
263         }
264
265         return ret;
266     }
267
268     /**
269      * Tells the resource manager to forget about a heuristically completed transaction branch.
270      *
271      * @param xid A global transaction identifier.
272      *
273      * @exception XAException An error has occurred. Possible XAExceptions
274      * are XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
275      */

276
277     public void forget (Xid xid) throws XAException JavaDoc {
278
279         TraceTm.jotm.debug("XATerminatorImpl.forget()");
280
281         // get the transaction associated with the passed Xid
282
// if the Xid passed was not corrected the forget will fail
283

284         XidImpl myxid = new XidImpl(xid);
285
286         try {
287             Current.getCurrent().forgetTx(myxid);
288         } catch (Exception JavaDoc e) {
289             TraceTm.jotm.error("XATerminatorImpl.forget(): forget raised exception ", e);
290         }
291     }
292
293     /**
294      * Obtains a list of prepared transaction branches from a resource manager. The transaction manager
295      * calls this method during recovery to obtain the list of transaction branches that are currently in
296      * prepared or heuristically completed states.
297      *
298      * @param flag One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. TMHOFLAGS must
299      * be used when no other flags are set in the parameter. These constants are defined in
300      * javax.transaction.xa.XAResource interface.
301      *
302      * @exception XAException An error has occurred. Possible values
303      * are XAER_RMERR, XAER_RMFAIL, XAER_INVAL, or XAER_PROTO.
304      *
305      * @return The resource manager returns zero or more XIDs of the transaction branches that are
306      * currently in a prepared or heuristically completed state. If an error occurs during the
307      * operation, the resource manager should throw the appropriate XAException.
308      */

309
310     public Xid[] recover (int flag) throws XAException JavaDoc {
311
312         TraceTm.jotm.debug("XATerminatorImpl.recover()");
313
314         // get a list of transaction in the prepared or heuristically complete states
315

316         return (Xid []) Current.getCurrent().getPreparedHeuristicXid();
317
318     }
319
320 }
321
Popular Tags