1 25 26 package org.objectweb.easybeans.transaction.interceptors; 27 28 import static javax.transaction.Status.STATUS_COMMITTED ; 29 import static javax.transaction.Status.STATUS_ROLLEDBACK ; 30 31 import javax.ejb.EJBException ; 32 import javax.transaction.InvalidTransactionException ; 33 import javax.transaction.SystemException ; 34 import javax.transaction.Transaction ; 35 36 import org.objectweb.easybeans.api.EasyBeansInvocationContext; 37 import org.objectweb.easybeans.api.bean.EasyBeansSFSB; 38 import org.objectweb.easybeans.api.container.EZBSessionContext; 39 import org.objectweb.easybeans.log.JLog; 40 import org.objectweb.easybeans.log.JLogFactory; 41 42 47 public class BMTStatefulTransactionInterceptor extends AbsTransactionInterceptor { 48 49 52 private JLog logger = JLogFactory.getLog(BMTStatefulTransactionInterceptor.class); 53 54 57 public BMTStatefulTransactionInterceptor() { 58 super(); 59 } 60 61 70 @Override 71 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 72 logger.debug("Calling BMT TX interceptor"); 73 74 Transaction transaction; 76 try { 77 transaction = getTransactionManager().getTransaction(); 78 } catch (SystemException se) { 79 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 80 } 81 82 logger.debug("Transaction found = {0}", transaction); 83 84 95 96 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 117 118 EasyBeansSFSB statefulBean = (EasyBeansSFSB) invocationContext.getTarget(); 120 EZBSessionContext sessionContext = (EZBSessionContext) statefulBean.getEasyBeansContext(); 121 Transaction beanTransaction = sessionContext.getBeanTransaction(); 123 if (beanTransaction != null) { 125 try { 126 getTransactionManager().resume(beanTransaction); 127 } catch (InvalidTransactionException ite) { 128 throw new EJBException ( 129 "Cannot call resume() on the previous bean transaction. There is an invalid transaction", ite); 130 } catch (IllegalStateException ise) { 131 throw new EJBException ( 132 "Cannot call resume() on the previous bean transaction. There is another associated transaction", 133 ise); 134 } catch (SystemException se) { 135 throw new EJBException ( 136 "Cannot call resume() on the previous bean transaction. Unexpected error condition", se); 137 } 138 } 139 140 try { 141 return invocationContext.proceed(); 142 } catch (Exception e) { 143 handleBeanManagedException(invocationContext, e); 144 return null; 146 } finally { 147 148 153 Transaction transactionAfter = null; 154 try { 155 transactionAfter = getTransactionManager().getTransaction(); 156 } catch (SystemException se) { 157 throw new EJBException ("Cannot get the current transaction on transaction manager.", se); 158 } 159 if (transactionAfter != null) { 160 int transactionStatus = transactionAfter.getStatus(); 161 if (transactionStatus != STATUS_COMMITTED && transactionStatus != STATUS_ROLLEDBACK) { 163 sessionContext.setBeanTransaction(transactionAfter); 164 } else { 165 sessionContext.setBeanTransaction(null); 166 } 167 } 168 169 173 if (suspendedTransaction != null) { 174 175 logger.debug("Resuming transaction {0}", transaction); 176 177 try { 178 getTransactionManager().resume(suspendedTransaction); 179 } catch (InvalidTransactionException ite) { 180 throw new EJBException ( 181 "Cannot call resume() on the given transaction. There is an invalid transaction", ite); 182 } catch (IllegalStateException ise) { 183 throw new EJBException ( 184 "Cannot call resume() on the given transaction. There is another associated transaction", 185 ise); 186 } catch (SystemException se) { 187 throw new EJBException ("Cannot call resume() on the given transaction. Unexpected error condition", 188 se); 189 } 190 } 191 } 192 } 193 } 194 | Popular Tags |