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.InvalidTransactionException ; 34 import javax.transaction.NotSupportedException ; 35 import javax.transaction.RollbackException ; 36 import javax.transaction.SystemException ; 37 import javax.transaction.Transaction ; 38 39 import org.objectweb.easybeans.api.EasyBeansInvocationContext; 40 import org.objectweb.easybeans.log.JLog; 41 import org.objectweb.easybeans.log.JLogFactory; 42 43 47 public class CMTRequiresNewTransactionInterceptor extends AbsTransactionInterceptor { 48 49 52 private JLog logger = JLogFactory.getLog(CMTRequiresNewTransactionInterceptor.class); 53 54 55 59 public CMTRequiresNewTransactionInterceptor() { 60 super(); 61 } 62 63 72 @Override 73 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 74 logger.debug("Calling RequiresNew TX interceptor"); 75 76 Transaction transaction; 78 try { 79 transaction = getTransactionManager().getTransaction(); 80 } catch (SystemException se) { 81 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 82 } 83 84 logger.debug("Transaction found = {0}", transaction); 85 86 87 95 Transaction suspendedTransaction = null; 97 if (transaction != null) { 98 try { 99 logger.debug("Suspending transaction {0}", transaction); 100 suspendedTransaction = getTransactionManager().suspend(); 101 } catch (SystemException se) { 102 throw new EJBException ("Cannot call suspend() on the transaction manager.", se); 103 } 104 } 105 106 118 try { 119 getTransactionManager().begin(); 120 } catch (NotSupportedException nse) { 121 throw new EJBException ("Transaction Manager implementation does not support nested transactions.", nse); 122 } catch (SystemException se) { 123 throw new EJBException ("Cannot call begin() on the transaction manager.", se); 124 } 125 126 boolean gotBusinessException = false; 127 try { 128 return invocationContext.proceed(); 129 } catch (Exception e) { 130 gotBusinessException = true; 131 handleContextContainerTransaction(invocationContext, e); 132 133 return null; 135 } finally { 136 137 if (!gotBusinessException) { 140 Transaction transactionAfter = null; 142 try { 143 transactionAfter = getTransactionManager().getTransaction(); 144 } catch (SystemException se) { 145 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 146 } 147 148 if (transactionAfter == null) { 149 throw new EJBException ("Transaction disappeared."); 150 } 151 152 158 try { 159 switch (getTransactionManager().getStatus()) { 160 case STATUS_ACTIVE: 161 getTransactionManager().commit(); 162 break; 163 case STATUS_MARKED_ROLLBACK: 164 getTransactionManager().rollback(); 165 break; 166 default: 167 throw new RuntimeException ("Unexpected transaction status" + getTransactionManager().getStatus()); 168 } 169 } catch (RollbackException e) { 170 throw new TransactionRolledbackLocalException ("Could not commit transaction", e); 171 } catch (Exception e) { 172 throw new EJBException ("Container exception", e); 173 } 174 } 175 176 if (suspendedTransaction != null) { 178 179 logger.debug("Resuming transaction {0}", transaction); 180 181 try { 182 getTransactionManager().resume(suspendedTransaction); 183 } catch (InvalidTransactionException ite) { 184 throw new EJBException ( 185 "Cannot call resume() on the given transaction. There is an invalid transaction", ite); 186 } catch (IllegalStateException ise) { 187 throw new EJBException ( 188 "Cannot call resume() on the given transaction. There is another associated transaction", 189 ise); 190 } catch (SystemException se) { 191 throw new EJBException ("Cannot call resume() on the given transaction. Unexpected error condition", 192 se); 193 } 194 } 195 } 196 } 197 198 } 199 | Popular Tags |