1 45 package org.openejb.core; 46 47 import java.util.Hashtable ; 48 import java.util.Vector ; 49 50 import javax.transaction.Status ; 51 import javax.transaction.Synchronization ; 52 import javax.transaction.Transaction ; 53 import javax.transaction.TransactionManager ; 54 import javax.transaction.xa.XAResource ; 55 56 87 public class TransactionManagerWrapper implements TransactionManager { 88 93 final private TransactionManager transactionManager; 94 96 final private Hashtable wrapperMap = new Hashtable (); 97 98 final static protected org.apache.log4j.Category logger = org.apache.log4j.Category.getInstance("Transaction"); 99 100 105 public TransactionManagerWrapper(TransactionManager txMngr) { 106 transactionManager = txMngr; 107 } 108 109 public javax.transaction.TransactionManager getTxManager() { 110 return transactionManager; 111 } 112 113 120 public void begin( )throws javax.transaction.SystemException , javax.transaction.NotSupportedException { 121 int status=transactionManager.getStatus(); 122 if(status== Status.STATUS_NO_TRANSACTION || 123 status== Status.STATUS_ROLLEDBACK || 124 status== Status.STATUS_COMMITTED ) { 125 transactionManager.begin(); 126 createTxWrapper(); 127 }else { 128 throw new javax.transaction.NotSupportedException ("Can't start new transaction."+getStatus(status)); 129 } 130 } 131 140 public void commit()throws javax.transaction.SystemException , javax.transaction.RollbackException , javax.transaction.HeuristicRollbackException , javax.transaction.HeuristicMixedException { 141 transactionManager.commit(); 142 } 143 150 public int getStatus()throws javax.transaction.SystemException { 151 return transactionManager.getStatus(); 152 } 153 160 public Transaction getTransaction( )throws javax.transaction.SystemException { 161 return getTxWrapper(transactionManager.getTransaction()); 162 } 163 171 public void resume(Transaction tx) 172 throws javax.transaction.SystemException , javax.transaction.InvalidTransactionException { 173 if ( tx instanceof TransactionWrapper ) { 174 tx = ((TransactionWrapper)tx).transaction; 175 } 176 transactionManager.resume(tx); 177 } 178 185 public Transaction suspend( )throws javax.transaction.SystemException { 186 return getTxWrapper(transactionManager.suspend()); 187 } 188 194 public void rollback()throws javax.transaction.SystemException { 195 transactionManager.rollback(); 196 } 197 203 public void setRollbackOnly( )throws javax.transaction.SystemException { 204 transactionManager.setRollbackOnly(); 205 } 206 213 public void setTransactionTimeout(int x)throws javax.transaction.SystemException { 214 transactionManager.setTransactionTimeout(x); 215 } 216 217 224 private Transaction getTxWrapper(Transaction tx)throws javax.transaction.SystemException { 225 if ( tx == null ) { 226 return null; 227 } 228 return (TransactionWrapper)wrapperMap.get(tx); 229 } 230 231 235 private void createTxWrapper() { 236 try { 237 Transaction tx = transactionManager.getTransaction(); 238 TransactionWrapper txW = new TransactionWrapper(tx); 239 tx.registerSynchronization(txW); 240 wrapperMap.put(tx,txW); 241 } catch ( Exception re ) { 242 logger.info("", re); 245 } 246 } 247 248 252 private class TransactionWrapper 253 implements Transaction , javax.transaction.Synchronization { 254 255 258 private final Transaction transaction; 259 260 private final Vector registeredSynchronizations; 261 262 final public static int MAX_PRIORITY_LEVEL = 3; 263 264 private TransactionWrapper(Transaction tx) { 265 transaction = tx; 266 registeredSynchronizations = new Vector (); 267 } 268 269 270 274 public Transaction getTransaction() { 275 276 return transaction; 277 } 278 279 public boolean equals(java.lang.Object obj) { 280 if(obj != null && obj instanceof TransactionWrapper) { 281 return transaction.equals( ((TransactionWrapper)obj).getTransaction() ); 282 } 283 284 return false; 285 } 286 public int hashCode() { 288 return transaction.hashCode(); 289 } 290 291 public String toString(){ 292 return transaction.toString(); 293 } 294 295 public void commit() 296 throws javax.transaction.SystemException , javax.transaction.RollbackException , javax.transaction.HeuristicRollbackException , javax.transaction.HeuristicMixedException { 297 transaction.commit(); 298 } 299 300 public boolean delistResource(XAResource xaRes, int flag)throws javax.transaction.SystemException { 301 return transaction.delistResource(xaRes,flag); 302 } 303 304 public boolean enlistResource(XAResource xaRes)throws javax.transaction.SystemException , javax.transaction.RollbackException { 305 return transaction.enlistResource(xaRes); 306 } 307 308 public int getStatus()throws javax.transaction.SystemException { 309 return transaction.getStatus(); 310 } 311 316 public void registerSynchronization(Synchronization sync) 317 throws javax.transaction.SystemException , javax.transaction.RollbackException { 318 319 registerSynchronization(sync,MAX_PRIORITY_LEVEL); 320 } 321 322 331 private void registerSynchronization(Synchronization sync, int priority) { 332 if (!registeredSynchronizations.contains(sync)) { 333 registeredSynchronizations.addElement(sync); 334 } 335 } 336 337 public void rollback()throws javax.transaction.SystemException { 338 transaction.rollback(); 339 } 340 341 public void setRollbackOnly() throws javax.transaction.SystemException { 342 transaction.setRollbackOnly(); 343 } 344 348 public void beforeCompletion() { 349 int count = registeredSynchronizations.size(); 350 for ( int i=0; i<count; ++i ) { 351 try { 352 Synchronization sync = (Synchronization )registeredSynchronizations.elementAt(i); 353 sync.beforeCompletion(); 354 } catch ( RuntimeException re ) { 355 logger.error("", re); 356 } 357 } 358 } 359 360 public void afterCompletion(int status) { 361 int count = registeredSynchronizations.size(); 362 for ( int i=0; i<count; ++i ) { 363 try { 364 Synchronization sync = (Synchronization )registeredSynchronizations.elementAt(i); 365 sync.afterCompletion(status); 366 } catch ( RuntimeException re ) { 367 logger.error("", re); 368 } 369 } 370 wrapperMap.remove(transaction); 371 } 372 373 } 375 381 public static String getStatus( int status ) 382 { 383 StringBuffer buffer; 384 385 buffer = new StringBuffer (100); 386 switch ( status ) { 387 case Status.STATUS_ACTIVE: 388 buffer.append( "STATUS_ACTIVE: " ); 389 buffer.append( "A transaction is associated with the target object and it is in the active state." ); 390 break; 391 case Status.STATUS_COMMITTED: 392 buffer.append( "STATUS_COMMITTED: " ); 393 buffer.append( "A transaction is associated with the target object and it has been committed." ); 394 break; 395 case Status.STATUS_COMMITTING: 396 buffer.append( "STATUS_COMMITTING: " ); 397 buffer.append( "A transaction is associated with the target object and it is in the process of committing." ); 398 break; 399 case Status.STATUS_MARKED_ROLLBACK: 400 buffer.append( "STATUS_MARKED_ROLLBACK: " ); 401 buffer.append( "A transaction is associated with the target object and it has been marked for rollback, perhaps as a result of a setRollbackOnly operation." ); 402 break; 403 case Status.STATUS_NO_TRANSACTION: 404 buffer.append( "STATUS_NO_TRANSACTION: " ); 405 buffer.append( "No transaction is currently associated with the target object." ); 406 break; 407 case Status.STATUS_PREPARED: 408 buffer.append( "STATUS_PREPARED: " ); 409 buffer.append( "A transaction is associated with the target object and it has been prepared, i.e." ); 410 break; 411 case Status.STATUS_PREPARING: 412 buffer.append( "STATUS_PREPARING: " ); 413 buffer.append( "A transaction is associated with the target object and it is in the process of preparing." ); 414 break; 415 case Status.STATUS_ROLLEDBACK: 416 buffer.append( "STATUS_ROLLEDBACK: " ); 417 buffer.append( "A transaction is associated with the target object and the outcome has been determined as rollback." ); 418 break; 419 case Status.STATUS_ROLLING_BACK: 420 buffer.append( "STATUS_ROLLING_BACK: " ); 421 buffer.append( "A transaction is associated with the target object and it is in the process of rolling back." ); 422 break; 423 default: 424 buffer.append( "Unknown status " + status ); 425 break; 426 } 427 return buffer.toString(); 428 } 429 } 430 | Popular Tags |