1 45 package org.openejb.core.transaction; 46 47 import java.rmi.RemoteException ; 48 49 import javax.ejb.EnterpriseBean ; 50 import javax.transaction.Transaction ; 51 import javax.transaction.TransactionManager ; 52 53 import org.openejb.ApplicationException; 54 import org.openejb.InvalidateReferenceException; 55 import org.openejb.OpenEJB; 56 import org.openejb.SystemException; 57 import org.openejb.core.ThreadContext; 58 import org.openejb.util.Logger; 59 60 70 public abstract class TransactionPolicy { 71 72 public static final int Mandatory = 0; 73 public static final int Never = 1; 74 public static final int NotSupported = 2; 75 public static final int Required = 3; 76 public static final int RequiresNew = 4; 77 public static final int Supports = 5; 78 public static final int BeanManaged = 6; 79 80 public int policyType; 81 private TransactionManager manager; 82 protected TransactionContainer container; 83 84 protected final static Logger logger = Logger.getInstance( "OpenEJB", "org.openejb.util.resources" ); 85 protected final static Logger txLogger = Logger.getInstance( "Transaction", "org.openejb.util.resources" ); 86 87 protected TransactionManager getTxMngr( ) { 88 if(manager==null) { 89 manager = OpenEJB.getTransactionManager(); 90 } 91 return manager; 92 } 93 94 public TransactionContainer getContainer(){ 95 return container; 96 } 97 98 public String policyToString() { 99 return "Internal Error: no such policy"; 100 } 101 102 public abstract void handleApplicationException( Throwable appException, TransactionContext context ) throws org.openejb.ApplicationException; 103 public abstract void handleSystemException( Throwable sysException, EnterpriseBean instance, TransactionContext context ) throws org.openejb.ApplicationException, org.openejb.SystemException; 104 public abstract void beforeInvoke( EnterpriseBean bean, TransactionContext context ) throws org.openejb.SystemException, org.openejb.ApplicationException; 105 public abstract void afterInvoke(EnterpriseBean bean, TransactionContext context ) throws org.openejb.ApplicationException, org.openejb.SystemException; 106 107 protected void markTxRollbackOnly( Transaction tx ) throws SystemException{ 108 try { 109 if ( tx != null ) { 110 tx.setRollbackOnly(); 111 if(txLogger.isInfoEnabled()) { 112 txLogger.info(policyToString()+"setRollbackOnly() on transaction "+tx); 113 } 114 } 115 } catch ( javax.transaction.SystemException se ) { 116 logger.error("Exception during setRollbackOnly()", se); 117 throw new org.openejb.SystemException(se); 118 } 119 } 120 121 protected Transaction suspendTransaction() throws SystemException{ 122 try { 123 Transaction tx = getTxMngr( ).suspend(); 124 if(txLogger.isInfoEnabled()) { 125 txLogger.info(policyToString()+"Suspended transaction "+tx); 126 } 127 return tx; 128 } catch ( javax.transaction.SystemException se ) { 129 logger.error("Exception during suspend()", se); 130 throw new org.openejb.SystemException(se); 131 } 132 } 133 134 protected void resumeTransaction(Transaction tx) throws SystemException{ 135 try { 136 if ( tx == null) { 137 if(txLogger.isInfoEnabled()) { 138 txLogger.info(policyToString()+"No transaction to resume"); 139 } 140 } else { 141 if(txLogger.isInfoEnabled()) { 142 txLogger.info(policyToString()+"Resuming transaction "+tx); 143 } 144 getTxMngr( ).resume(tx); 145 } 146 }catch(javax.transaction.InvalidTransactionException ite){ 147 txLogger.error("Could not resume the client's transaction, the transaction is no longer valid: "+ite.getMessage()); 149 throw new org.openejb.SystemException(ite); 150 }catch(IllegalStateException e){ 151 txLogger.error("Could not resume the client's transaction: "+e.getMessage()); 153 throw new org.openejb.SystemException(e); 154 }catch(javax.transaction.SystemException e){ 155 txLogger.error("Could not resume the client's transaction: The transaction reported a system exception: "+e.getMessage()); 157 throw new org.openejb.SystemException(e); 158 } 159 } 160 161 protected void commitTransaction( Transaction tx ) throws SystemException{ 162 try { 163 if(txLogger.isInfoEnabled()) { 164 txLogger.info(policyToString()+"Committing transaction "+tx); 165 } 166 if(tx.equals(getTxMngr().getTransaction())) { 167 getTxMngr().commit(); 171 } else { 172 tx.commit(); 173 } 174 } catch ( javax.transaction.RollbackException e ) { 175 txLogger.info("The transaction has been rolled back rather than commited: "+e.getMessage()); 177 178 } catch ( javax.transaction.HeuristicMixedException e ) { 179 txLogger.info("A heuristic decision was made, some relevant updates have been committed while others have been rolled back: "+e.getMessage()); 181 182 } catch ( javax.transaction.HeuristicRollbackException e ) { 183 txLogger.info("A heuristic decision was made while commiting the transaction, some relevant updates have been rolled back: "+e.getMessage()); 185 186 } catch (SecurityException e){ 187 txLogger.error("The current thread is not allowed to commit the transaction: "+e.getMessage()); 189 throw new org.openejb.SystemException( e ); 190 191 } catch (IllegalStateException e){ 192 txLogger.error("The current thread is not associated with a transaction: "+e.getMessage()); 194 throw new org.openejb.SystemException( e ); 195 196 } catch (javax.transaction.SystemException e){ 197 txLogger.error("The Transaction Manager has encountered an unexpected error condition while attempting to commit the transaction: "+e.getMessage()); 198 throw new org.openejb.SystemException( e ); 200 } 201 } 202 203 protected void rollbackTransaction( Transaction tx ) throws SystemException{ 204 try { 205 if(txLogger.isInfoEnabled()) { 206 txLogger.info(policyToString()+"Rolling back transaction "+tx); 207 } 208 if(tx.equals(getTxMngr().getTransaction())) { 209 getTxMngr().rollback(); 213 } else { 214 tx.rollback(); 215 } 216 } catch (IllegalStateException e){ 217 logger.error("The TransactionManager reported an exception while attempting to rollback the transaction: "+e.getMessage()); 219 throw new org.openejb.SystemException( e ); 220 221 } catch (javax.transaction.SystemException e){ 222 logger.error("The TransactionManager reported an exception while attempting to rollback the transaction: "+e.getMessage()); 224 throw new org.openejb.SystemException( e ); 225 } 226 } 227 228 protected void throwAppExceptionToServer( Throwable appException ) throws ApplicationException{ 229 throw new ApplicationException( appException ); 230 } 231 232 protected void throwTxExceptionToServer( Throwable sysException ) throws ApplicationException{ 233 234 String message = "The transaction was rolled back because the bean encountered a non-application exception :" + sysException.getClass().getName() + " : "+sysException.getMessage(); 236 javax.transaction.TransactionRolledbackException txException = new javax.transaction.TransactionRolledbackException (message); 237 238 throw new InvalidateReferenceException( txException ); 240 241 } 243 244 250 protected void throwExceptionToServer( Throwable sysException ) throws ApplicationException{ 251 RemoteException re = new RemoteException ("The bean encountered a non-application exception.", sysException); 254 255 throw new InvalidateReferenceException( re ); 257 258 260 } 261 262 protected void logSystemException(Throwable sysException){ 263 logger.error( "The bean instances business method encountered a system exception:"+sysException.getMessage(), sysException); 266 } 267 268 protected void discardBeanInstance(EnterpriseBean instance, ThreadContext callContext){ 269 container.discardInstance( instance, callContext ); 270 } 271 272 protected void beginTransaction() throws javax.transaction.SystemException { 273 try { 274 getTxMngr( ).begin(); 275 if(txLogger.isInfoEnabled()) { 276 txLogger.info(policyToString()+"Started transaction "+getTxMngr( ).getTransaction()); 277 } 278 } catch ( javax.transaction.NotSupportedException nse ) { 279 logger.error("", nse); 280 } 281 } 282 283 284 335 protected void handleCallbackException(){ 336 } 337 } 338 339 340 | Popular Tags |