KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > transaction > UserTransactionWrapper


1 /*
2  * CoadunationLib: The coaduntion library.
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * UserTransactionWrapper.java
20  */

21
22 // the package path
23
package com.rift.coad.util.transaction;
24
25 // java imports
26
import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.transaction.UserTransaction JavaDoc;
29 import javax.transaction.TransactionManager JavaDoc;
30 import javax.transaction.Status JavaDoc;
31
32 // logging import
33
import org.apache.log4j.Logger;
34
35
36 /**
37  * This object is responsible for wrapping the user transaction.
38  *
39  * @author Brett Chaldecott
40  */

41 public class UserTransactionWrapper {
42     
43     /**
44      * This object contains the information about a single transaction.
45      */

46     public class TransactionInfo {
47         
48         // private member variable
49
private boolean ownLock = false;
50         private int lockCount = 0;
51         private boolean committed = false;
52         
53         /**
54          * The constructor of the transaction information object.
55          */

56         public TransactionInfo(boolean ownLock) {
57             this.ownLock = ownLock;
58         }
59         
60         
61         /**
62          * This method returns true if this object owns the lock.
63          *
64          * @return This method returns TRUE if this lock is owned buy this
65          * object.
66          */

67         public boolean getOwnLock() {
68             return ownLock;
69         }
70         
71         
72         /**
73          * This method returns the lock count for this transaction.
74          *
75          * @return The lock value.
76          */

77         public int lock() {
78             return ++lockCount;
79         }
80         
81         
82         /**
83          * This method returns the lock count.
84          *
85          * @return The lock value.
86          */

87         public int unlock() {
88             if (lockCount == 0) {
89                 return lockCount;
90             }
91             return --lockCount;
92         }
93         
94         
95         /**
96          * This method returns the lock count for the current transaction.
97          *
98          * @return The current value of the lock count.
99          */

100         public int getLockCount() {
101             return lockCount;
102         }
103         
104         
105         /**
106          * This method returns true if this object is committed.
107          *
108          * @return TRUE if committed, FALSE if not.
109          */

110         public boolean getCommited() {
111             return committed;
112         }
113         
114         
115         /**
116          * This method sets the committed flag to true.
117          *
118          * @exception TransactionException
119          */

120         public void commit() throws TransactionException {
121             try {
122                 ut.commit();
123             } catch (java.lang.NullPointerException JavaDoc ex) {
124                 log.error("Failed to commit the changes because of null " +
125                         "pointer exception. Assuming cleanup was successfull : "
126                         + ex.getMessage(),ex);
127             } catch (Exception JavaDoc ex) {
128                 
129                 log.error("Failed to commit the changes : "
130                         + ex.getMessage(),ex);
131                 throw new TransactionException("Failed to commit the changes : "
132                         + ex.getMessage(),ex);
133             } finally {
134                 lockCount--;
135                 committed = true;
136             }
137             
138         }
139     }
140     
141     // private member variables
142
protected static Logger log =
143             Logger.getLogger(UserTransactionWrapper.class.getName());
144     
145     // private member variables
146
private Context JavaDoc context = null;
147     private UserTransaction JavaDoc ut = null;
148     private TransactionManager transactionManager = null;
149     private ThreadLocal JavaDoc currentTransaction = new ThreadLocal JavaDoc();
150     
151     /**
152      * Creates a new instance of UserTransactionWrapper
153      */

154     public UserTransactionWrapper() throws TransactionException {
155         try {
156             context = new InitialContext JavaDoc();
157             ut = (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
158         } catch (Exception JavaDoc ex) {
159             throw new TransactionException("Failed to instanciate the " +
160                     "UserTransactionWrapper because : " + ex.getMessage(),ex);
161         }
162         
163     }
164     
165     
166     /**
167      * This method begins a transaction for a thread, if one is not already
168      * running.
169      *
170      * @exception TransactionException
171      */

172     public void begin() throws TransactionException {
173         try {
174             TransactionInfo trans = (TransactionInfo)currentTransaction.get();
175             if (trans == null) {
176                 if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) {
177                     ut.begin();
178                     trans = new TransactionInfo(true);
179                     currentTransaction.set(trans);
180                 } else {
181                     trans = new TransactionInfo(false);
182                     currentTransaction.set(trans);
183                 }
184             }
185             trans.lock();
186         } catch (Exception JavaDoc ex) {
187             throw new TransactionException("Failed to start the transaction : " +
188                     ex.getMessage(),ex);
189         }
190     }
191     
192     
193     /**
194      * This method commits the transaction
195      *
196      * @exception TransactionException
197      */

198     public void commit() throws TransactionException {
199         try {
200             TransactionInfo trans = (TransactionInfo)currentTransaction.get();
201             if (trans == null) {
202                 throw new TransactionException(
203                         "There is no transaction for this thread");
204             } else if (trans.getOwnLock() == false) {
205                 log.info("Commit called on transaction not owned by this object");
206                 return;
207             } else if (trans.getLockCount() != 1) {
208                 throw new TransactionException(
209                         "This transaction cannot be commit at this point as " +
210                         "there are two many recursions. " +
211                         "Must be commit at the top.");
212             }
213             trans.commit();
214         } catch (TransactionException ex) {
215             throw ex;
216         } catch (Exception JavaDoc ex) {
217             throw new TransactionException("Failed to start the transaction : " +
218                     ex.getMessage(),ex);
219         }
220     }
221     
222     
223     /**
224      * This method is called to release a lock on a transaction and will result
225      * in rollback if the transaction is not commited
226      *
227      * @exception TransactionException;
228      */

229     public void release() {
230         try {
231             TransactionInfo trans = (TransactionInfo)currentTransaction.get();
232             if (trans == null) {
233                 return;
234             }
235             if ((0 == trans.unlock()) && trans.getOwnLock() &&
236                     !trans.getCommited() &&
237                     (ut.getStatus() == Status.STATUS_ACTIVE)) {
238                 ut.rollback();
239             }
240             if (trans.getLockCount() == 0) {
241                 currentTransaction.set(null);
242             }
243         } catch (Exception JavaDoc ex) {
244             log.error("Failed to release the transaction : " +
245                     ex.getMessage(),ex);
246         }
247     }
248     
249     
250 }
251
Popular Tags