KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > transaction > DummyBaseTransactionManager


1 package org.jboss.cache.transaction;
2
3 import javax.transaction.HeuristicMixedException JavaDoc;
4 import javax.transaction.HeuristicRollbackException JavaDoc;
5 import javax.transaction.InvalidTransactionException JavaDoc;
6 import javax.transaction.NotSupportedException JavaDoc;
7 import javax.transaction.RollbackException JavaDoc;
8 import javax.transaction.Status JavaDoc;
9 import javax.transaction.SystemException JavaDoc;
10 import javax.transaction.Transaction JavaDoc;
11 import javax.transaction.TransactionManager JavaDoc;
12
13 /**
14  * @author bela
15  * @version $Revision: 1.4 $
16  * Date: May 15, 2003
17  * Time: 4:11:37 PM
18  */

19 public class DummyBaseTransactionManager implements TransactionManager JavaDoc, java.io.Serializable JavaDoc
20 {
21    static ThreadLocal JavaDoc<Transaction JavaDoc> thread_local = new ThreadLocal JavaDoc<Transaction JavaDoc>();
22    private static final long serialVersionUID = -6716097342564237376l;
23
24    /**
25     * Starts a new transaction, and associate it with the calling thread.
26     *
27     * @throws javax.transaction.NotSupportedException
28     * If the calling thread is already
29     * associated with a transaction, and nested transactions are
30     * not supported.
31     * @throws javax.transaction.SystemException
32     * If the transaction service fails in an
33     * unexpected way.
34     */

35    public void begin() throws NotSupportedException JavaDoc, SystemException JavaDoc
36    {
37       Transaction JavaDoc currentTx;
38       if ((currentTx = getTransaction()) != null)
39          throw new NotSupportedException JavaDoc(Thread.currentThread() +
40                  " is already associated with a transaction (" + currentTx + ")");
41       DummyTransaction tx = new DummyTransaction(this);
42       setTransaction(tx);
43    }
44
45    /**
46     * Commit the transaction associated with the calling thread.
47     *
48     * @throws javax.transaction.RollbackException
49     * If the transaction was marked for rollback
50     * only, the transaction is rolled back and this exception is
51     * thrown.
52     * @throws IllegalStateException If the calling thread is not associated
53     * with a transaction.
54     * @throws javax.transaction.SystemException
55     * If the transaction service fails in an
56     * unexpected way.
57     * @throws javax.transaction.HeuristicMixedException
58     * If a heuristic decision was made and
59     * some some parts of the transaction have been committed while
60     * other parts have been rolled back.
61     * @throws javax.transaction.HeuristicRollbackException
62     * If a heuristic decision to roll
63     * back the transaction was made.
64     * @throws SecurityException If the caller is not allowed to commit this
65     * transaction.
66     */

67    public void commit() throws RollbackException JavaDoc, HeuristicMixedException JavaDoc,
68            HeuristicRollbackException JavaDoc, SecurityException JavaDoc,
69            IllegalStateException JavaDoc, SystemException JavaDoc
70    {
71       int status;
72       Transaction JavaDoc tx = getTransaction();
73       if (tx == null)
74          throw new IllegalStateException JavaDoc("thread not associated with transaction");
75       status = tx.getStatus();
76       if (status == Status.STATUS_MARKED_ROLLBACK)
77          throw new RollbackException JavaDoc();
78       tx.commit();
79
80       // Disassociate tx from thread.
81
setTransaction(null);
82    }
83
84    /**
85     * Rolls back the transaction associated with the calling thread.
86     *
87     * @throws IllegalStateException If the transaction is in a state
88     * where it cannot be rolled back. This could be because the
89     * calling thread is not associated with a transaction, or
90     * because it is in the
91     * {@link javax.transaction.Status#STATUS_PREPARED prepared state}.
92     * @throws SecurityException If the caller is not allowed to roll back
93     * this transaction.
94     * @throws javax.transaction.SystemException
95     * If the transaction service fails in an
96     * unexpected way.
97     */

98    public void rollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc,
99            SystemException JavaDoc
100    {
101       Transaction JavaDoc tx = getTransaction();
102       if (tx == null)
103          throw new IllegalStateException JavaDoc("no transaction associated with thread");
104       tx.rollback();
105
106       // Disassociate tx from thread.
107
setTransaction(null);
108    }
109
110    /**
111     * Mark the transaction associated with the calling thread for rollback
112     * only.
113     *
114     * @throws IllegalStateException If the transaction is in a state
115     * where it cannot be rolled back. This could be because the
116     * calling thread is not associated with a transaction, or
117     * because it is in the
118     * {@link javax.transaction.Status#STATUS_PREPARED prepared state}.
119     * @throws javax.transaction.SystemException
120     * If the transaction service fails in an
121     * unexpected way.
122     */

123    public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc
124    {
125       Transaction JavaDoc tx = getTransaction();
126       if (tx == null)
127          throw new IllegalStateException JavaDoc("no transaction associated with calling thread");
128       tx.setRollbackOnly();
129    }
130
131    /**
132     * Get the status of the transaction associated with the calling thread.
133     *
134     * @return The status of the transaction. This is one of the
135     * {@link javax.transaction.Status} constants. If no transaction is associated
136     * with the calling thread,
137     * {@link javax.transaction.Status#STATUS_NO_TRANSACTION} is returned.
138     * @throws javax.transaction.SystemException
139     * If the transaction service fails in an
140     * unexpected way.
141     */

142    public int getStatus() throws SystemException JavaDoc
143    {
144       Transaction JavaDoc tx = getTransaction();
145       return tx != null ? tx.getStatus() : Status.STATUS_NO_TRANSACTION;
146    }
147
148    /**
149     * Get the transaction associated with the calling thread.
150     *
151     * @return The transaction associated with the calling thread, or
152     * <code>null</code> if the calling thread is not associated
153     * with a transaction.
154     * @throws javax.transaction.SystemException
155     * If the transaction service fails in an
156     * unexpected way.
157     */

158    public Transaction JavaDoc getTransaction() throws SystemException JavaDoc
159    {
160       return thread_local.get();
161    }
162
163    /**
164     * Change the transaction timeout for transactions started by the calling
165     * thread with the {@link #begin()} method.
166     *
167     * @param seconds The new timeout value, in seconds. If this parameter
168     * is <code>0</code>, the timeout value is reset to the default
169     * value.
170     * @throws javax.transaction.SystemException
171     * If the transaction service fails in an
172     * unexpected way.
173     */

174    public void setTransactionTimeout(int seconds) throws SystemException JavaDoc
175    {
176       throw new SystemException JavaDoc("not supported");
177    }
178
179    /**
180     * Suspend the association the calling thread has to a transaction,
181     * and return the suspended transaction.
182     * When returning from this method, the calling thread is no longer
183     * associated with a transaction.
184     *
185     * @return The transaction that the calling thread was associated with,
186     * or <code>null</code> if the calling thread was not associated
187     * with a transaction.
188     * @throws javax.transaction.SystemException
189     * If the transaction service fails in an
190     * unexpected way.
191     */

192    public Transaction JavaDoc suspend() throws SystemException JavaDoc
193    {
194       Transaction JavaDoc retval = getTransaction();
195       setTransaction(null);
196       return retval;
197    }
198
199    /**
200     * Resume the association of the calling thread with the given
201     * transaction.
202     *
203     * @param tx The transaction to be associated with the calling thread.
204     * @throws javax.transaction.InvalidTransactionException
205     * If the argument does not represent
206     * a valid transaction.
207     * @throws IllegalStateException If the calling thread is already
208     * associated with a transaction.
209     * @throws javax.transaction.SystemException
210     * If the transaction service fails in an
211     * unexpected way.
212     */

213    public void resume(Transaction JavaDoc tx) throws InvalidTransactionException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc
214    {
215       setTransaction(tx);
216    }
217
218    /**
219     * Just used for unit tests
220     *
221     * @param tx
222     */

223    public void setTransaction(Transaction JavaDoc tx)
224    {
225       thread_local.set(tx);
226    }
227
228 }
229
Popular Tags