KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > transaction > TransactionContainer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.transaction;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.naming.InitialContext JavaDoc;
35 import javax.transaction.Status JavaDoc;
36 import javax.transaction.Transaction JavaDoc;
37 import javax.transaction.TransactionManager JavaDoc;
38 import javax.transaction.UserTransaction JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Transaction identifier implementation.
44  */

45 public class TransactionContainer {
46   private static final L10N L = new L10N(TransactionContainer.class);
47   private static final Logger JavaDoc log = Log.open(TransactionContainer.class);
48
49   private static TransactionContainer _container;
50
51   private UserTransaction JavaDoc _userTM;
52   private TransactionManager JavaDoc _tm;
53
54   public static TransactionContainer getTransactionContainer()
55   {
56     if (_container == null) {
57       _container = new TransactionContainer();
58
59       try {
60     InitialContext JavaDoc ic = new InitialContext JavaDoc();
61
62     UserTransaction JavaDoc userTM;
63     userTM = (UserTransaction JavaDoc) ic.lookup("java:comp/UserTransaction");
64
65     _container.setUserTransaction(userTM);
66
67     TransactionManager JavaDoc tm;
68     tm = (TransactionManager JavaDoc) ic.lookup("java:comp/TransactionManager");
69     
70     _container.setTransactionManager(tm);
71       } catch (Throwable JavaDoc e) {
72     log.log(Level.WARNING, e.toString(), e);
73       }
74     }
75
76     return _container;
77   }
78
79   /**
80    * Sets the user transaction.
81    */

82   public void setUserTransaction(UserTransaction JavaDoc userTM)
83   {
84     _userTM = userTM;
85   }
86
87   /**
88    * Sets the transaction manager.
89    */

90   public void setTransactionManager(TransactionManager JavaDoc tm)
91   {
92     _tm = tm;
93   }
94
95   /**
96    * Returns a transaction context for the "required" transaction. If
97    * there's already an active transaction, use it. Otherwise create a
98    * new transaction.
99    *
100    * @return the current transaction context
101    */

102   public Transaction JavaDoc beginRequired()
103   {
104     try {
105       Transaction JavaDoc currentTrans = _tm.getTransaction();
106
107       if (currentTrans != null)
108     return currentTrans;
109       
110       // _userTransaction.setTransactionTimeout((int) (_transactionTimeout / 1000L));
111
_userTM.begin();
112       
113       return null;
114     } catch (RuntimeException JavaDoc e) {
115       throw e;
116     } catch (Exception JavaDoc e) {
117       throw new TransactionRuntimeException(e);
118     }
119   }
120
121   /**
122    * Returns a transaction context for the "RequiresNew" transaction.
123    * Always creates a new transaction, suspending any old one.
124    *
125    * @return the current transaction context
126    */

127   public Transaction JavaDoc beginRequiresNew()
128   {
129     try {
130       Transaction JavaDoc oldTrans = _tm.getTransaction();
131
132       if (oldTrans != null)
133     oldTrans = _tm.suspend();
134
135       // _userTransaction.setTransactionTimeout((int) (_transactionTimeout / 1000L));
136
_userTM.begin();
137       
138       return oldTrans;
139     } catch (RuntimeException JavaDoc e) {
140       throw e;
141     } catch (Exception JavaDoc e) {
142       throw new TransactionRuntimeException(e);
143     }
144   }
145
146   /**
147    * Require a transaction, throwing an exception if none exists.
148    *
149    * @return the current transaction context
150    */

151   public void beginMandatory()
152   {
153     try {
154       Transaction JavaDoc oldTrans = _tm.getTransaction();
155
156       if (oldTrans == null)
157     throw new TransactionRuntimeException(L.l("'Mandatory' transaction boundary requires a transaction."));
158     } catch (RuntimeException JavaDoc e) {
159       throw e;
160     } catch (Exception JavaDoc e) {
161       throw new TransactionRuntimeException(e);
162     }
163   }
164
165   /**
166    * Require no transactions, throwing an exception if one exists.
167    */

168   public void beginNever()
169   {
170     try {
171       Transaction JavaDoc oldTrans = _tm.getTransaction();
172
173       if (oldTrans != null)
174     throw new TransactionRuntimeException(L.l("'Never' transaction boundary must not have a transaction."));
175     } catch (RuntimeException JavaDoc e) {
176       throw e;
177     } catch (Exception JavaDoc e) {
178       throw new TransactionRuntimeException(e);
179     }
180   }
181
182   /**
183    * Suspends any active transaction.
184
185    * @return the current transaction context
186    */

187   public Transaction JavaDoc beginSuspend()
188   {
189     try {
190       Transaction JavaDoc oldTrans = _tm.getTransaction();
191
192       if (oldTrans != null)
193     oldTrans = _tm.suspend();
194
195       return oldTrans;
196     } catch (RuntimeException JavaDoc e) {
197       throw e;
198     } catch (Exception JavaDoc e) {
199       throw new TransactionRuntimeException(e);
200     }
201   }
202
203   /**
204    * Sets a rollback-only transaction.
205    */

206   public void setRollbackOnly(Throwable JavaDoc e)
207   {
208   }
209
210   /**
211    * Commits the transaction (rolling back if rollback only)
212    */

213   public void commit(Transaction JavaDoc oldTransaction)
214   {
215     try {
216       Transaction JavaDoc currentTrans = _tm.getTransaction();
217
218       if (currentTrans == null) {
219       }
220       else if (currentTrans.getStatus() != Status.STATUS_MARKED_ROLLBACK)
221     _userTM.commit();
222       else
223     _userTM.rollback();
224     } catch (RuntimeException JavaDoc e) {
225       throw e;
226     } catch (Exception JavaDoc e) {
227       throw new TransactionRuntimeException(e);
228     } finally {
229       if (oldTransaction != null) {
230     try {
231       _tm.resume(oldTransaction);
232     } catch (Exception JavaDoc e) {
233       throw new TransactionRuntimeException(e);
234     }
235       }
236     }
237   }
238
239   /**
240    * Rolls back any existing transaction.
241    */

242   public void rollback(Transaction JavaDoc oldTransaction)
243   {
244     try {
245       Transaction JavaDoc currentTrans = _tm.getTransaction();
246
247       if (currentTrans != null)
248     _userTM.rollback();
249     } catch (RuntimeException JavaDoc e) {
250       throw e;
251     } catch (Exception JavaDoc e) {
252       throw new TransactionRuntimeException(e);
253     } finally {
254       if (oldTransaction != null) {
255     try {
256       _tm.resume(oldTransaction);
257     } catch (Exception JavaDoc e) {
258       throw new TransactionRuntimeException(e);
259     }
260       }
261     }
262   }
263 }
264
Popular Tags