1 25 26 package org.objectweb.easybeans.transaction.interceptors; 27 28 import static javax.transaction.Status.STATUS_COMMITTED ; 29 30 import javax.ejb.EJBException ; 31 import javax.transaction.InvalidTransactionException ; 32 import javax.transaction.SystemException ; 33 import javax.transaction.Transaction ; 34 35 import org.objectweb.easybeans.api.EasyBeansInvocationContext; 36 import org.objectweb.easybeans.log.JLog; 37 import org.objectweb.easybeans.log.JLogFactory; 38 39 44 public class BMTStatelessTransactionInterceptor extends AbsTransactionInterceptor { 45 46 49 private JLog logger = JLogFactory.getLog(BMTStatelessTransactionInterceptor.class); 50 51 55 public BMTStatelessTransactionInterceptor() { 56 super(); 57 } 58 59 68 @Override 69 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 70 logger.debug("Calling BMT TX interceptor"); 71 72 Transaction transaction; 74 try { 75 transaction = getTransactionManager().getTransaction(); 76 } catch (SystemException se) { 77 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 78 } 79 80 logger.debug("Transaction found = {0}", transaction); 81 82 93 94 Transaction suspendedTransaction = null; 95 if (transaction != null) { 96 try { 97 logger.debug("Suspending transaction {0}", transaction); 98 suspendedTransaction = getTransactionManager().suspend(); 99 } catch (SystemException se) { 100 throw new EJBException ("Cannot call suspend() on the transaction manager.", se); 101 } 102 } 103 104 boolean gotBusinessException = false; 105 try { 106 return invocationContext.proceed(); 107 } catch (Exception e) { 108 gotBusinessException = true; 109 handleBeanManagedException(invocationContext, e); 110 return null; 112 } finally { 113 if (!gotBusinessException) { 114 132 Transaction transactionAfter = null; 133 try { 134 transactionAfter = getTransactionManager().getTransaction(); 135 } catch (SystemException se) { 136 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 137 } 138 if (transactionAfter != null) { 139 int transactionStatus = transactionAfter.getStatus(); 140 if (transactionStatus != STATUS_COMMITTED) { 142 String errMsg = "Transaction started by the bean but not committed."; 143 logger.error(errMsg); 145 transactionAfter.rollback(); 147 throw new EJBException (errMsg); 150 } 151 } 152 } 153 154 158 if (suspendedTransaction != null) { 159 160 logger.debug("Resuming transaction {0}", transaction); 161 162 try { 163 getTransactionManager().resume(suspendedTransaction); 164 } catch (InvalidTransactionException ite) { 165 throw new EJBException ( 166 "Cannot call resume() on the given transaction. There is an invalid transaction", ite); 167 } catch (IllegalStateException ise) { 168 throw new EJBException ( 169 "Cannot call resume() on the given transaction. There is another associated transaction", 170 ise); 171 } catch (SystemException se) { 172 throw new EJBException ("Cannot call resume() on the given transaction. Unexpected error condition", 173 se); 174 } 175 } 176 } 177 } 178 179 } 180 | Popular Tags |