1 package org.apache.geronimo.transaction.context; 2 3 import java.io.Serializable ; 4 import javax.resource.ResourceException ; 5 import javax.transaction.HeuristicMixedException ; 6 import javax.transaction.HeuristicRollbackException ; 7 import javax.transaction.InvalidTransactionException ; 8 import javax.transaction.NotSupportedException ; 9 import javax.transaction.RollbackException ; 10 import javax.transaction.SystemException ; 11 import javax.transaction.UserTransaction ; 12 13 import org.apache.geronimo.transaction.TrackedConnectionAssociator; 14 15 public final class OnlineUserTransaction implements UserTransaction , Serializable { 16 private transient TransactionContextManager transactionContextManager; 17 private transient TrackedConnectionAssociator trackedConnectionAssociator; 18 19 boolean isActive() { 20 return transactionContextManager != null; 21 } 22 23 public void setUp(TransactionContextManager transactionContextManager, TrackedConnectionAssociator trackedConnectionAssociator) { 24 this.transactionContextManager = transactionContextManager; 25 this.trackedConnectionAssociator = trackedConnectionAssociator; 26 } 27 28 29 public int getStatus() throws SystemException { 30 return transactionContextManager.getStatus(); 31 } 32 33 public void setRollbackOnly() throws IllegalStateException , SystemException { 34 transactionContextManager.setRollbackOnly(); 35 } 36 37 public void setTransactionTimeout(int seconds) throws SystemException { 38 if (seconds < 0) { 39 throw new SystemException ("transaction timeout must be positive or 0, not " + seconds); 40 } 41 transactionContextManager.setTransactionTimeout(seconds); 42 } 43 44 public void begin() throws NotSupportedException , SystemException { 45 transactionContextManager.newBeanTransactionContext(0L); 46 47 if(trackedConnectionAssociator != null) { 48 try { 49 trackedConnectionAssociator.newTransaction(); 50 } catch (ResourceException e) { 51 throw (SystemException )new SystemException ().initCause(e); 52 } 53 } 54 } 55 56 public void commit() throws HeuristicMixedException , HeuristicRollbackException , IllegalStateException , RollbackException , SecurityException , SystemException { 57 TransactionContext ctx = transactionContextManager.getContext(); 58 if (ctx instanceof BeanTransactionContext == false) { 59 throw new IllegalStateException ("Transaction has not been started"); 60 } 61 BeanTransactionContext beanContext = (BeanTransactionContext) ctx; 62 try { 63 if (!beanContext.commit()) { 64 throw new RollbackException (); 65 } 66 } finally { 67 TransactionContext oldContext = beanContext.getOldContext(); 68 transactionContextManager.setContext(oldContext); 69 try { 70 oldContext.resume(); 71 } catch (InvalidTransactionException e) { 72 throw (SystemException )new SystemException ("Unable to resume perexisting transaction context").initCause(e); 73 } 74 } 75 } 76 77 public void rollback() throws IllegalStateException , SecurityException , SystemException { 78 TransactionContext ctx = transactionContextManager.getContext(); 79 if (ctx instanceof BeanTransactionContext == false) { 80 throw new IllegalStateException ("Transaction has not been started"); 81 } 82 BeanTransactionContext beanContext = (BeanTransactionContext) ctx; 83 try { 84 beanContext.rollback(); 85 } finally { 86 TransactionContext oldContext = beanContext.getOldContext(); 87 transactionContextManager.setContext(oldContext); 88 try { 89 oldContext.resume(); 90 } catch (InvalidTransactionException e) { 91 throw (SystemException )new SystemException ("Unable to resume perexisting transaction context").initCause(e); 92 } 93 } 94 } 95 } 96 | Popular Tags |