1 package org.openejb.core.stateful; 2 3 import java.rmi.RemoteException ; 4 5 import javax.ejb.EnterpriseBean ; 6 import javax.transaction.Status ; 7 import javax.transaction.Transaction ; 8 9 import org.openejb.ApplicationException; 10 import org.openejb.InvalidateReferenceException; 11 import org.openejb.core.transaction.TransactionContainer; 12 import org.openejb.core.transaction.TransactionContext; 13 import org.openejb.core.transaction.TransactionPolicy; 14 15 19 public class StatefulBeanManagedTxPolicy extends TransactionPolicy { 20 21 public StatefulBeanManagedTxPolicy(TransactionContainer container){ 22 this(); 23 if(container instanceof org.openejb.Container && 24 ((org.openejb.Container)container).getContainerType()!=org.openejb.Container.STATEFUL) { 25 throw new IllegalArgumentException (); 26 } 27 this.container = container; 28 } 29 30 public StatefulBeanManagedTxPolicy(){ 31 policyType = BeanManaged; 32 } 33 34 public String policyToString() { 35 return "TX_BeanManaged: "; 36 } 37 63 public void beforeInvoke(EnterpriseBean instance, TransactionContext context) throws org.openejb.SystemException, org.openejb.ApplicationException{ 64 try { 65 66 StatefulInstanceManager instanceManager = (StatefulInstanceManager) context.context.get(StatefulInstanceManager.class); 67 context.clientTx = suspendTransaction(); 69 70 Object primaryKey = context.callContext.getPrimaryKey(); 72 Object possibleBeanTx = instanceManager.getAncillaryState( primaryKey ); 73 if ( possibleBeanTx instanceof Transaction ) { 74 context.currentTx = (Transaction )possibleBeanTx; 75 resumeTransaction( context.currentTx ); 76 } 77 } catch ( org.openejb.OpenEJBException e ) { 78 handleSystemException( e.getRootCause(), instance, context ); 79 } 80 } 81 82 97 public void afterInvoke(EnterpriseBean instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{ 98 try { 99 context.currentTx = getTxMngr().getTransaction(); 101 102 105 if ( context.currentTx != null && 106 context.currentTx.getStatus() != Status.STATUS_COMMITTED && 107 context.currentTx.getStatus() != Status.STATUS_ROLLEDBACK ) { 108 suspendTransaction(); 111 } 112 113 115 Object primaryKey = context.callContext.getPrimaryKey(); 116 StatefulInstanceManager instanceManager = (StatefulInstanceManager) context.context.get(StatefulInstanceManager.class); 117 instanceManager.setAncillaryState( primaryKey, context.currentTx ); 118 119 } catch ( org.openejb.OpenEJBException e ) { 120 handleSystemException( e.getRootCause(), instance, context ); 121 } catch ( javax.transaction.SystemException e ) { 122 handleSystemException( e, instance, context ); 123 } catch ( Throwable e ){ 124 handleSystemException( e, instance, context ); 125 } finally { 126 resumeTransaction( context.clientTx ); 127 } 128 } 129 130 143 public void handleApplicationException( Throwable appException, TransactionContext context) throws ApplicationException{ 144 throw new ApplicationException( appException ); 146 } 147 148 179 public void handleSystemException( Throwable sysException, EnterpriseBean instance, TransactionContext context) throws org.openejb.ApplicationException, org.openejb.SystemException{ 180 181 logSystemException( sysException ); 183 184 if ( context.currentTx != null ) markTxRollbackOnly( context.currentTx ); 186 187 discardBeanInstance( instance, context.callContext); 189 190 throwExceptionToServer( sysException ); 192 193 } 194 195 protected void throwExceptionToServer(Throwable sysException) throws ApplicationException{ 196 RemoteException re = new RemoteException ("The bean encountered a non-application exception.", sysException); 198 199 throw new InvalidateReferenceException( re ); 200 201 203 } 204 205 protected void throwTxExceptionToServer( Throwable sysException ) throws ApplicationException{ 206 207 String message = "The transaction was rolled back because the bean encountered a non-application exception :" + sysException.getClass().getName() + " : "+sysException.getMessage(); 209 javax.transaction.TransactionRolledbackException txException = new javax.transaction.TransactionRolledbackException (message); 210 211 throw new InvalidateReferenceException( txException ); 212 213 } 215 } 216 217 | Popular Tags |