Search in sources :

Example 1 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class CmpContainer method findEJBObject.

private Object findEJBObject(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 List<Object> results = cmpEngine.queryBeans(callContext, callMethod, args);
        final KeyGenerator kg = beanContext.getKeyGenerator();
        // single ProxyInfo object is returned.
        if (callMethod.getReturnType() == Collection.class || callMethod.getReturnType() == Enumeration.class) {
            final List<ProxyInfo> proxies = new ArrayList<ProxyInfo>();
            for (final Object value : results) {
                final EntityBean bean = (EntityBean) value;
                if (value == null) {
                    proxies.add(null);
                } else {
                    // get the primary key
                    final Object primaryKey = kg.getPrimaryKey(bean);
                    // create a new ProxyInfo based on the deployment info and primary key and add it to the vector
                    proxies.add(new ProxyInfo(beanContext, primaryKey));
                }
            }
            if (callMethod.getReturnType() == Enumeration.class) {
                return new Enumerator(proxies);
            } else {
                return proxies;
            }
        } else {
            if (results.size() != 1) {
                throw new ObjectNotFoundException("A Enteprise bean with deployment_id = " + beanContext.getDeploymentID() + (args != null && args.length >= 1 ? " and primarykey = " + args[0] : "") + " Does not exist");
            }
            // create a new ProxyInfo based on the deployment info and primary key
            final EntityBean bean = (EntityBean) results.get(0);
            if (bean == null) {
                return null;
            } else {
                final Object primaryKey = kg.getPrimaryKey(bean);
                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");
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) FinderException(javax.ejb.FinderException) Enumerator(org.apache.openejb.util.Enumerator) EntityBean(javax.ejb.EntityBean) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) Collection(java.util.Collection) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject)

Example 2 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class CmpContainer method businessMethod.

private Object businessMethod(final Method callMethod, final Method runMethod, 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;
    entrancyTracker.enter(beanContext, callContext.getPrimaryKey());
    try {
        bean = (EntityBean) cmpEngine.loadBean(callContext, callContext.getPrimaryKey());
        if (bean == null) {
            throw new NoSuchObjectException(beanContext.getDeploymentID() + " : " + callContext.getPrimaryKey());
        }
        returnValue = runMethod.invoke(bean, args);
        // when there is not transaction, merge the data from the bean back into the cmp engine
        cmpEngine.storeBeanIfNoTx(callContext, bean);
    } catch (final NoSuchObjectException e) {
        handleApplicationException(txPolicy, e, false);
    } 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 {
        entrancyTracker.exit(beanContext, callContext.getPrimaryKey());
        afterInvoke(txPolicy, callContext);
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) EntityBean(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) NoSuchObjectException(java.rmi.NoSuchObjectException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class JpaCmpEngine method loadBean.

public Object loadBean(final ThreadContext callContext, final Object primaryKey) {
    final TransactionPolicy txPolicy = startTransaction("load", callContext);
    try {
        final BeanContext beanContext = callContext.getBeanContext();
        final Class<?> beanClass = beanContext.getCmpImplClass();
        // Try to load it from the entity manager
        final EntityManager entityManager = getEntityManager(beanContext);
        return entityManager.find(beanClass, primaryKey);
    } finally {
        commitTransaction("load", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)

Example 4 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class JpaCmpEngine method createBean.

public Object createBean(EntityBean bean, final ThreadContext callContext) throws CreateException {
    // TODO verify that extract primary key requires a flush followed by a merge
    final TransactionPolicy txPolicy = startTransaction("persist", callContext);
    creating.get().add(bean);
    try {
        final BeanContext beanContext = callContext.getBeanContext();
        final EntityManager entityManager = getEntityManager(beanContext);
        entityManager.persist(bean);
        entityManager.flush();
        bean = entityManager.merge(bean);
        // extract the primary key from the bean
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);
        return primaryKey;
    } finally {
        creating.get().remove(bean);
        commitTransaction("persist", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) SimpleKeyGenerator(org.apache.openejb.core.cmp.SimpleKeyGenerator) KeyGenerator(org.apache.openejb.core.cmp.KeyGenerator) ComplexKeyGenerator(org.apache.openejb.core.cmp.ComplexKeyGenerator) Cmp2KeyGenerator(org.apache.openejb.core.cmp.cmp2.Cmp2KeyGenerator)

Example 5 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class JpaCmpEngine method removeBean.

public void removeBean(final ThreadContext callContext) {
    final TransactionPolicy txPolicy = startTransaction("remove", callContext);
    try {
        final BeanContext deploymentInfo = callContext.getBeanContext();
        final Class<?> beanClass = deploymentInfo.getCmpImplClass();
        final EntityManager entityManager = getEntityManager(deploymentInfo);
        final Object primaryKey = callContext.getPrimaryKey();
        // Try to load it from the entity manager
        final Object bean = entityManager.find(beanClass, primaryKey);
        // remove the bean
        entityManager.remove(bean);
    } finally {
        commitTransaction("remove", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject)

Aggregations

TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)38 BeanContext (org.apache.openejb.BeanContext)26 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)17 Method (java.lang.reflect.Method)14 EJBLocalObject (javax.ejb.EJBLocalObject)11 EJBObject (javax.ejb.EJBObject)11 EntityBean (javax.ejb.EntityBean)11 ThreadContext (org.apache.openejb.core.ThreadContext)10 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)10 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)10 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)10 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)10 ApplicationException (org.apache.openejb.ApplicationException)8 SystemInstance (org.apache.openejb.loader.SystemInstance)7 OpenEJBException (org.apache.openejb.OpenEJBException)6 ProxyInfo (org.apache.openejb.ProxyInfo)6 SystemException (org.apache.openejb.SystemException)6 Operation (org.apache.openejb.core.Operation)6 SuspendedTransaction (org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5