@Override
public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
logger.debug("Calling BMT TX interceptor");
// Get current transaction
Transaction transaction;
try {
transaction = getTransactionManager().getTransaction();
} catch (SystemException se) {
throw new EJBException("Cannot get the current transaction on transaction manager.", se);
}
logger.debug("Transaction found = {0}", transaction);
/*
* When a client invokes a business method via one of the enterprise
* bean's client view interfaces, the container suspends any transaction
* that may be associated with the client request. If there is a
* transaction associated with the instance (this would happen if a
* stateful session bean instance started the transaction in some
* previous business method), the container associates the method
* execution with this transaction. If there are interceptor methods
* associated with the bean instances, these actions are taken before
* the interceptor methods are invoked.
*/
Transaction suspendedTransaction = null;
if (transaction != null) {
try {
logger.debug("Suspending transaction {0}", transaction);
suspendedTransaction = getTransactionManager().suspend();
} catch (SystemException se) {
throw new EJBException("Cannot call suspend() on the transaction manager.", se);
}
}
/*
* In the case of a stateful session bean, it is possible that the
* business method that started a transaction completes without
* committing or rolling back the transaction. In such a case, the
* container must retain the association between the transaction and the
* instance across multiple client calls until the instance commits or
* rolls back the transaction. When the client invokes the next business
* method, the container must invoke the business method (and any
* applicable interceptor methods for the bean) in this transaction
* context.
*/
// Get Bean and context
EasyBeansSFSB statefulBean = (EasyBeansSFSB) invocationContext.getTarget();
EZBSessionContext sessionContext = (EZBSessionContext) statefulBean.getEasyBeansContext();
// Get bean transaction (if any)
Transaction beanTransaction = sessionContext.getBeanTransaction();
// resume transaction for the call
if (beanTransaction != null) {
logger.debug("Resuming bean transaction {0}", beanTransaction);
try {
getTransactionManager().resume(beanTransaction);
} catch (InvalidTransactionException ite) {
throw new EJBException(
"Cannot call resume() on the previous bean transaction. There is an invalid transaction", ite);
} catch (IllegalStateException ise) {
throw new EJBException(
"Cannot call resume() on the previous bean transaction. There is another associated transaction",
ise);
} catch (SystemException se) {
throw new EJBException(
"Cannot call resume() on the previous bean transaction. Unexpected error condition", se);
}
}
try {
return invocationContext.proceed();
} catch (Exception e) {
handleBeanManagedException(invocationContext, e);
// Shouldn't come here
return null;
} finally {
/*
* Saves the current bean transaction if the business method that
* started a transaction completes without committing or rolling
* back the transaction.
*/
Transaction transactionAfter = null;
try {
transactionAfter = getTransactionManager().getTransaction();
} catch (SystemException se) {
throw new EJBException("Cannot get the current transaction on transaction manager.", se);
}
if (transactionAfter != null) {
int transactionStatus = transactionAfter.getStatus();
// not yet committed or rollbacked --> save transaction for the next call.
if (transactionStatus != STATUS_COMMITTED && transactionStatus != STATUS_ROLLEDBACK) {
logger.debug("Saving bean transaction {0}", transactionAfter);
sessionContext.setBeanTransaction(transactionAfter);
} else {