KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > transaction > SlideTransactionManager


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/transaction/SlideTransactionManager.java,v 1.14 2004/07/28 09:34:33 ib Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/07/28 09:34:33 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.transaction;
25
26 import java.util.Hashtable JavaDoc;
27
28 import javax.transaction.HeuristicMixedException JavaDoc;
29 import javax.transaction.HeuristicRollbackException JavaDoc;
30 import javax.transaction.InvalidTransactionException JavaDoc;
31 import javax.transaction.NotSupportedException JavaDoc;
32 import javax.transaction.RollbackException JavaDoc;
33 import javax.transaction.Status JavaDoc;
34 import javax.transaction.SystemException JavaDoc;
35 import javax.transaction.Transaction JavaDoc;
36 import javax.transaction.TransactionManager JavaDoc;
37
38 import org.apache.slide.util.Messages;
39 import org.apache.slide.util.logger.Logger;
40 import org.apache.slide.util.logger.SimpleLogger;
41
42 /**
43  * JTA Transaction manager implementation.
44  * <p>
45  * Implementation notes :
46  * <ul>
47  * <li>Does not support nested transactions</li>
48  * <li>No security</li>
49  * </ul>
50  *
51  * @version $Revision: 1.14 $
52  */

53 public final class SlideTransactionManager implements TransactionManager JavaDoc {
54
55
56     // -------------------------------------------------------------- Constants
57

58
59     protected static final String JavaDoc LOG_CHANNEL =
60         SlideTransactionManager.class.getName();
61
62
63     public static final int DEFAULT_TRANSACTION_TIMEOUT = 30;
64
65
66     // ------------------------------------------------------------ Constructor
67

68
69     // ----------------------------------------------------- Instance Variables
70

71
72     /**
73      * Transaction bindings thread id <-> transaction object.
74      */

75     private Hashtable JavaDoc bindings = new Hashtable JavaDoc();
76
77
78     /**
79      * Transaction bindings thread id <-> transaction timeout.
80      */

81     private Hashtable JavaDoc timeouts = new Hashtable JavaDoc();
82
83
84     /**
85      * Associated logger.
86      */

87     private Logger logger = new SimpleLogger();
88
89
90     // ------------------------------------------------------------- Properties
91

92
93     // --------------------------------------------------------- Public Methods
94

95
96     /**
97      * Get the logger associated with the transaction manager.
98      */

99     public Logger getLogger() {
100         return logger;
101     }
102
103
104     /**
105      * Set the logger of the transaction manager.
106      */

107     public void setLogger(Logger logger) {
108         this.logger = logger;
109     }
110
111
112     // --------------------------------------------- TransactionManager Methods
113

114
115     /**
116      * Create a new transaction and associate it with the current thread.
117      *
118      * @exception NotSupportedException Thrown if the thread is already
119      * associated with a transaction and the Transaction Manager
120      * implementation does not support nested transactions.
121      * @exception SystemException Thrown if the transaction manager encounters
122      * an unexpected error condition.
123      */

124     public void begin()
125         throws NotSupportedException JavaDoc, SystemException JavaDoc {
126
127         Transaction JavaDoc currentTransaction = getTransaction();
128         if (currentTransaction != null)
129             throw new NotSupportedException JavaDoc();
130
131         currentTransaction = new SlideTransaction(this);
132         bindings.put(Thread.currentThread(), currentTransaction);
133
134         if (logger.isEnabled(LOG_CHANNEL, Logger.DEBUG)) {
135             String JavaDoc logMessage = Messages.format
136                 (SlideTransactionManager.class.getName() + ".begin",
137                  currentTransaction.toString());
138             logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG);
139         }
140
141     }
142
143
144     /**
145      * Complete the transaction associated with the current thread. When this
146      * method completes, the thread becomes associated with no transaction.
147      * If the commit is terminated with an exception, the rollback should be
148      * called, to do a proper clean-up.
149      *
150      * @exception RollbackException Thrown to indicate that the transaction
151      * has been rolled back rather than committed.
152      * @exception HeuristicMixedException Thrown to indicate that a heuristic
153      * decision was made and that some relevant updates have been committed
154      * while others have been rolled back.
155      * @exception HeuristicRollbackException Thrown to indicate that a
156      * heuristic decision was made and that some relevant updates have been
157      * rolled back.
158      * @exception SecurityException Thrown to indicate that the thread is not
159      * allowed to commit the transaction.
160      * @exception IllegalStateException Thrown if the current thread is not
161      * associated with a transaction.
162      * @exception SystemException Thrown if the transaction manager encounters
163      * an unexpected error condition.
164      */

165     public void commit()
166         throws RollbackException JavaDoc, HeuristicMixedException JavaDoc,
167         HeuristicRollbackException JavaDoc, SecurityException JavaDoc, IllegalStateException JavaDoc,
168         SystemException JavaDoc {
169
170         Thread JavaDoc currentThread = Thread.currentThread();
171         Transaction JavaDoc currentTransaction =
172             (Transaction JavaDoc) bindings.get(currentThread);
173         if (currentTransaction == null)
174             throw new IllegalStateException JavaDoc();
175
176         timeouts.remove(currentThread);
177
178         if (logger.isEnabled(LOG_CHANNEL, Logger.DEBUG)) {
179             String JavaDoc logMessage = Messages.format
180                 (SlideTransactionManager.class.getName() + ".commit",
181                  currentTransaction.toString());
182             logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG);
183         }
184
185         try {
186             currentTransaction.commit();
187         } finally {
188             bindings.remove(currentThread);
189         }
190
191     }
192
193
194     /**
195      * Roll back the transaction associated with the current thread. When
196      * this method completes, the thread becomes associated with no
197      * transaction.
198      *
199      * @exception SecurityException Thrown to indicate that the thread is not
200      * allowed to commit the transaction.
201      * @exception IllegalStateException Thrown if the current thread is not
202      * associated with a transaction.
203      * @exception SystemException Thrown if the transaction manager encounters
204      * an unexpected error condition.
205      */

206     public void rollback()
207         throws SecurityException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc {
208
209         Thread JavaDoc currentThread = Thread.currentThread();
210         Transaction JavaDoc currentTransaction =
211             (Transaction JavaDoc) bindings.remove(currentThread);
212         if (currentTransaction == null)
213             throw new IllegalStateException JavaDoc();
214
215         timeouts.remove(currentThread);
216
217         String JavaDoc logMessage = Messages.format
218             (SlideTransactionManager.class.getName() + ".rollback",
219              currentTransaction.toString());
220         logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG);
221
222         currentTransaction.rollback();
223
224     }
225
226
227     /**
228      * Modify the transaction associated with the current thread such that
229      * the only possible outcome of the transaction is to roll back the
230      * transaction.
231      *
232      * @exception IllegalStateException Thrown if the current thread is not
233      * associated with a transaction.
234      * @exception SystemException Thrown if the transaction manager encounters
235      * an unexpected error condition.
236      */

237     public void setRollbackOnly()
238         throws IllegalStateException JavaDoc, SystemException JavaDoc {
239
240         Transaction JavaDoc currentTransaction = getTransaction();
241         if (currentTransaction == null)
242             throw new IllegalStateException JavaDoc();
243
244         String JavaDoc logMessage = Messages.format
245             (SlideTransactionManager.class.getName() + ".rollbackOnly",
246              currentTransaction.toString());
247         logger.log(logMessage, LOG_CHANNEL, Logger.INFO);
248
249         currentTransaction.setRollbackOnly();
250
251     }
252
253
254     /**
255      * Obtain the status of the transaction associated with the current thread.
256      *
257      * @exception SystemException Thrown if the transaction manager encounters
258      * an unexpected error condition.
259      * @return The transaction status. If no transaction is associated with
260      * the current thread, this method returns the Status.NoTransaction value.
261      */

262     public int getStatus()
263         throws SystemException JavaDoc {
264
265         Transaction JavaDoc currentTransaction = getTransaction();
266         if (currentTransaction == null)
267             return Status.STATUS_NO_TRANSACTION;
268
269         return currentTransaction.getStatus();
270
271     }
272
273
274     /**
275      * Get the transaction object that represents the transaction context of
276      * the calling thread.
277      *
278      * @return the Transaction object representing the transaction associated
279      * with the calling thread.
280      * @exception SystemException Thrown if the transaction manager encounters
281      * an unexpected error condition.
282      */

283     public Transaction JavaDoc getTransaction()
284         throws SystemException JavaDoc {
285         return (Transaction JavaDoc) bindings.get(Thread.currentThread());
286     }
287
288
289     /**
290      * Resume the transaction context association of the calling thread with
291      * the transaction represented by the supplied Transaction object. When
292      * this method returns, the calling thread is associated with the
293      * transaction context specified.
294      *
295      * @param tobj The Transaction object that represents the transaction to
296      * be resumed.
297      * @exception InvalidTransactionException Thrown if the parameter
298      * transaction object contains an invalid transaction.
299      * @exception IllegalStateException Thrown if the thread is already
300      * associated with another transaction.
301      * @exception SystemException Thrown if the transaction manager encounters
302      * an unexpected error condition.
303      */

304     public void resume(Transaction JavaDoc tobj)
305         throws InvalidTransactionException JavaDoc, IllegalStateException JavaDoc,
306         SystemException JavaDoc {
307
308         if (getTransaction() != null)
309             throw new IllegalStateException JavaDoc();
310
311         if (tobj == null)
312             throw new InvalidTransactionException JavaDoc();
313
314         bindings.put(Thread.currentThread(), tobj);
315
316     }
317
318
319     /**
320      * Suspend the transaction currently associated with the calling thread
321      * and return a Transaction object that represents the transaction
322      * context being suspended. If the calling thread is not associated with
323      * a transaction, the method returns a null object reference. When this
324      * method returns, the calling thread is associated with no transaction.
325      *
326      * @return Transaction object representing the suspended transaction.
327      * @exception SystemException Thrown if the transaction manager encounters
328      * an unexpected error condition.
329      */

330     public Transaction JavaDoc suspend()
331         throws SystemException JavaDoc {
332
333         Transaction JavaDoc currentTransaction = getTransaction();
334
335         if (currentTransaction != null) {
336             Thread JavaDoc currentThread = Thread.currentThread();
337             bindings.remove(currentThread);
338             timeouts.remove(currentThread);
339         }
340
341         return currentTransaction;
342
343     }
344
345
346     /**
347      * Modify the value of the timeout value that is associated with the
348      * transactions started by the current thread with the begin method.
349      * <p>
350      * If an application has not called this method, the transaction service
351      * uses some default value for the transaction timeout.
352      *
353      * @param seconds The value of the timeout in seconds. If the value is
354      * zero, the transaction service restores the default value.
355      * @exception SystemException Thrown if the transaction manager encounters
356      * an unexpected error condition.
357      */

358     public void setTransactionTimeout(int seconds)
359         throws SystemException JavaDoc {
360
361         timeouts.put(Thread.currentThread(), new Integer JavaDoc(seconds));
362
363     }
364
365
366 }
367
Popular Tags