@Override
public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
logger.debug("Calling RequiresNew 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);
/*
* If a client calls with a transaction context, the container
* suspends the association of the transaction context with the
* current thread before starting the new transaction and invoking
* the business method. The container resumes the suspended
* transaction association after the business method and the new
* transaction have been completed.
*/
// Existing transaction
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);
}
}
/*
* If the client invokes the enterprise bean's method while the client
* is not associated with a transaction context, the container
* automatically starts a new transaction before delegating a method
* call to the enterprise bean business method. The container
* automatically enlists all the resource managers accessed by the
* business method with the transaction. If the business method invokes
* other enterprise beans, the container passes the transaction context
* with the invocation. The container attempts to commit the transaction
* when the business method has completed. The container performs the
* commit protocol before the method result is sent to the client.
*/
try {
getTransactionManager().begin();
} catch (NotSupportedException nse) {
throw new EJBException("Transaction Manager implementation does not support nested transactions.", nse);
} catch (SystemException se) {
throw new EJBException("Cannot call begin() on the transaction manager.", se);
}
boolean gotBusinessException = false;
try {
return invocationContext.proceed();
} catch (Exception e) {
gotBusinessException = true;
handleContextContainerTransaction(invocationContext, e);
// Shouldn't come here
return null;
} finally {
// only do some operations if transaction has been started before
// invoking the method.
if (!gotBusinessException) {
// sanity check.
Transaction transactionAfter = null;
try {
transactionAfter = getTransactionManager().getTransaction();
} catch (SystemException se) {
throw new EJBException("Cannot get the current transaction on transaction manager.", se);
}