use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.
the class CmpContainer method homeMethod.
private Object homeMethod(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
final EntityBean bean;
Object returnValue = null;
try {
/*
Obtain a bean instance from the method ready pool
*/
bean = createNewInstance(callContext);
// set the entity context
setEntityContext(bean);
try {
callContext.setCurrentOperation(Operation.HOME);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
try {
returnValue = runMethod.invoke(bean, args);
} catch (final IllegalArgumentException e) {
System.out.println("********************************************************");
System.out.println("callMethod = " + callMethod);
System.out.println("runMethod = " + runMethod);
System.out.println("bean = " + bean.getClass().getName());
throw e;
}
} finally {
unsetEntityContext(bean);
}
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
final ExceptionType type = callContext.getBeanContext().getExceptionType(e);
if (type == ExceptionType.SYSTEM) {
/* System Exception ****************************/
handleSystemException(txPolicy, e, callContext);
} else {
/* Application Exception ***********************/
handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
}
} finally {
afterInvoke(txPolicy, callContext);
}
return returnValue;
}
use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.
the class CmpContainer method removeEJBObject.
private void removeEJBObject(final Method callMethod, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
try {
final EntityBean entityBean = (EntityBean) cmpEngine.loadBean(callContext, callContext.getPrimaryKey());
if (entityBean == null) {
throw new NoSuchObjectException(callContext.getBeanContext().getDeploymentID() + " " + callContext.getPrimaryKey());
}
ejbRemove(entityBean);
cmpEngine.removeBean(callContext);
} catch (final NoSuchObjectException e) {
handleApplicationException(txPolicy, e, false);
} catch (final Throwable e) {
// handle reflection exception
handleSystemException(txPolicy, e, callContext);
} finally {
afterInvoke(txPolicy, callContext);
}
}
use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.
the class CmpContainer method findByPrimaryKey.
private Object findByPrimaryKey(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
try {
final EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext, args[0]);
if (bean == null) {
throw new ObjectNotFoundException(beanContext.getDeploymentID() + " : " + args[0]);
}
// rebuild the primary key
final KeyGenerator kg = beanContext.getKeyGenerator();
final Object primaryKey = kg.getPrimaryKey(bean);
// create a new ProxyInfo based on the deployment info and primary key
return new ProxyInfo(beanContext, primaryKey);
} catch (final FinderException fe) {
handleApplicationException(txPolicy, fe, false);
} catch (final Throwable e) {
// handle reflection exception
handleSystemException(txPolicy, e, callContext);
} finally {
afterInvoke(txPolicy, callContext);
}
throw new AssertionError("Should not get here");
}
use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.
the class JpaCmpEngine method storeBeanIfNoTx.
public void storeBeanIfNoTx(final ThreadContext callContext, final Object bean) {
final TransactionPolicy callerTxPolicy = callContext.getTransactionPolicy();
if (callerTxPolicy != null && callerTxPolicy.isTransactionActive()) {
return;
}
final TransactionPolicy txPolicy = startTransaction("store", callContext);
try {
// only store if we started a new transaction
if (txPolicy.isNewTransaction()) {
final EntityManager entityManager = getEntityManager(callContext.getBeanContext());
entityManager.merge(bean);
}
} finally {
commitTransaction("store", callContext, txPolicy);
}
}
use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.
the class EntityContainer method ejbLoad_If_No_Transaction.
public void ejbLoad_If_No_Transaction(final ThreadContext callContext, final EntityBean bean) throws Exception {
final Operation orginalOperation = callContext.getCurrentOperation();
if (orginalOperation == Operation.BUSINESS || orginalOperation == Operation.REMOVE) {
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.LOAD);
bean.ejbLoad();
}
} catch (final NoSuchEntityException e) {
instanceManager.discardInstance(callContext, bean);
throw new ApplicationException(new NoSuchObjectException("Entity not found: " + callContext.getPrimaryKey()));
} catch (final Exception e) {
instanceManager.discardInstance(callContext, bean);
throw e;
} finally {
callContext.setCurrentOperation(orginalOperation);
txPolicy.commit();
}
}
}
Aggregations