1 22 package org.jboss.tm.usertx.server; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 import java.rmi.RemoteException ; 28 29 import javax.naming.InitialContext ; 30 import javax.naming.Context ; 31 import javax.naming.NamingException ; 32 33 import javax.transaction.Transaction ; 34 import javax.transaction.TransactionManager ; 35 import javax.transaction.Status ; 36 import javax.transaction.NotSupportedException ; 37 import javax.transaction.SystemException ; 38 import javax.transaction.RollbackException ; 39 import javax.transaction.HeuristicMixedException ; 40 import javax.transaction.HeuristicRollbackException ; 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 54 public class UserTransactionSessionImpl 55 implements UserTransactionSession 56 { 57 58 private static TransactionManager tm = null; 59 private static Logger log = Logger.getLogger(UserTransactionSessionImpl.class); 60 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 74 protected static TransactionManager getTransactionManager() 75 { 76 if (tm == null) 77 { 78 try 79 { 80 Context ctx = new InitialContext (); 81 tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 82 } 83 catch (NamingException ex) 84 { 85 log.error("java:/TransactionManager lookup failed", ex); 86 } 87 } 88 return tm; 89 } 90 91 92 private static TransactionPropagationContextFactory tpcFactory = null; 93 94 97 protected static TransactionPropagationContextFactory getTPCFactory() 98 { 99 if (tpcFactory == null) 100 { 101 tpcFactory = TransactionPropagationContextUtil.getTPCFactory(); 102 } 103 return tpcFactory; 104 } 105 106 110 113 public void destroy() 114 throws RemoteException 115 { 116 unreferenced(); 117 } 118 119 126 public Object begin(int timeout) 127 throws RemoteException , 128 NotSupportedException , 129 SystemException 130 { 131 TransactionManager tm = getTransactionManager(); 132 tm.setTransactionTimeout(timeout); 134 tm.begin(); 136 Object tpc = getTPCFactory().getTransactionPropagationContext(); 137 Transaction tx = tm.suspend(); 139 activeTx.put(tpc, tx); 141 return tpc; 143 } 144 145 150 public void commit(Object tpc) 151 throws RemoteException , 152 RollbackException , 153 HeuristicMixedException , 154 HeuristicRollbackException , 155 SecurityException , 156 IllegalStateException , 157 SystemException 158 { 159 Transaction tx = (Transaction )activeTx.get(tpc); 160 161 if (tx == null) 162 throw new IllegalStateException ("No transaction."); 163 164 TransactionManager tm = getTransactionManager(); 166 tm.resume(tx); 167 168 boolean finished = true; 169 170 try 171 { 172 tm.commit(); 173 } 174 catch (java.lang.SecurityException ex) 175 { 176 finished = false; 177 throw ex; 178 } 179 catch (java.lang.IllegalStateException ex) 180 { 181 finished = false; 182 throw ex; 183 } 184 finally 185 { 186 activeTx.remove(tpc); 187 } 188 } 189 190 195 public void rollback(Object tpc) 196 throws RemoteException , 197 SecurityException , 198 IllegalStateException , 199 SystemException 200 { 201 Transaction tx = (Transaction )activeTx.get(tpc); 202 203 if (tx == null) 204 throw new IllegalStateException ("No transaction."); 205 206 TransactionManager tm = getTransactionManager(); 208 tm.resume(tx); 209 210 tm.rollback(); 211 activeTx.remove(tpc); 212 } 213 214 219 public void setRollbackOnly(Object tpc) 220 throws RemoteException , 221 IllegalStateException , 222 SystemException 223 { 224 Transaction tx = (Transaction )activeTx.get(tpc); 225 226 if (tx == null) 227 throw new IllegalStateException ("No transaction."); 228 229 tx.setRollbackOnly(); 230 } 231 232 237 public int getStatus(Object tpc) 238 throws RemoteException , 239 SystemException 240 { 241 Transaction tx = (Transaction )activeTx.get(tpc); 242 243 if (tx == null) 244 return Status.STATUS_NO_TRANSACTION; 245 246 return tx.getStatus(); 247 } 248 249 250 254 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 txs = activeTx.values(); 268 Iterator iter = txs.iterator(); 269 while (iter.hasNext()) 270 { 271 Transaction tx = (Transaction )iter.next(); 272 try 273 { 274 tx.rollback(); 275 } 276 catch (Exception ex) 277 { 278 log.error("rollback failed", ex); 279 } 280 } 281 } 282 } 283 284 } 285 | Popular Tags |