KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > XAEnvironment


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: XAEnvironment.java,v 1.8 2006/10/30 21:14:12 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12
13 import javax.transaction.xa.XAException JavaDoc;
14 import javax.transaction.xa.XAResource JavaDoc;
15 import javax.transaction.xa.Xid JavaDoc;
16
17 import com.sleepycat.je.txn.Txn;
18 import com.sleepycat.je.txn.TxnManager;
19
20 /**
21  * Javadoc for this public class is generated
22  * via the doc templates in the doc_src directory.
23  */

24 public class XAEnvironment extends Environment implements XAResource JavaDoc {
25
26     private static final boolean DEBUG = false;
27
28     /**
29      * Javadoc for this public method is generated via
30      * the doc templates in the doc_src directory.
31      */

32     public XAEnvironment(File JavaDoc envHome, EnvironmentConfig configuration)
33         throws DatabaseException {
34
35     super(envHome, configuration);
36     }
37
38     /**
39      * Used to get the Transaction object given an XA Xid.
40      */

41     public Transaction getXATransaction(Xid JavaDoc xid)
42     throws DatabaseException {
43
44     Txn ret = environmentImpl.getTxnManager().getTxnFromXid(xid);
45     if (ret == null) {
46         return null;
47     }
48     /* Do we guarantee object identity for Transaction objects? */
49     return new Transaction(this, ret);
50     }
51
52     /**
53      * Used to set the Transaction object for an XA Xid. Public for tests.
54      */

55     public void setXATransaction(Xid JavaDoc xid, Transaction txn)
56     throws DatabaseException {
57
58     environmentImpl.getTxnManager().
59         registerXATxn(xid, txn.getTxn(), false);
60     }
61
62     /*
63      * XAResource methods.
64      */

65
66     /**
67      * Javadoc for this public method is generated via
68      * the doc templates in the doc_src directory.
69      */

70     public void commit(Xid JavaDoc xid, boolean ignore /*onePhase*/)
71     throws XAException JavaDoc {
72
73     if (DEBUG) {
74         System.out.println("*** commit called " + xid + "/" + ignore);
75     }
76
77     if (xid == null) {
78         return;
79     }
80
81     try {
82         checkEnv();
83         Transaction txn = getXATransaction(xid);
84         if (txn == null) {
85         throw new XAException JavaDoc
86             ("No transaction found for " + xid + " during commit.");
87         }
88         removeReferringHandle(txn);
89         if (txn.getTxn().getOnlyAbortable()) {
90         throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
91         }
92         txn.getTxn().commit(xid);
93     } catch (DatabaseException DE) {
94         throwNewXAException(DE);
95     }
96     if (DEBUG) {
97         System.out.println("*** commit finished");
98     }
99     }
100
101     /**
102      * Javadoc for this public method is generated via
103      * the doc templates in the doc_src directory.
104      */

105     public void end(Xid JavaDoc xid, int flags)
106     throws XAException JavaDoc {
107
108     if (DEBUG) {
109         System.out.println("*** end called " + xid + "/" + flags);
110     }
111
112     /* flags - One of TMSUCCESS, TMFAIL, or TMSUSPEND. */
113
114     boolean tmFail = (flags & XAResource.TMFAIL) != 0;
115     boolean tmSuccess = (flags & XAResource.TMSUCCESS) != 0;
116     boolean tmSuspend = (flags & XAResource.TMSUSPEND) != 0;
117     if ((tmFail && tmSuccess) ||
118         ((tmFail || tmSuccess) && tmSuspend)) {
119         throw new XAException JavaDoc(XAException.XAER_INVAL);
120     }
121
122     try {
123         if (DEBUG) {
124         System.out.println
125             ("Transaction for " + Thread.currentThread() + " is " +
126              environmentImpl.getTxnManager().getTxnForThread());
127         }
128
129         Transaction txn =
130         environmentImpl.getTxnManager().unsetTxnForThread();
131         if (txn == null) {
132         txn = getXATransaction(xid);
133         boolean isSuspended = (txn != null) &&
134             txn.getTxn().isSuspended();
135         if (!isSuspended) {
136             throw new XAException JavaDoc(XAException.XAER_NOTA);
137         }
138         }
139
140         if (tmFail) {
141         txn.getTxn().setOnlyAbortable();
142         }
143
144         if (tmSuspend) {
145         txn.getTxn().setSuspended(true);
146         }
147
148     } catch (DatabaseException DE) {
149         throwNewXAException(DE);
150     }
151     }
152
153     /**
154      * Javadoc for this public method is generated via
155      * the doc templates in the doc_src directory.
156      */

157     public void forget(Xid JavaDoc xid)
158     throws XAException JavaDoc {
159
160     if (DEBUG) {
161         System.out.println("*** forget called");
162     }
163
164     throw new XAException JavaDoc(XAException.XAER_NOTA);
165     }
166
167     /**
168      * Javadoc for this public method is generated via
169      * the doc templates in the doc_src directory.
170      */

171     public boolean isSameRM(XAResource JavaDoc rm)
172     throws XAException JavaDoc {
173
174     if (DEBUG) {
175         System.out.println("*** isSameRM called");
176     }
177
178     try {
179         checkEnv();
180     } catch (DatabaseException DE) {
181         throwNewXAException(DE);
182     }
183
184     if (rm == null) {
185         return false;
186     }
187
188     if (!(rm instanceof XAEnvironment)) {
189         return false;
190     }
191
192     return environmentImpl ==
193         DbInternal.envGetEnvironmentImpl((XAEnvironment) rm);
194     }
195
196     /**
197      * Javadoc for this public method is generated via
198      * the doc templates in the doc_src directory.
199      */

200     public int prepare(Xid JavaDoc xid)
201     throws XAException JavaDoc {
202
203     if (DEBUG) {
204         System.out.println("*** prepare called");
205     }
206
207     try {
208         checkEnv();
209         Transaction txn = getXATransaction(xid);
210         if (txn == null) {
211         throw new XAException JavaDoc
212             ("No transaction found for " + xid + " during prepare.");
213         }
214         txn.getTxn().prepare(xid);
215
216         if (DEBUG) {
217         System.out.println("*** prepare returning XA_OK");
218         }
219
220         return XAResource.XA_OK;
221     } catch (DatabaseException DE) {
222         throwNewXAException(DE);
223     }
224     return XAResource.XA_OK; // for compiler
225
}
226
227     /**
228      * Javadoc for this public method is generated via
229      * the doc templates in the doc_src directory.
230      */

231     public Xid JavaDoc[] recover(int flags)
232     throws XAException JavaDoc {
233
234     if (DEBUG) {
235         System.out.println("*** recover called");
236     }
237
238     /* flags - One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. */
239
240     boolean tmStartRScan = (flags & XAResource.TMSTARTRSCAN) != 0;
241     boolean tmEndRScan = (flags & XAResource.TMENDRSCAN) != 0;
242     if ((tmStartRScan && tmEndRScan) ||
243         (!tmStartRScan && !tmEndRScan && flags != TMNOFLAGS)) {
244         throw new XAException JavaDoc(XAException.XAER_INVAL);
245     }
246
247     /*
248      * We don't have to actually do anything with STARTRSCAN or ENDRSCAN
249      * since we return the whole set of Xid's to be recovered on each call.
250      */

251     try {
252         checkHandleIsValid();
253         checkEnv();
254
255         if (DEBUG) {
256         System.out.println("*** recover returning1");
257         }
258
259         return environmentImpl.getTxnManager().XARecover();
260     } catch (DatabaseException DE) {
261         throwNewXAException(DE);
262     }
263     return null; // for compiler
264
}
265
266     /**
267      * Javadoc for this public method is generated via
268      * the doc templates in the doc_src directory.
269      */

270     public void rollback(Xid JavaDoc xid)
271     throws XAException JavaDoc {
272
273     if (DEBUG) {
274         System.out.println("*** rollback called");
275     }
276
277     try {
278         checkEnv();
279         Transaction txn = getXATransaction(xid);
280         if (txn == null) {
281         throw new XAException JavaDoc
282             ("No transaction found for " + xid + " during abort.");
283         }
284         removeReferringHandle(txn);
285         txn.getTxn().abort(xid);
286     } catch (DatabaseException DE) {
287         throwNewXAException(DE);
288     }
289
290     if (DEBUG) {
291         System.out.println("*** rollback returning");
292     }
293     }
294
295     /**
296      * Javadoc for this public method is generated via
297      * the doc templates in the doc_src directory.
298      */

299     public int getTransactionTimeout()
300     throws XAException JavaDoc {
301
302     try {
303         return (int) ((getConfig().getTxnTimeout() + 999999L) / 1000000L);
304     } catch (Exception JavaDoc DE) {
305         throwNewXAException(DE);
306     }
307     return 0; // for compiler
308
}
309
310     /**
311      * Javadoc for this public method is generated via
312      * the doc templates in the doc_src directory.
313      */

314     public boolean setTransactionTimeout(int timeout)
315     throws XAException JavaDoc {
316
317     return false;
318     }
319
320     /**
321      * Javadoc for this public method is generated via
322      * the doc templates in the doc_src directory.
323      */

324     public void start(Xid JavaDoc xid, int flags)
325     throws XAException JavaDoc {
326
327     if (DEBUG) {
328         System.out.println("*** start called " + xid + "/" + flags);
329     }
330
331     boolean tmJoin = (flags & XAResource.TMJOIN) != 0;
332     boolean tmResume = (flags & XAResource.TMRESUME) != 0;
333
334     /* Check flags - only one of TMNOFLAGS, TMJOIN, or TMRESUME. */
335     if (xid == null ||
336         (tmJoin && tmResume) ||
337         (!tmJoin &&
338          !tmResume &&
339          flags != XAResource.TMNOFLAGS)) {
340         throw new XAException JavaDoc(XAException.XAER_INVAL);
341     }
342
343     try {
344         Transaction txn = getXATransaction(xid);
345         TxnManager txnMgr = environmentImpl.getTxnManager();
346
347         if (flags == XAResource.TMNOFLAGS) {
348
349         /*
350          * If neither RESUME nor JOIN was set, make sure xid doesn't
351          * exist in allXATxns. Throw XAER_DUPID if it does.
352          */

353         if (txn == null) {
354             if (DEBUG) {
355             System.out.println
356                 ("Transaction for XID " + xid + " being created");
357             }
358
359             txn = beginTransaction(null, null);
360             setXATransaction(xid, txn);
361
362         } else {
363             throw new XAException JavaDoc(XAException.XAER_DUPID);
364         }
365         } else if (tmJoin) {
366         if (txn == null) {
367             throw new XAException JavaDoc(XAException.XAER_NOTA);
368         }
369
370         if (txnMgr.getTxnForThread() != null) {
371             throw new XAException JavaDoc(XAException.XAER_PROTO);
372         }
373         } else if (tmResume) {
374         if (txn == null) {
375             throw new XAException JavaDoc(XAException.XAER_NOTA);
376         }
377
378         if (!txn.getTxn().isSuspended()) {
379             throw new XAException JavaDoc(XAException.XAER_PROTO);
380         }
381         txn.getTxn().setSuspended(false);
382         }
383
384         if (DEBUG) {
385         System.out.println
386             ("Setting Transaction for " + Thread.currentThread());
387         }
388         txnMgr.setTxnForThread(txn);
389     } catch (DatabaseException DE) {
390         if (DEBUG) {
391         System.out.println("*** start exception");
392         }
393         throwNewXAException(DE);
394     }
395
396     if (DEBUG) {
397         System.out.println("*** start finished");
398     }
399     }
400
401     private void throwNewXAException(Exception JavaDoc E)
402     throws XAException JavaDoc {
403
404     XAException JavaDoc ret = new XAException JavaDoc(E.toString());
405     ret.initCause(E);
406     throw ret;
407     }
408 }
409
Popular Tags