KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > simpletrans > SimpleTransactionManager


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.simpletrans;
56
57 import java.io.Serializable JavaDoc;
58 import javax.naming.Reference JavaDoc;
59 import javax.naming.Referenceable JavaDoc;
60 import javax.transaction.HeuristicMixedException JavaDoc;
61 import javax.transaction.HeuristicRollbackException JavaDoc;
62 import javax.transaction.RollbackException JavaDoc;
63 import javax.transaction.Status JavaDoc;
64 import javax.transaction.SystemException JavaDoc;
65 import javax.transaction.Transaction JavaDoc;
66 import javax.transaction.TransactionManager JavaDoc;
67 import javax.transaction.UserTransaction JavaDoc;
68
69 import org.apache.log4j.Logger;
70
71 /**
72  * a singleton implementation of a transaction manager, using thread-local variables to store
73  * the current transaction.
74  *
75  * @author J R Briggs
76  */

77 public class SimpleTransactionManager implements TransactionManager JavaDoc, UserTransaction JavaDoc, Serializable JavaDoc, Referenceable JavaDoc {
78   private static final Logger log = Logger.getLogger(SimpleTransactionManager.class.getName());
79   private static Reference JavaDoc REF = new Reference JavaDoc(SimpleTransactionManager.class.getName());
80   private static final SimpleTransactionManager stm = new SimpleTransactionManager();
81   
82   private static final Boolean JavaDoc FALSE = new Boolean JavaDoc(false);
83   private static final Boolean JavaDoc TRUE = new Boolean JavaDoc(true);
84   
85   private static ThreadLocal JavaDoc IN_TRANS = new ThreadLocal JavaDoc() {
86     protected Object JavaDoc initialValue() {
87       return FALSE;
88     }
89   };
90   
91   private static ThreadLocal JavaDoc TRANS = new ThreadLocal JavaDoc() {
92     protected Object JavaDoc initialValue() {
93       return new SimpleTransaction();
94     }
95   };
96   
97   public static final SimpleTransactionManager getTransactionManager() {
98     return stm;
99   }
100   
101   public Reference JavaDoc getReference() {
102     return REF;
103   }
104   
105  /**
106   * begin a transaction. This method differs from that specified by the TransactionManager
107   * interface, in that it doesn't throw an exception if the current thread is already in
108   * a transaction -- and it doesn't create a nested transaction. This basically
109   * assumes that everything will run in the current transaction.
110   */

111   public void begin() {
112     if (!isInTransaction()) {
113       IN_TRANS.set(TRUE);
114       Transaction JavaDoc t = new SimpleTransaction();
115       TRANS.set(t);
116       if (log.isDebugEnabled()) {
117         log.debug("started new transaction " + t);
118       }
119     }
120     else if (log.isDebugEnabled()) {
121       log.debug("new transaction not started, already in trans");
122     }
123   }
124   
125  /**
126   * commit the current transaction (note: this doesn't throw an error if the
127   * current thread is not in a transaction)
128   */

129   public void commit() throws RollbackException JavaDoc, HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc, SystemException JavaDoc {
130     if (isInTransaction()) {
131       Transaction JavaDoc trans = (Transaction JavaDoc)TRANS.get();
132       trans.commit();
133       TRANS.set(null);
134       IN_TRANS.set(FALSE);
135       if (log.isDebugEnabled()) {
136         log.debug("committed transaction " + trans);
137       }
138     }
139     else if (log.isDebugEnabled()) {
140       log.debug("not in transaction, no commit");
141     }
142   }
143  
144  /**
145   * get the status of the current transaction
146   */

147   public int getStatus() throws SystemException JavaDoc {
148     if (isInTransaction()) {
149       Transaction JavaDoc trans = (Transaction JavaDoc)TRANS.get();
150       return trans.getStatus();
151     }
152     else {
153       return Status.STATUS_NO_TRANSACTION;
154     }
155   }
156   
157  /**
158   * get the transaction for the current (calling) thread
159   */

160   public Transaction JavaDoc getTransaction() {
161     if (isInTransaction()) {
162       return (Transaction JavaDoc)TRANS.get();
163     }
164     else {
165       return null;
166     }
167   }
168   
169  /**
170   * not supported
171   */

172   public void resume(Transaction JavaDoc transaction) {
173     throw new UnsupportedOperationException JavaDoc();
174   }
175   
176  /**
177   * rollback the current transaction (note: this doesn't throw an error if the
178   * current thread is not in a transaction)
179   */

180   public void rollback() throws SystemException JavaDoc {
181     if (isInTransaction()) {
182       Transaction JavaDoc trans = (Transaction JavaDoc)TRANS.get();
183       trans.rollback();
184       TRANS.set(null);
185       IN_TRANS.set(FALSE);
186       if (log.isDebugEnabled()) {
187         log.debug("rolled back transaction");
188       }
189     }
190     else if (log.isDebugEnabled()) {
191       log.debug("not in transaction, no commit");
192     }
193   }
194   
195  /**
196   * flag the current transaction for rollback (note: this doesn't throw an error
197   * if the current thread is not in a transaction)
198   */

199   public void setRollbackOnly() throws SystemException JavaDoc {
200     if (isInTransaction()) {
201       Transaction JavaDoc trans = (Transaction JavaDoc)TRANS.get();
202       trans.setRollbackOnly();
203     }
204   }
205   
206  /**
207   * not supported
208   */

209   public void setTransactionTimeout(int param) {
210     throw new UnsupportedOperationException JavaDoc();
211   }
212   
213  /**
214   * not supported
215   */

216   public Transaction JavaDoc suspend() {
217     throw new UnsupportedOperationException JavaDoc();
218   }
219   
220  /**
221   * a helper function to indicate if the calling thread is in a transaction
222   */

223   protected boolean isInTransaction() {
224     Boolean JavaDoc b = (Boolean JavaDoc)IN_TRANS.get();
225     if (b.booleanValue()) {
226       Transaction JavaDoc trans = (Transaction JavaDoc)TRANS.get();
227       try {
228         if (trans != null && trans.getStatus() != Status.STATUS_NO_TRANSACTION
229             && trans.getStatus() != Status.STATUS_UNKNOWN) {
230           return true;
231         }
232       }
233       catch (Exception JavaDoc e) {
234         e.printStackTrace();
235       }
236     }
237     return false;
238   }
239   
240
241 }
Popular Tags