KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > jta > TransactionImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.jts.jta;
30
31 import java.util.*;
32 import org.omg.CosTransactions.*;
33 import javax.transaction.*;
34 import javax.transaction.xa.*;
35
36 import com.sun.jts.CosTransactions.Configuration;
37 import com.sun.jts.CosTransactions.ControlImpl;
38 import com.sun.jts.CosTransactions.TopCoordinator;
39 import com.sun.jts.CosTransactions.GlobalTID;
40
41 import javax.transaction.Synchronization JavaDoc;
42 import javax.transaction.SystemException JavaDoc;
43
44 import org.omg.CosTransactions.Status;
45 import org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc;
46 import org.omg.CORBA.INVALID_TRANSACTION JavaDoc;
47 import org.omg.CORBA.NO_PERMISSION JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49 import java.util.logging.Level JavaDoc;
50 import com.sun.logging.LogDomains;
51 /**
52  * An implementation of javax.transaction.Transaction using JTS
53  * XXX TODO should catch all org.omg.CORBA.SystemException
54  * and throw javax.transaction.SystemException
55  *
56  * @author Tony Ng
57  */

58 public class TransactionImpl implements Transaction {
59
60     /**
61      * OTS Control object for this transaction
62      */

63     private Control control;
64
65     private GlobalTID gtid;
66
67     private TransactionState tranState = null;
68
69     private static TransactionManagerImpl tm = TransactionManagerImpl.getTransactionManagerImpl();
70     /*
71         Logger to log transaction messages
72     */

73     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
74
75     // START 4662745
76
private long startTime;
77     // END 4662745
78

79     public TransactionImpl(TransactionManagerImpl tm,
80                            Control control, GlobalTID gtid)
81         throws SystemException JavaDoc {
82
83         this.control = control;
84         this.gtid = gtid;
85         this.tm = tm;
86         // START 4662745
87
startTime=System.currentTimeMillis();
88         // END 4662745
89
}
90
91     public TransactionImpl(Control control, GlobalTID gtid)
92         throws SystemException JavaDoc {
93
94         this.control = control;
95         this.gtid = gtid;
96         startTime=System.currentTimeMillis();
97     }
98
99     /**
100      * return the OTS Control object for this transaction.
101      */

102     Control getControl() {
103         return control;
104     }
105
106     //-----------------------------------------------------------------
107
// The following implements javax.transaction.Trasaction interface
108
//-----------------------------------------------------------------
109

110     /**
111      * Complete the transaction represented by this Transaction object
112      */

113     public void commit() throws HeuristicMixedException,
114         RollbackException, HeuristicRollbackException, IllegalStateException JavaDoc,
115         SecurityException JavaDoc, SystemException JavaDoc
116     {
117         try {
118             if (Configuration.isLocalFactory()) {
119               ((ControlImpl) control).get_localTerminator().commit(true);
120             } else {
121                 control.get_terminator().commit(true);
122             }
123         } catch (TRANSACTION_ROLLEDBACK JavaDoc ex) {
124             throw new RollbackException();
125         } catch (INVALID_TRANSACTION JavaDoc ex) {
126             throw new IllegalStateException JavaDoc();
127         } catch (HeuristicMixed ex) {
128             throw new HeuristicMixedException();
129         } catch (HeuristicHazard ex) {
130             throw new HeuristicMixedException();
131         } catch (NO_PERMISSION JavaDoc ex) {
132             throw new SecurityException JavaDoc();
133         } catch (Unavailable ex) {
134             SystemException JavaDoc sException = new SystemException JavaDoc();
135             sException.initCause(ex);
136             throw sException;
137         } catch (Exception JavaDoc ex) {
138             SystemException JavaDoc sException = new SystemException JavaDoc();
139             sException.initCause(ex);
140             throw sException;
141         }
142
143     }
144
145
146     /**
147      * Rollback the transaction represented by this Transaction object.
148      */

149     public void rollback()
150         throws IllegalStateException JavaDoc, SystemException JavaDoc {
151
152         try {
153             if (Configuration.isLocalFactory()) {
154               ((ControlImpl) control).get_localTerminator().rollback();
155             } else {
156               control.get_terminator().rollback();
157             }
158         } catch (INVALID_TRANSACTION JavaDoc ex) {
159             throw new IllegalStateException JavaDoc();
160         } catch (TRANSACTION_ROLLEDBACK JavaDoc ex) {
161             throw new IllegalStateException JavaDoc();
162         } catch (Unavailable ex) {
163             SystemException JavaDoc sException = new SystemException JavaDoc();
164             sException.initCause(ex);
165             throw sException;
166         } catch (Exception JavaDoc ex) {
167             SystemException JavaDoc sException = new SystemException JavaDoc();
168             sException.initCause(ex);
169             throw sException;
170         }
171     }
172
173
174     /**
175      * enlist a resource with the current transaction
176      * If a transaction is marked as rollback, enlistment will
177      * succeed if the resource has been enlisted before. Otherwise,
178      * enlistment will fail. In both cases, a RollbackException will
179      * be thrown.
180      */

181     public boolean enlistResource(XAResource res)
182         throws RollbackException, IllegalStateException JavaDoc,
183             SystemException JavaDoc {
184
185         int status = getStatus();
186         if (status != javax.transaction.Status.STATUS_ACTIVE &&
187             status != javax.transaction.Status.STATUS_MARKED_ROLLBACK) {
188             throw new IllegalStateException JavaDoc();
189         }
190     //START IASRI 4706150
191
try{
192         if(tm.getXAResourceTimeOut() > 0)
193             res.setTransactionTimeout(tm.getXAResourceTimeOut());
194     }catch(Exception JavaDoc ex){
195         _logger.log(Level.WARNING,"jts.error_while_setting_xares_txn_timeout",ex);
196     }
197     //END IASRI 4706150
198
try {
199             if (tranState == null) {
200                 tranState = new TransactionState(gtid, this);
201                 // Synchronization sync = new SynchronizationListener(tranState);
202
// registerSynchronization(sync);
203
}
204             tranState.startAssociation(res, control, status);
205             if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK) {
206                 throw new RollbackException();
207             }
208             return true;
209         } catch (XAException ex) {
210             _logger.log(Level.WARNING,"jts.resource_outside_transaction",ex);
211             if (ex.errorCode == XAException.XAER_OUTSIDE) {
212                 throw new IllegalStateException JavaDoc();
213             }
214             // XXX FIXME should throw rollback exception on XARB_*
215
// for now just throw SystemException
216
throw new SystemException JavaDoc();
217         }
218
219     }
220
221     public boolean delistResource(XAResource res, int flags)
222         throws IllegalStateException JavaDoc, SystemException JavaDoc {
223
224         /*
225         int status = getStatus();
226         if (status != javax.transaction.Status.STATUS_ACTIVE &&
227             status != javax.transaction.Status.STATUS_MARKED_ROLLBACK) {
228             throw new IllegalStateException();
229         }
230         */

231
232         try {
233             // TransactionState tranState = tm.getTransactionState(gtid, this);
234
if (tranState == null) {
235                 // transaction has completed
236
throw new IllegalStateException JavaDoc();
237             }
238             if (tranState.containsXAResource(res) == false) {
239                 throw new IllegalStateException JavaDoc();
240             }
241             tranState.endAssociation(res, flags);
242             if ((flags & XAResource.TMFAIL) != 0) {
243                 // set transaction to rollback only if TMFAIL used
244
setRollbackOnly();
245             }
246             return true;
247         } catch (XAException ex) {
248             throw new SystemException JavaDoc();
249         }
250     }
251
252     public int getStatus() throws SystemException JavaDoc {
253         // XXX what should getStatus return on exception?
254
Status status;
255         try {
256             if (Configuration.isLocalFactory()) {
257               status = ((ControlImpl) control).get_localCoordinator().get_status();
258             } else {
259               status = control.get_coordinator().get_status();
260             }
261             return TransactionManagerImpl.mapStatus(status);
262         } catch (TRANSACTION_ROLLEDBACK JavaDoc ex) {
263             return javax.transaction.Status.STATUS_NO_TRANSACTION;
264         } catch (INVALID_TRANSACTION JavaDoc ex) {
265             return javax.transaction.Status.STATUS_NO_TRANSACTION;
266         } catch (Unavailable ex) {
267             return javax.transaction.Status.STATUS_NO_TRANSACTION;
268         } catch (Exception JavaDoc ex) {
269             _logger.log(Level.WARNING,"jts.unexpected_error_in_getstatus",ex);
270             throw new SystemException JavaDoc();
271         }
272     }
273
274     public boolean equals(Object JavaDoc object) {
275         if ((object instanceof TransactionImpl) == false) {
276             return false;
277         } else if (object == this) {
278             return true;
279         } else {
280             return gtid.equals(((TransactionImpl) object).gtid);
281         }
282     }
283
284     public int hashCode() {
285         return gtid.hashCode();
286     }
287
288     public void registerSynchronization(Synchronization JavaDoc sync)
289         throws RollbackException, IllegalStateException JavaDoc,
290         SystemException JavaDoc {
291
292         int status = getStatus();
293         if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK) {
294             throw new RollbackException();
295         }
296         if (status != javax.transaction.Status.STATUS_ACTIVE) {
297             throw new IllegalStateException JavaDoc();
298         }
299         if (tranState == null) {
300             tranState = new TransactionState(gtid, this);
301         }
302         tranState.registerSynchronization(sync, control, false);
303     }
304
305     public void registerInterposedSynchronization(Synchronization JavaDoc sync)
306         throws RollbackException, IllegalStateException JavaDoc,
307         SystemException JavaDoc {
308
309         int status = getStatus();
310         if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK) {
311             throw new RollbackException();
312         }
313         if (status != javax.transaction.Status.STATUS_ACTIVE) {
314             throw new IllegalStateException JavaDoc();
315         }
316         if (tranState == null) {
317             tranState = new TransactionState(gtid, this);
318         }
319         tranState.registerSynchronization(sync, control, true);
320     }
321
322     public void setRollbackOnly()
323         throws IllegalStateException JavaDoc, SystemException JavaDoc {
324
325         int status = getStatus();
326         if (status != javax.transaction.Status.STATUS_MARKED_ROLLBACK &&
327             status != javax.transaction.Status.STATUS_ACTIVE) {
328             throw new IllegalStateException JavaDoc();
329         }
330         try {
331             if (Configuration.isLocalFactory()) {
332               ((ControlImpl) control).get_localCoordinator().rollback_only();
333             } else {
334               control.get_coordinator().rollback_only();
335             }
336         } catch (Unavailable ex) {
337             throw new SystemException JavaDoc();
338         } catch (Inactive ex) {
339             throw new IllegalStateException JavaDoc();
340         } catch (Exception JavaDoc ex) {
341             throw new SystemException JavaDoc();
342         }
343     }
344
345
346     /**
347      * a simple assertion mechanism that print stack trace
348      * if assertion fails
349      */

350     static private void assert_prejdk14(boolean value) {
351         if (!value) {
352             Exception JavaDoc e = new Exception JavaDoc();
353             _logger.log(Level.WARNING,"jts.assert",e);
354         }
355     }
356     // START IASRI 4662745
357
/*
358      * This method is used for the Admin Framework displaying
359      * of Transactions Ids
360      */

361     public String JavaDoc getTransactionId(){
362         return gtid.toString();
363     }
364
365     /*
366      * This method returns the time this transaction was started
367      */

368     public long getStartTime(){
369         return startTime;
370     }
371     // END IASRI 4662745
372

373
374 /**
375 class SynchronizationListener implements Synchronization {
376
377     private GlobalTID gtid;
378     private TransactionState tranState;
379
380     SynchronizationListener(TransactionState tranState) {
381         this.tranState = tranState;
382     }
383
384     public void afterCompletion(int status) {
385         // tranState.cleanupTransactionStateMapping();
386     }
387
388     public void beforeCompletion() {
389         try {
390         tranState.beforeCompletion();
391     }catch(XAException xaex){
392         _logger.log(Level.WARNING,"jts.unexpected_xa_error_in_beforecompletion", new java.lang.Object[] {new Integer(xaex.errorCode), xaex.getMessage()});
393         _logger.log(Level.WARNING,"",xaex);
394         } catch (Exception ex) {
395         _logger.log(Level.WARNING,"jts.unexpected_error_in_beforecompletion",ex);
396         }
397     }
398 }
399 **/

400
401 }
402
Popular Tags