1 22 package org.jboss.tm.usertx.client; 23 24 import java.io.Serializable ; 25 26 import java.rmi.RemoteException ; 27 28 import java.util.LinkedList ; 29 import java.util.Hashtable ; 30 31 import javax.naming.InitialContext ; 32 import javax.naming.Reference ; 33 import javax.naming.Referenceable ; 34 import javax.naming.NamingException ; 35 36 import javax.transaction.UserTransaction ; 37 import javax.transaction.Transaction ; 38 import javax.transaction.Status ; 39 import javax.transaction.NotSupportedException ; 40 import javax.transaction.SystemException ; 41 import javax.transaction.RollbackException ; 42 import javax.transaction.HeuristicMixedException ; 43 import javax.transaction.HeuristicRollbackException ; 44 45 import org.jboss.tm.TransactionPropagationContextFactory; 46 47 import org.jboss.tm.usertx.interfaces.UserTransactionSession; 48 import org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory; 49 import org.jboss.naming.NamingContextFactory; 50 51 65 public class ClientUserTransaction 66 implements UserTransaction , 67 TransactionPropagationContextFactory, 68 Referenceable , 69 Serializable 70 { 71 73 private static final long serialVersionUID = 1747989355209242872L; 74 75 78 private static ClientUserTransaction singleton = null; 79 80 83 public static ClientUserTransaction getSingleton() 84 { 85 if (singleton == null) 86 singleton = new ClientUserTransaction(); 87 return singleton; 88 } 89 90 91 93 96 private ClientUserTransaction() 97 { 98 } 99 100 102 106 public void begin() 107 throws NotSupportedException , SystemException 108 { 109 ThreadInfo info = getThreadInfo(); 110 111 try 112 { 113 Object tpc = getSession().begin(info.getTimeout()); 114 info.push(tpc); 115 } 116 catch (SystemException e) 117 { 118 throw e; 119 } 120 catch (RemoteException e) 121 { 122 destroySession(); 124 throw new SystemException (e.toString()); 125 } 126 catch (Exception e) 127 { 128 throw new SystemException (e.toString()); 129 } 130 } 131 132 public void commit() 133 throws RollbackException , 134 HeuristicMixedException , 135 HeuristicRollbackException , 136 SecurityException , 137 IllegalStateException , 138 SystemException 139 { 140 ThreadInfo info = getThreadInfo(); 141 142 try 143 { 144 getSession().commit(info.getTpc()); 145 info.pop(); 146 } 147 catch (RollbackException e) 148 { 149 info.pop(); 150 throw e; 151 } 152 catch (HeuristicMixedException e) 153 { 154 throw e; 155 } 156 catch (HeuristicRollbackException e) 157 { 158 throw e; 159 } 160 catch (SecurityException e) 161 { 162 throw e; 163 } 164 catch (SystemException e) 165 { 166 throw e; 167 } 168 catch (IllegalStateException e) 169 { 170 throw e; 171 } 172 catch (RemoteException e) 173 { 174 destroySession(); 176 throw new SystemException (e.toString()); 177 } 178 catch (Exception e) 179 { 180 throw new SystemException (e.toString()); 181 } 182 } 183 184 public void rollback() 185 throws SecurityException , 186 IllegalStateException , 187 SystemException 188 { 189 ThreadInfo info = getThreadInfo(); 190 191 try 192 { 193 getSession().rollback(info.getTpc()); 194 info.pop(); 195 } 196 catch (SecurityException e) 197 { 198 throw e; 199 } 200 catch (SystemException e) 201 { 202 throw e; 203 } 204 catch (IllegalStateException e) 205 { 206 throw e; 207 } 208 catch (RemoteException e) 209 { 210 destroySession(); 212 throw new SystemException (e.toString()); 213 } 214 catch (Exception e) 215 { 216 throw new SystemException (e.toString()); 217 } 218 } 219 220 public void setRollbackOnly() 221 throws IllegalStateException , 222 SystemException 223 { 224 ThreadInfo info = getThreadInfo(); 225 226 try 227 { 228 getSession().setRollbackOnly(info.getTpc()); 229 } 230 catch (SystemException e) 231 { 232 throw e; 233 } 234 catch (IllegalStateException e) 235 { 236 throw e; 237 } 238 catch (RemoteException e) 239 { 240 destroySession(); 242 throw new SystemException (e.toString()); 243 } 244 catch (Exception e) 245 { 246 throw new SystemException (e.toString()); 247 } 248 } 249 250 public int getStatus() 251 throws SystemException 252 { 253 ThreadInfo info = getThreadInfo(); 254 Object tpc = info.getTpc(); 255 256 if (tpc == null) 257 { 258 return Status.STATUS_NO_TRANSACTION; 259 } 260 261 try 262 { 263 return getSession().getStatus(tpc); 264 } 265 catch (SystemException e) 266 { 267 throw e; 268 } 269 catch (RemoteException e) 270 { 271 destroySession(); 273 throw new SystemException (e.toString()); 274 } 275 catch (Exception e) 276 { 277 throw new SystemException (e.toString()); 278 } 279 } 280 281 public void setTransactionTimeout(int seconds) 282 throws SystemException 283 { 284 getThreadInfo().setTimeout(seconds); 285 } 286 287 288 292 public Object getTransactionPropagationContext() 293 { 294 return getThreadInfo().getTpc(); 295 } 296 297 public Object getTransactionPropagationContext(Transaction tx) 298 { 299 throw new InternalError ("Should not have been used."); 301 } 302 303 304 308 public Reference getReference() 309 throws NamingException 310 { 311 Reference ref = new Reference ("org.jboss.tm.usertx.client.ClientUserTransaction", 312 "org.jboss.tm.usertx.client.ClientUserTransactionObjectFactory", 313 null); 314 315 return ref; 316 } 317 318 319 321 324 private UserTransactionSession session = null; 325 326 329 private transient ThreadLocal threadInfo = new ThreadLocal (); 330 331 332 335 private synchronized void createSession() 336 { 337 if (session != null) 339 destroySession(); 340 341 try 342 { 343 UserTransactionSessionFactory factory; 345 Hashtable env = (Hashtable ) NamingContextFactory.lastInitialContextEnv.get(); 346 InitialContext ctx = new InitialContext (env); 347 factory = (UserTransactionSessionFactory) ctx.lookup("UserTransactionSessionFactory"); 348 session = factory.newInstance(); 350 } 351 catch (Exception ex) 352 { 353 throw new RuntimeException ("UT factory lookup failed", ex); 354 } 355 } 356 357 360 private synchronized void destroySession() 361 { 362 if (session != null) 363 { 364 try 365 { 366 session.destroy(); 367 } 368 catch (RemoteException ex) 369 { 370 } 372 session = null; 373 } 374 } 375 376 380 private synchronized UserTransactionSession getSession() 381 { 382 if (session == null) 383 createSession(); 384 return session; 385 } 386 387 388 391 private ThreadInfo getThreadInfo() 392 { 393 ThreadInfo ret = (ThreadInfo) threadInfo.get(); 394 395 if (ret == null) 396 { 397 ret = new ThreadInfo(); 398 threadInfo.set(ret); 399 } 400 401 return ret; 402 } 403 404 405 407 411 private class ThreadInfo 412 { 413 418 private LinkedList tpcStack = new LinkedList (); 419 420 424 private int timeout = 0; 425 426 430 protected void finalize() 431 throws Throwable 432 { 433 try 434 { 435 while (!tpcStack.isEmpty()) 436 { 437 Object tpc = getTpc(); 438 pop(); 439 440 try 441 { 442 getSession().rollback(tpc); 443 } 444 catch (Exception ex) 445 { 446 } 448 } 449 } 450 catch (Throwable t) 451 { 452 } 454 super.finalize(); 455 } 456 457 460 void push(Object tpc) 461 { 462 tpcStack.addLast(tpc); 463 } 464 465 468 void pop() 469 { 470 tpcStack.removeLast(); 471 } 472 473 476 Object getTpc() 477 { 478 return (tpcStack.isEmpty()) ? null : tpcStack.getLast(); 479 } 480 481 486 int getTimeout() 487 { 488 return timeout; 489 } 490 491 495 void setTimeout(int seconds) 496 { 497 timeout = seconds; 498 } 499 } 500 501 } 502 | Popular Tags |