Search in sources :

Example 1 with NoSuchEntityException

use of javax.ejb.NoSuchEntityException in project wildfly by wildfly.

the class CMTTxInterceptor method handleInCallerTx.

protected void handleInCallerTx(InterceptorContext invocation, Throwable t, Transaction tx, final EJBComponent component) throws Exception {
    ApplicationExceptionDetails ae = component.getApplicationException(t.getClass(), invocation.getMethod());
    if (ae != null) {
        if (ae.isRollback())
            setRollbackOnly(tx);
        // an app exception can never be an Error
        throw (Exception) t;
    }
    Exception toThrow;
    if (!(t instanceof EJBTransactionRolledbackException)) {
        if (t instanceof Error) {
            toThrow = new EJBTransactionRolledbackException(EjbLogger.ROOT_LOGGER.convertUnexpectedError());
            toThrow.initCause(t);
        } else if (t instanceof NoSuchEJBException || t instanceof NoSuchEntityException) {
            // If this is an NoSuchEJBException, pass through to the caller
            toThrow = (Exception) t;
        } else if (t instanceof RuntimeException) {
            toThrow = new EJBTransactionRolledbackException(t.getMessage(), (Exception) t);
        } else {
            // application exception
            throw (Exception) t;
        }
    } else {
        toThrow = (Exception) t;
    }
    setRollbackOnly(tx);
    throw toThrow;
}
Also used : NoSuchEJBException(javax.ejb.NoSuchEJBException) EJBTransactionRolledbackException(javax.ejb.EJBTransactionRolledbackException) NoSuchEntityException(javax.ejb.NoSuchEntityException) NoSuchEJBException(javax.ejb.NoSuchEJBException) RollbackException(javax.transaction.RollbackException) NoSuchEntityException(javax.ejb.NoSuchEntityException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) EJBTransactionRolledbackException(javax.ejb.EJBTransactionRolledbackException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 2 with NoSuchEntityException

use of javax.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(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) NoSuchObjectException(org.apache.openejb.core.NoSuchObjectException) Operation(org.apache.openejb.core.Operation) NoSuchEntityException(javax.ejb.NoSuchEntityException) OpenEJBException(org.apache.openejb.OpenEJBException) NoSuchObjectException(org.apache.openejb.core.NoSuchObjectException) RemoteException(java.rmi.RemoteException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) SystemException(org.apache.openejb.SystemException) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) ApplicationException(org.apache.openejb.ApplicationException) NoSuchEntityException(javax.ejb.NoSuchEntityException)

Example 3 with NoSuchEntityException

use of javax.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(javax.ejb.NoSuchEntityException) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) EJBAccessException(javax.ejb.EJBAccessException) NoSuchEntityException(javax.ejb.NoSuchEntityException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(org.apache.openejb.SystemException)

Example 4 with NoSuchEntityException

use of javax.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(javax.ejb.EJBException) NoSuchEntityException(javax.ejb.NoSuchEntityException) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource)

Aggregations

NoSuchEntityException (javax.ejb.NoSuchEntityException)4 RemoteException (java.rmi.RemoteException)2 EJBException (javax.ejb.EJBException)2 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 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchObjectException (java.rmi.NoSuchObjectException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 EJBAccessException (javax.ejb.EJBAccessException)1 EJBTransactionRolledbackException (javax.ejb.EJBTransactionRolledbackException)1 EntityBean (javax.ejb.EntityBean)1 NoSuchEJBException (javax.ejb.NoSuchEJBException)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1