1 25 26 package org.objectweb.easybeans.transaction.interceptors; 27 28 import static javax.transaction.Status.STATUS_ACTIVE ; 29 import static javax.transaction.Status.STATUS_MARKED_ROLLBACK ; 30 31 import javax.ejb.EJBException ; 32 import javax.ejb.TransactionRolledbackLocalException ; 33 import javax.transaction.NotSupportedException ; 34 import javax.transaction.RollbackException ; 35 import javax.transaction.SystemException ; 36 import javax.transaction.Transaction ; 37 38 import org.objectweb.easybeans.api.EasyBeansInvocationContext; 39 import org.objectweb.easybeans.log.JLog; 40 import org.objectweb.easybeans.log.JLogFactory; 41 42 46 public class CMTRequiredTransactionInterceptor extends AbsTransactionInterceptor { 47 48 51 private JLog logger = JLogFactory.getLog(CMTRequiredTransactionInterceptor.class); 52 53 57 public CMTRequiredTransactionInterceptor() { 58 super(); 59 } 60 61 71 @Override 72 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 73 logger.debug("Calling Required TX interceptor"); 74 75 Transaction transaction; 77 try { 78 transaction = getTransactionManager().getTransaction(); 79 } catch (SystemException se) { 80 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 81 } 82 83 logger.debug("Transaction found = {0}", transaction); 84 85 91 boolean startedTransaction = false; 92 if (transaction == null) { 93 try { 94 getTransactionManager().begin(); 95 startedTransaction = true; 96 } catch (NotSupportedException nse) { 97 throw new EJBException ("Transaction Manager implementation does not support nested transactions.", nse); 98 } catch (SystemException se) { 99 throw new EJBException ("Cannot call begin() on the transaction manager.", se); 100 } 101 } 102 108 boolean gotBusinessException = false; 109 try { 110 return invocationContext.proceed(); 111 112 } catch (Exception e) { 113 gotBusinessException = true; 114 115 if (!startedTransaction) { 118 handleContextClientTransaction(invocationContext, e); 119 } else { 120 handleContextContainerTransaction(invocationContext, e); 122 } 123 return null; 125 } finally { 126 if (!gotBusinessException) { 127 128 if (startedTransaction) { 132 133 Transaction transactionAfter = null; 135 try { 136 transactionAfter = getTransactionManager().getTransaction(); 137 } catch (SystemException se) { 138 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 139 } 140 141 if (transactionAfter == null) { 142 throw new RuntimeException ("Transaction disappeared."); 143 } 144 145 151 try { 152 switch (getTransactionManager().getStatus()) { 153 case STATUS_ACTIVE: 154 getTransactionManager().commit(); 155 break; 156 case STATUS_MARKED_ROLLBACK: 157 getTransactionManager().rollback(); 158 break; 159 default: 160 throw new RuntimeException ("Unexpected transaction status" + getTransactionManager().getStatus()); 161 } 162 } catch (RollbackException e) { 163 throw new TransactionRolledbackLocalException ("Could not commit transaction", e); 164 } catch (Exception e) { 165 throw new EJBException ("Container exception", e); 166 } 167 } 168 } 169 } 170 171 } 172 173 } 174 | Popular Tags |