KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > usertx > server > UserTransactionSessionImpl


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tm.usertx.server;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import java.rmi.RemoteException JavaDoc;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import javax.transaction.Transaction JavaDoc;
34 import javax.transaction.TransactionManager JavaDoc;
35 import javax.transaction.Status JavaDoc;
36 import javax.transaction.NotSupportedException JavaDoc;
37 import javax.transaction.SystemException JavaDoc;
38 import javax.transaction.RollbackException JavaDoc;
39 import javax.transaction.HeuristicMixedException JavaDoc;
40 import javax.transaction.HeuristicRollbackException JavaDoc;
41
42 import org.jboss.logging.Logger;
43 import org.jboss.tm.TransactionPropagationContextFactory;
44 import org.jboss.tm.TransactionPropagationContextUtil;
45 import org.jboss.tm.usertx.interfaces.UserTransactionSession;
46 import org.jboss.util.collection.WeakValueHashMap;
47
48 /** A UserTransaction session implementation.
49  * It handles transactions on behalf of a single client.
50  * @author Ole Husgaard
51  * @author Scott.Stark@jboss.org
52  * @version $Revision: 37459 $
53  */

54 public class UserTransactionSessionImpl
55    implements UserTransactionSession
56 {
57    /** Cache a reference to the TM. */
58    private static TransactionManager JavaDoc tm = null;
59    private static Logger log = Logger.getLogger(UserTransactionSessionImpl.class);
60    /**
61     * Maps the TPCs of all active transactions to their transactions.
62     */

63    private static WeakValueHashMap activeTx = new WeakValueHashMap();
64    private static UserTransactionSessionImpl instance = new UserTransactionSessionImpl();
65
66    public static UserTransactionSession getInstance()
67    {
68       return instance;
69    }
70
71    /**
72     * Get a reference to the transaction manager.
73     */

74    protected static TransactionManager JavaDoc getTransactionManager()
75    {
76       if (tm == null)
77       {
78          try
79          {
80             Context JavaDoc ctx = new InitialContext JavaDoc();
81             tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
82          }
83          catch (NamingException JavaDoc ex)
84          {
85             log.error("java:/TransactionManager lookup failed", ex);
86          }
87       }
88       return tm;
89    }
90
91    /** Cache a reference to the TPC Factory. */
92    private static TransactionPropagationContextFactory tpcFactory = null;
93    
94    /**
95     * Get a reference to the TPC Factory
96     */

97    protected static TransactionPropagationContextFactory getTPCFactory()
98    {
99       if (tpcFactory == null)
100       {
101          tpcFactory = TransactionPropagationContextUtil.getTPCFactory();
102       }
103       return tpcFactory;
104    }
105
106    //
107
// implements interface UserTransactionSession
108
//
109

110    /**
111     * Destroy this session.
112     */

113    public void destroy()
114       throws RemoteException JavaDoc
115    {
116       unreferenced();
117    }
118    
119    /**
120     * Start a new transaction, and return its TPC.
121     *
122     * @param timeout The timeout value for the new transaction, in seconds.
123     *
124     * @return The transaction propagation context for the new transaction.
125     */

126    public Object JavaDoc begin(int timeout)
127       throws RemoteException JavaDoc,
128       NotSupportedException JavaDoc,
129       SystemException JavaDoc
130    {
131       TransactionManager JavaDoc tm = getTransactionManager();
132       // Set timeout value
133
tm.setTransactionTimeout(timeout);
134       // Start tx, and get its TPC.
135
tm.begin();
136       Object JavaDoc tpc = getTPCFactory().getTransactionPropagationContext();
137       // Suspend thread association.
138
Transaction JavaDoc tx = tm.suspend();
139       // Remember that a new tx is now active.
140
activeTx.put(tpc, tx);
141       // return the TPC
142
return tpc;
143    }
144    
145    /**
146     * Commit the transaction.
147     *
148     * @param tpc The transaction propagation context for the transaction.
149     */

150    public void commit(Object JavaDoc tpc)
151       throws RemoteException JavaDoc,
152       RollbackException JavaDoc,
153       HeuristicMixedException JavaDoc,
154       HeuristicRollbackException JavaDoc,
155       SecurityException JavaDoc,
156       IllegalStateException JavaDoc,
157       SystemException JavaDoc
158    {
159       Transaction JavaDoc tx = (Transaction JavaDoc)activeTx.get(tpc);
160       
161       if (tx == null)
162          throw new IllegalStateException JavaDoc("No transaction.");
163
164       // Resume thread association
165
TransactionManager JavaDoc tm = getTransactionManager();
166       tm.resume(tx);
167
168       boolean finished = true;
169       
170       try
171       {
172          tm.commit();
173       }
174       catch (java.lang.SecurityException JavaDoc ex)
175       {
176          finished = false;
177          throw ex;
178       }
179       catch (java.lang.IllegalStateException JavaDoc ex)
180       {
181          finished = false;
182          throw ex;
183       }
184       finally
185       {
186          activeTx.remove(tpc);
187       }
188    }
189    
190    /**
191     * Rollback the transaction.
192     *
193     * @param tpc The transaction propagation context for the transaction.
194     */

195    public void rollback(Object JavaDoc tpc)
196       throws RemoteException JavaDoc,
197       SecurityException JavaDoc,
198       IllegalStateException JavaDoc,
199       SystemException JavaDoc
200    {
201       Transaction JavaDoc tx = (Transaction JavaDoc)activeTx.get(tpc);
202       
203       if (tx == null)
204          throw new IllegalStateException JavaDoc("No transaction.");
205       
206       // Resume thread association
207
TransactionManager JavaDoc tm = getTransactionManager();
208       tm.resume(tx);
209
210       tm.rollback();
211       activeTx.remove(tpc);
212    }
213    
214    /**
215     * Mark the transaction for rollback only.
216     *
217     * @param tpc The transaction propagation context for the transaction.
218     */

219    public void setRollbackOnly(Object JavaDoc tpc)
220       throws RemoteException JavaDoc,
221       IllegalStateException JavaDoc,
222       SystemException JavaDoc
223    {
224       Transaction JavaDoc tx = (Transaction JavaDoc)activeTx.get(tpc);
225       
226       if (tx == null)
227          throw new IllegalStateException JavaDoc("No transaction.");
228       
229       tx.setRollbackOnly();
230    }
231    
232    /**
233     * Return status of the transaction.
234     *
235     * @param tpc The transaction propagation context for the transaction.
236     */

237    public int getStatus(Object JavaDoc tpc)
238       throws RemoteException JavaDoc,
239       SystemException JavaDoc
240    {
241       Transaction JavaDoc tx = (Transaction JavaDoc)activeTx.get(tpc);
242       
243       if (tx == null)
244          return Status.STATUS_NO_TRANSACTION;
245       
246       return tx.getStatus();
247    }
248    
249    
250    //
251
// implements interface Unreferenced
252
//
253

254    /**
255     * When no longer referenced, be sure to rollback any
256     * transactions that are still active.
257     */

258    public void unreferenced()
259    {
260       log.debug("Lost connection to UserTransaction client.");
261       
262       if (!activeTx.isEmpty())
263       {
264          log.error("Lost connection to UserTransaction clients: " +
265          "Rolling back " + activeTx.size() +
266          " active transaction(s).");
267          Collection JavaDoc txs = activeTx.values();
268          Iterator JavaDoc iter = txs.iterator();
269          while (iter.hasNext())
270          {
271             Transaction JavaDoc tx = (Transaction JavaDoc)iter.next();
272             try
273             {
274                tx.rollback();
275             }
276             catch (Exception JavaDoc ex)
277             {
278                log.error("rollback failed", ex);
279             }
280          }
281       }
282    }
283    
284 }
285
Popular Tags