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 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 TransactionRule method apply.
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
final Transactional annotation2 = description.getAnnotation(Transactional.class);
if (annotation == null && annotation2 == null) {
base.evaluate();
return;
}
final BeanContext beanContext = getBeanContext();
final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
final TransactionType transactionType = TransactionType.get(annotation == null ? TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
beanContext.getMethodContext(method).setTransactionType(transactionType);
ThreadContext tc = ThreadContext.getThreadContext();
final boolean tcCreated;
if (tc == null) {
tcCreated = true;
tc = ThreadContext.enter(new ThreadContext(beanContext, null));
} else {
tcCreated = false;
}
final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType, tc);
try {
base.evaluate();
} finally {
if (rollback) {
policy.setRollbackOnly();
}
EjbTransactionUtil.afterInvoke(policy, tc);
if (tcCreated) {
ThreadContext.exit(tc);
}
}
}
};
}
Aggregations