use of org.apache.openejb.core.Operation in project tomee by apache.
the class EntityContainer method ejbStore_If_No_Transaction.
public void ejbStore_If_No_Transaction(final ThreadContext callContext, final EntityBean bean) throws Exception {
final Operation currentOp = callContext.getCurrentOperation();
if (currentOp == Operation.BUSINESS) {
final TransactionPolicy callerTxPolicy = callContext.getTransactionPolicy();
if (callerTxPolicy != null && callerTxPolicy.isTransactionActive()) {
return;
}
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = beanContext.getTransactionPolicyFactory().createTransactionPolicy(TransactionType.Supports);
try {
// double check we don't have an active transaction
if (!txPolicy.isTransactionActive()) {
callContext.setCurrentOperation(Operation.STORE);
bean.ejbStore();
}
} catch (final Exception e) {
instanceManager.discardInstance(callContext, bean);
throw e;
} finally {
callContext.setCurrentOperation(currentOp);
txPolicy.commit();
}
}
}
use of org.apache.openejb.core.Operation in project tomee by apache.
the class EntityInstanceManager method freeInstance.
public void freeInstance(final ThreadContext callContext, final EntityBean bean) throws SystemException {
discardInstance(callContext, bean);
final Operation currentOp = callContext.getCurrentOperation();
callContext.setCurrentOperation(Operation.UNSET_CONTEXT);
try {
/*
* unsetEntityContext executes in an unspecified transactional context. In this case we choose to
* allow it to have what every transaction context is current. Better then suspending it
* unnecessarily.
*
* We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
* logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
* we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
* to do.
*/
bean.unsetEntityContext();
} catch (final Exception e) {
/*
* The EJB 1.1 specification does not specify how exceptions thrown by unsetEntityContext impact the
* transaction, if there is one. In this case we choose to do nothing since the instance is being disposed
* of anyway.
*/
logger.info(getClass().getName() + ".freeInstance: ignoring exception " + e + " on bean instance " + bean);
} finally {
callContext.setCurrentOperation(currentOp);
}
}
use of org.apache.openejb.core.Operation in project tomee by apache.
the class EjbTransactionUtil method handleSystemException.
/**
* Performs EJB rules when a system exception occurs.
*/
public static void handleSystemException(final TransactionPolicy txPolicy, final Throwable sysException, final ThreadContext callContext) throws InvalidateReferenceException {
// Log the system exception or error
Operation operation = null;
if (callContext != null) {
operation = callContext.getCurrentOperation();
}
if (operation != null) {
logger.error("EjbTransactionUtil.handleSystemException: " + sysException.getMessage(), sysException);
} else {
logger.debug("EjbTransactionUtil.handleSystemException: " + sysException.getMessage(), sysException);
}
// Mark the transaction for rollback
txPolicy.setRollbackOnly(sysException);
// Throw InvalidateReferenceException
if (txPolicy.isClientTransaction()) {
// using caller's transaction
final String message = "The transaction has been marked rollback only because the bean encountered a non-application exception :" + sysException.getClass().getName() + " : " + sysException.getMessage();
final TransactionRolledbackException txException = new TransactionRolledbackException(message, sysException);
throw new InvalidateReferenceException(txException);
} else {
// no transaction or in a new transaction for this method call
final RemoteException re = new RemoteException("The bean encountered a non-application exception", sysException);
throw new InvalidateReferenceException(re);
}
}
use of org.apache.openejb.core.Operation in project tomee by apache.
the class StatelessContainer method _invoke.
@SuppressWarnings("ThrowFromFinallyBlock")
private Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
Object returnValue = null;
try {
if (type == InterfaceType.SERVICE_ENDPOINT) {
callContext.setCurrentOperation(Operation.BUSINESS_WS);
returnValue = invokeWebService(args, beanContext, runMethod, instance);
} else {
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final Operation operation = type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS;
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, operation, interceptors, instance.interceptors);
returnValue = interceptorStack.invoke(args);
}
} catch (final Throwable re) {
// handle reflection exception
final ExceptionType exceptionType = beanContext.getExceptionType(re);
if (exceptionType == ExceptionType.SYSTEM) {
/* System Exception ****************************/
// The bean instance is not put into the pool via instanceManager.poolInstance
// and therefore the instance will be garbage collected and destroyed.
// In case of StrictPooling flag being set to true we also release the semaphore
// in the discardInstance method of the instanceManager.
callContext.setDiscardInstance(true);
handleSystemException(txPolicy, re, callContext);
} else {
/* Application Exception ***********************/
handleApplicationException(txPolicy, re, exceptionType == ExceptionType.APPLICATION_ROLLBACK);
}
} finally {
try {
afterInvoke(txPolicy, callContext);
} catch (final SystemException | RuntimeException e) {
callContext.setDiscardInstance(true);
throw e;
}
}
return returnValue;
}
use of org.apache.openejb.core.Operation in project tomee by apache.
the class MdbInstanceFactory method freeInstance.
/**
* Frees an instance no longer needed by the resource adapter. This method makes all the necessary lifecycle
* callbacks and decrements the instance count. This method should not be used to disposed of beans that have
* thrown a system exception. Instead the discardInstance method should be called.
*
* @param instance the bean instance to free
* @param ignoredInstanceCount
*/
public void freeInstance(final Instance instance, final boolean ignoredInstanceCount) {
if (instance == null) {
throw new NullPointerException("bean is null");
}
// decrement the instance count
if (!ignoredInstanceCount) {
synchronized (this) {
instanceCount--;
}
}
final ThreadContext callContext = ThreadContext.getThreadContext();
final Operation originalOperation = callContext == null ? null : callContext.getCurrentOperation();
final BaseContext.State[] originalAllowedStates = callContext == null ? null : callContext.getCurrentAllowedStates();
try {
// call post destroy method
if (callContext != null) {
callContext.setCurrentOperation(Operation.PRE_DESTROY);
}
final Method remove = instance.bean instanceof MessageDrivenBean ? MessageDrivenBean.class.getMethod("ejbRemove") : null;
final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
interceptorStack.invoke();
if (instance.creationalContext != null) {
instance.creationalContext.release();
}
} catch (final Throwable re) {
MdbInstanceFactory.logger.error("The bean instance " + instance.bean + " threw a system exception:" + re, re);
} finally {
if (callContext != null) {
callContext.setCurrentOperation(originalOperation);
callContext.setCurrentAllowedStates(originalAllowedStates);
}
}
}
Aggregations