Search in sources :

Example 1 with NoSuchEntityException

use of jakarta.ejb.NoSuchEntityException 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();
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) NoSuchObjectException(java.rmi.NoSuchObjectException) Operation(org.apache.openejb.core.Operation) NoSuchEntityException(jakarta.ejb.NoSuchEntityException) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) EJBAccessException(jakarta.ejb.EJBAccessException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchEntityException(jakarta.ejb.NoSuchEntityException) SystemException(org.apache.openejb.SystemException)

Example 2 with NoSuchEntityException

use of jakarta.ejb.NoSuchEntityException in project tomee by apache.

the class EntityInstanceManager method obtainInstance.

public EntityBean obtainInstance(final ThreadContext callContext) throws OpenEJBException {
    // primary key is null if its a servicing a home methods (create, find, ejbHome)
    final Object primaryKey = callContext.getPrimaryKey();
    final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
    if (callContext.getPrimaryKey() != null && txPolicy != null && txPolicy.isTransactionActive()) {
        final Key key = new Key(callContext.getBeanContext().getDeploymentID(), primaryKey);
        SynchronizationWrapper wrapper = (SynchronizationWrapper) txPolicy.getResource(key);
        if (wrapper != null) {
            if (!wrapper.isAssociated()) {
                /*
                    * If the bean identity was removed (via ejbRemove()) within the same transaction,
                    * then it's SynchronizationWrapper will be in the txReady pool but marked as disassociated.
                    * This allows us to prevent a condition where the caller removes the bean and then attempts to
                    * call a business method on that bean within the same transaction.  After a bean is removed any
                    * subsequent invocations on that bean with the same transaction should throw a NoSuchEntityException.
                    * its likely that the application server would have already made the reference invalid, but this bit of
                    * code is an extra precaution.
                    */
                throw new InvalidateReferenceException(new NoSuchObjectException("Entity not found: " + primaryKey));
            } else if (callContext.getCurrentOperation() == Operation.REMOVE) {
                /*
                    *  To avoid calling ejbStore( ) on a bean that after its removed, we can not delegate
                    *  the wrapper is marked as disassociated from the transaction to avoid processing the
                    *  beforeCompletion( ) method on the SynchronizationWrapper object.
                    */
                wrapper.disassociate();
            }
            if (wrapper.isAvailable() || wrapper.primaryKey.equals(primaryKey)) {
                return wrapper.getEntityBean();
            } else {
                // its simpler to implement.
                return wrapper.getEntityBean();
            }
        } else {
            /*
                * If no synchronized wrapper for the key exists
                * Then the bean entity is being access by this transaction for the first time,
                * so it needs to be enrolled in the transaction.
                */
            final EntityBean bean = getPooledInstance(callContext);
            wrapper = new SynchronizationWrapper(callContext.getBeanContext(), primaryKey, bean, false, key, txPolicy);
            if (callContext.getCurrentOperation() == Operation.REMOVE) {
                /*
                    *  To avoid calling ejbStore( ) on a bean that after its removed, we can not delegate
                    *  the wrapper is marked as disassociated from the transaction to avoid processing the
                    *  beforeCompletion( ) method on the SynchronizationWrapper object.
                    *
                    *  We have to still use a wrapper so we can detect when a business method is called after
                    *  a ejbRemove() and act to prevent it from being processed.
                    */
                wrapper.disassociate();
            }
            txPolicy.registerSynchronization(wrapper);
            loadingBean(bean, callContext);
            final Operation orginalOperation = callContext.getCurrentOperation();
            callContext.setCurrentOperation(Operation.LOAD);
            try {
                bean.ejbLoad();
            } catch (final NoSuchEntityException e) {
                wrapper.disassociate();
                throw new InvalidateReferenceException(new NoSuchObjectException("Entity not found: " + primaryKey, e));
            } catch (final Exception e) {
                logger.error("Exception encountered during ejbLoad():", e);
                // djencks not sure about this dissociate call
                wrapper.disassociate();
                throw new OpenEJBException(e);
            } finally {
                callContext.setCurrentOperation(orginalOperation);
            }
            txPolicy.putResource(key, wrapper);
            return bean;
        }
    } else {
        // returned to the pool -- depending on if the tx is a client initiated or container initiated.
        return getPooledInstance(callContext);
    }
}
Also used : InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) OpenEJBException(org.apache.openejb.OpenEJBException) EntityBean(jakarta.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) NoSuchObjectException(org.apache.openejb.core.NoSuchObjectException) Operation(org.apache.openejb.core.Operation) NoSuchEntityException(jakarta.ejb.NoSuchEntityException) OpenEJBException(org.apache.openejb.OpenEJBException) NoSuchObjectException(org.apache.openejb.core.NoSuchObjectException) RemoteException(java.rmi.RemoteException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) NoSuchEntityException(jakarta.ejb.NoSuchEntityException) SystemException(org.apache.openejb.SystemException) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) ApplicationException(org.apache.openejb.ApplicationException)

Example 3 with NoSuchEntityException

use of jakarta.ejb.NoSuchEntityException in project tomee by apache.

the class BasicBmpBean method ejbLoad.

// 
// Remote interface methods
// =============================
// ================================
// EntityBean interface methods
// 
/**
 * A container invokes this method to instruct the
 * instance to synchronize its state by loading it state from the
 * underlying database.
 */
public void ejbLoad() throws EJBException, RemoteException {
    try {
        final InitialContext jndiContext = new InitialContext();
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
        final Connection con = ds.getConnection();
        try {
            final PreparedStatement stmt = con.prepareStatement("select * from entity where id = ?");
            try {
                stmt.setInt(1, primaryKey);
                final ResultSet rs = stmt.executeQuery();
                if (!rs.next()) {
                    throw new NoSuchEntityException("" + primaryKey);
                }
                lastName = rs.getString("last_name");
                firstName = rs.getString("first_name");
                if (rs.next()) {
                    throw new EJBException("Found more than one entity with id " + primaryKey);
                }
            } finally {
                stmt.close();
            }
        } finally {
            con.close();
        }
    } catch (final NamingException | SQLException e) {
        throw new EJBException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) NamingException(javax.naming.NamingException) EJBException(jakarta.ejb.EJBException) NoSuchEntityException(jakarta.ejb.NoSuchEntityException) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource)

Aggregations

NoSuchEntityException (jakarta.ejb.NoSuchEntityException)3 ApplicationException (org.apache.openejb.ApplicationException)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 SystemException (org.apache.openejb.SystemException)2 Operation (org.apache.openejb.core.Operation)2 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)2 EJBAccessException (jakarta.ejb.EJBAccessException)1 EJBException (jakarta.ejb.EJBException)1 EntityBean (jakarta.ejb.EntityBean)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchObjectException (java.rmi.NoSuchObjectException)1 RemoteException (java.rmi.RemoteException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 BeanContext (org.apache.openejb.BeanContext)1