KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.transaction;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import javax.transaction.HeuristicMixedException JavaDoc;
7 import javax.transaction.HeuristicRollbackException JavaDoc;
8 import javax.transaction.RollbackException JavaDoc;
9 import javax.transaction.Status JavaDoc;
10 import javax.transaction.Synchronization JavaDoc;
11 import javax.transaction.SystemException JavaDoc;
12 import javax.transaction.Transaction JavaDoc;
13 import javax.transaction.xa.XAResource JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.concurrent.CopyOnWriteArraySet JavaDoc;
16
17 /**
18  * @author bela
19  * @version $Revision: 1.10 $
20  * Date: May 15, 2003
21  * Time: 4:20:17 PM
22  */

23 public class DummyTransaction implements Transaction JavaDoc
24 {
25    int status = Status.STATUS_UNKNOWN;
26
27    static final Log log = LogFactory.getLog(DummyTransaction.class);
28
29    DummyBaseTransactionManager tm_;
30
31    final Set JavaDoc<Synchronization JavaDoc> participants = new CopyOnWriteArraySet JavaDoc<Synchronization JavaDoc>();
32
33    public DummyTransaction(DummyBaseTransactionManager tm)
34    {
35       tm_ = tm;
36       status = Status.STATUS_ACTIVE;
37    }
38
39    /**
40     * Attempt to commit this transaction.
41     *
42     * @throws RollbackException If the transaction was marked for rollback
43     * only, the transaction is rolled back and this exception is
44     * thrown.
45     * @throws SystemException If the transaction service fails in an
46     * unexpected way.
47     * @throws HeuristicMixedException If a heuristic decision was made and
48     * some some parts of the transaction have been committed while
49     * other parts have been rolled back.
50     * @throws HeuristicRollbackException If a heuristic decision to roll
51     * back the transaction was made.
52     * @throws SecurityException If the caller is not allowed to commit this
53     * transaction.
54     */

55    public void commit()
56            throws RollbackException JavaDoc, HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc,
57            SecurityException JavaDoc, SystemException JavaDoc
58    {
59       boolean doCommit;
60       status = Status.STATUS_PREPARING;
61       try
62       {
63          boolean outcome = notifyBeforeCompletion();
64          // status=Status.STATUS_PREPARED;
65
if (outcome == true && status != Status.STATUS_MARKED_ROLLBACK)
66          {
67             status = Status.STATUS_COMMITTING;
68             doCommit = true;
69          }
70          else
71          {
72             status = Status.STATUS_ROLLING_BACK;
73             doCommit = false;
74          }
75          notifyAfterCompletion(doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK);
76          status = doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK;
77          if (doCommit == false)
78             throw new RollbackException JavaDoc("outcome is " + outcome + " status: " + status);
79       }
80       finally
81       {
82          // Disassociate tx from thread.
83
tm_.setTransaction(null);
84       }
85    }
86
87    /**
88     * Rolls back this transaction.
89     *
90     * @throws IllegalStateException If the transaction is in a state
91     * where it cannot be rolled back. This could be because the
92     * transaction is no longer active, or because it is in the
93     * {@link Status#STATUS_PREPARED prepared state}.
94     * @throws SystemException If the transaction service fails in an
95     * unexpected way.
96     */

97    public void rollback() throws IllegalStateException JavaDoc, SystemException JavaDoc
98    {
99       try
100       {
101          // JBCACHE-360 -- to match JBossTM (and presumable the spec) a
102
// rollback transaction should have status ROLLEDBACK before
103
// calling afterCompletion().
104
//status=Status.STATUS_ROLLING_BACK;
105
status = Status.STATUS_ROLLEDBACK;
106          notifyAfterCompletion(Status.STATUS_ROLLEDBACK);
107       }
108       catch (Throwable JavaDoc t)
109       {
110       }
111       status = Status.STATUS_ROLLEDBACK;
112
113       // Disassociate tx from thread.
114
tm_.setTransaction(null);
115    }
116
117    /**
118     * Mark the transaction so that the only possible outcome is a rollback.
119     *
120     * @throws IllegalStateException If the transaction is not in an active
121     * state.
122     * @throws SystemException If the transaction service fails in an
123     * unexpected way.
124     */

125    public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc
126    {
127       status = Status.STATUS_MARKED_ROLLBACK;
128    }
129
130    /**
131     * Get the status of the transaction.
132     *
133     * @return The status of the transaction. This is one of the
134     * {@link Status} constants.
135     * @throws SystemException If the transaction service fails in an
136     * unexpected way.
137     */

138    public int getStatus() throws SystemException JavaDoc
139    {
140       return status;
141    }
142
143    /**
144     * Change the transaction timeout for transactions started by the calling
145     * thread with the {@link DummyTransactionManager#begin()} method.
146     *
147     * @param seconds The new timeout value, in seconds. If this parameter
148     * is <code>0</code>, the timeout value is reset to the default
149     * value.
150     * @throws SystemException If the transaction service fails in an
151     * unexpected way.
152     */

153    public void setTransactionTimeout(int seconds) throws SystemException JavaDoc
154    {
155       throw new SystemException JavaDoc("not supported");
156    }
157
158    /**
159     * Enlist an XA resource with this transaction.
160     *
161     * @return <code>true</code> if the resource could be enlisted with
162     * this transaction, otherwise <code>false</code>.
163     * @throws RollbackException If the transaction is marked for rollback
164     * only.
165     * @throws IllegalStateException If the transaction is in a state
166     * where resources cannot be enlisted. This could be because the
167     * transaction is no longer active, or because it is in the
168     * {@link Status#STATUS_PREPARED prepared state}.
169     * @throws SystemException If the transaction service fails in an
170     * unexpected way.
171     */

172    public boolean enlistResource(XAResource JavaDoc xaRes)
173            throws RollbackException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc
174    {
175       throw new SystemException JavaDoc("not supported");
176    }
177
178    /**
179     * Delist an XA resource from this transaction.
180     *
181     * @return <code>true</code> if the resource could be delisted from
182     * this transaction, otherwise <code>false</code>.
183     * @throws IllegalStateException If the transaction is in a state
184     * where resources cannot be delisted. This could be because the
185     * transaction is no longer active.
186     * @throws SystemException If the transaction service fails in an
187     * unexpected way.
188     */

189    public boolean delistResource(XAResource JavaDoc xaRes, int flag)
190            throws IllegalStateException JavaDoc, SystemException JavaDoc
191    {
192       throw new SystemException JavaDoc("not supported");
193    }
194
195    /**
196     * Register a {@link Synchronization} callback with this transaction.
197     *
198     * @throws RollbackException If the transaction is marked for rollback
199     * only.
200     * @throws IllegalStateException If the transaction is in a state
201     * where {@link Synchronization} callbacks cannot be registered.
202     * This could be because the transaction is no longer active,
203     * or because it is in the
204     * {@link Status#STATUS_PREPARED prepared state}.
205     * @throws SystemException If the transaction service fails in an
206     * unexpected way.
207     */

208    public void registerSynchronization(Synchronization JavaDoc sync)
209            throws RollbackException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc
210    {
211       if (sync == null)
212          throw new IllegalArgumentException JavaDoc("null synchronization " + this);
213
214       switch (status)
215       {
216          case Status.STATUS_ACTIVE:
217          case Status.STATUS_PREPARING:
218             break;
219          case Status.STATUS_PREPARED:
220             throw new IllegalStateException JavaDoc("already prepared. " + this);
221          case Status.STATUS_COMMITTING:
222             throw new IllegalStateException JavaDoc("already started committing. " + this);
223          case Status.STATUS_COMMITTED:
224             throw new IllegalStateException JavaDoc("already committed. " + this);
225          case Status.STATUS_MARKED_ROLLBACK:
226             throw new RollbackException JavaDoc("already marked for rollback " + this);
227          case Status.STATUS_ROLLING_BACK:
228             throw new RollbackException JavaDoc("already started rolling back. " + this);
229          case Status.STATUS_ROLLEDBACK:
230             throw new RollbackException JavaDoc("already rolled back. " + this);
231          case Status.STATUS_NO_TRANSACTION:
232             throw new IllegalStateException JavaDoc("no transaction. " + this);
233          case Status.STATUS_UNKNOWN:
234             throw new IllegalStateException JavaDoc("unknown state " + this);
235          default:
236             throw new IllegalStateException JavaDoc("illegal status: " + status + " tx=" + this);
237       }
238
239       if (log.isDebugEnabled())
240       {
241          log.debug("registering synchronization handler " + sync);
242       }
243       participants.add(sync);
244
245    }
246
247    void setStatus(int new_status)
248    {
249       status = new_status;
250    }
251
252    boolean notifyBeforeCompletion()
253    {
254       boolean retval = true;
255
256       for (Synchronization JavaDoc s : participants)
257       {
258          if (log.isDebugEnabled())
259          {
260             log.debug("processing beforeCompletion for " + s);
261          }
262          try
263          {
264             s.beforeCompletion();
265          }
266          catch (Throwable JavaDoc t)
267          {
268             retval = false;
269             log.error("beforeCompletion() failed for " + s, t);
270          }
271       }
272       return retval;
273    }
274
275    void notifyAfterCompletion(int status)
276    {
277       for (Synchronization JavaDoc s : participants)
278       {
279          if (log.isDebugEnabled())
280          {
281             log.debug("processing afterCompletion for " + s);
282          }
283          try
284          {
285             s.afterCompletion(status);
286          }
287          catch (Throwable JavaDoc t)
288          {
289             log.error("afterCompletion() failed for " + s, t);
290          }
291       }
292       participants.clear();
293    }
294
295 }
296
Popular Tags