Search in sources :

Example 21 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class JtaTransactionPolicy method beginTransaction.

protected Transaction beginTransaction() throws SystemException {
    final Transaction transaction;
    try {
        transactionManager.begin();
        transaction = transactionManager.getTransaction();
    } catch (final Exception e) {
        txLogger.error("The Transaction Manager has encountered an unexpected error condition while attempting to begin a new transaction: {0}", e.getMessage());
        throw new SystemException(e);
    }
    if (transaction == null) {
        throw new SystemException("Failed to begin a new transaction");
    }
    txLogger.debug("TX {0}: Started transaction {1}", transactionType, transaction);
    return transaction;
}
Also used : Transaction(javax.transaction.Transaction) SystemException(org.apache.openejb.SystemException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) RemoteException(java.rmi.RemoteException) SystemException(org.apache.openejb.SystemException) RollbackException(javax.transaction.RollbackException) ApplicationException(org.apache.openejb.ApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 22 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class RAFPassivater method passivate.

@Override
public synchronized void passivate(final Map stateTable) throws SystemException {
    try (final RandomAccessFile ras = new RandomAccessFile(JavaSecurityManagers.getSystemProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation" + fileID + ".ser", "rw")) {
        fileID++;
        final Iterator iterator = stateTable.keySet().iterator();
        Pointer lastPointer = null;
        while (iterator.hasNext()) {
            final Object id = iterator.next();
            final Object obj = stateTable.get(id);
            final byte[] bytes = Serializer.serialize(obj);
            final long filepointer = ras.getFilePointer();
            if (lastPointer == null) {
                lastPointer = new Pointer(fileID, filepointer, (int) filepointer);
            } else {
                lastPointer = new Pointer(fileID, filepointer, (int) (filepointer - lastPointer.filepointer));
            }
            masterTable.put(id, lastPointer);
            ras.write(bytes);
        }
    } catch (final Exception e) {
        throw new SystemException(e);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) SystemException(org.apache.openejb.SystemException) Iterator(java.util.Iterator) SystemException(org.apache.openejb.SystemException)

Example 23 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class SimplePassivater method activate.

@Override
public Object activate(final Object primaryKey) throws SystemException {
    try {
        final String filename = primaryKey.toString().replace(':', '=');
        final File sessionFile = new File(sessionDirectory, filename);
        if (sessionFile.exists()) {
            logger.info("Activating from file " + sessionFile);
            try (final InputStream source = IO.read(sessionFile);
                final ObjectInputStream ois = new EjbObjectInputStream(source)) {
                return ois.readObject();
            } finally {
                if (!sessionFile.delete()) {
                    sessionFile.deleteOnExit();
                }
            }
        } else {
            logger.info("Activation failed: file not found " + sessionFile);
            return null;
        }
    } catch (final Exception t) {
        logger.info("Activation failed ", t);
        throw new SystemException(t);
    }
}
Also used : SystemException(org.apache.openejb.SystemException) ObjectInputStream(java.io.ObjectInputStream) EjbObjectInputStream(org.apache.openejb.core.ivm.EjbObjectInputStream) InputStream(java.io.InputStream) EjbObjectInputStream(org.apache.openejb.core.ivm.EjbObjectInputStream) File(java.io.File) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) SystemException(org.apache.openejb.SystemException) ObjectInputStream(java.io.ObjectInputStream) EjbObjectInputStream(org.apache.openejb.core.ivm.EjbObjectInputStream)

Example 24 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class EntityInstanceManager method getPooledInstance.

protected EntityBean getPooledInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Stack methodReadyPool = poolMap.get(beanContext.getDeploymentID());
    if (methodReadyPool == null) {
        throw new SystemException("Invalid deployment id " + beanContext.getDeploymentID() + " for this container");
    }
    EntityBean bean = (EntityBean) methodReadyPool.pop();
    if (bean == null) {
        try {
            bean = (EntityBean) beanContext.getBeanClass().newInstance();
        } catch (final Exception e) {
            logger.error("Bean instantiation failed for class " + beanContext.getBeanClass(), e);
            throw new SystemException(e);
        }
        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.SET_CONTEXT);
        try {
            /*
                * setEntityContext executes in an unspecified transactional context. In this case we choose to
                * allow it to have what every transaction context is current. Better then suspending it
                * unnecessarily.
                *
                * We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
                * logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
                * we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
                * to do.
                */
            bean.setEntityContext(createEntityContext());
        } catch (final Exception e) {
            /*
                * The EJB 1.1 specification does not specify how exceptions thrown by setEntityContext impact the
                * transaction, if there is one.  In this case we choose the least disruptive operation, throwing an
                * application exception and NOT automatically marking the transaciton for rollback.
                */
            logger.error("Bean callback method failed ", e);
            throw new ApplicationException(e);
        } finally {
            callContext.setCurrentOperation(currentOp);
        }
    } else {
        reusingBean(bean, callContext);
    }
    if (callContext.getCurrentOperation() == Operation.BUSINESS || callContext.getCurrentOperation() == Operation.REMOVE) {
        /*
            * When a bean is retrieved from the bean pool to service a client's business method request it must be
            * notified that its about to enter service by invoking its ejbActivate( ) method. A bean instance
            * does not have its ejbActivate() invoked when:
            * 1. Its being retreived to service an ejbCreate()/ejbPostCreate().
            * 2. Its being retrieved to service an ejbFind method.
            * 3. Its being retrieved to service an ejbRemove() method.
            * See section 9.1.4 of the EJB 1.1 specification.
            */
        final Operation currentOp = callContext.getCurrentOperation();
        callContext.setCurrentOperation(Operation.ACTIVATE);
        try {
            /*
                In the event of an exception, OpenEJB is required to log the exception, evict the instance,
                and mark the transaction for rollback.  If there is a transaction to rollback, then the a
                javax.transaction.TransactionRolledbackException must be throw to the client.
                See EJB 1.1 specification, section 12.3.2
                */
            bean.ejbActivate();
        } catch (final Throwable e) {
            logger.error("Encountered exception during call to ejbActivate()", e);
            final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
            if (txPolicy != null && txPolicy.isTransactionActive()) {
                txPolicy.setRollbackOnly(e);
                throw new ApplicationException(new TransactionRolledbackException("Reflection exception thrown while attempting to call ejbActivate() on the instance", e));
            }
            throw new ApplicationException(new RemoteException("Exception thrown while attempting to call ejbActivate() on the instance. Exception message = " + e.getMessage(), e));
        } finally {
            callContext.setCurrentOperation(currentOp);
        }
    }
    return bean;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) EntityBean(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Operation(org.apache.openejb.core.Operation) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) RemoteException(java.rmi.RemoteException) 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) LinkedListStack(org.apache.openejb.util.LinkedListStack) Stack(org.apache.openejb.util.Stack)

Example 25 with SystemException

use of org.apache.openejb.SystemException in project tomee by apache.

the class ManagedContainer method obtainInstance.

private Instance obtainInstance(final Object primaryKey, final ThreadContext callContext) throws OpenEJBException {
    if (primaryKey == null) {
        throw new SystemException(new NullPointerException("Cannot obtain an instance of the stateful session bean with a null session id"));
    }
    final Transaction currentTransaction = getTransaction(callContext);
    // Find the instance
    Instance instance = checkedOutInstances.get(primaryKey);
    if (instance == null) {
        try {
            instance = cache.checkOut(primaryKey);
        } catch (final OpenEJBException e) {
            throw e;
        } catch (final Exception e) {
            throw new SystemException("Unexpected load exception", e);
        }
        // Did we find the instance?
        if (instance == null) {
            throw new InvalidateReferenceException(new NoSuchObjectException("Not Found"));
        }
        // remember instance until it is returned to the cache
        checkedOutInstances.put(primaryKey, instance);
    }
    synchronized (this) {
        if (instance.isInUse()) {
            // the bean is already being invoked; the only reentrant/concurrent operations allowed are Session synchronization callbacks
            final Operation currentOperation = callContext.getCurrentOperation();
            if (currentOperation != Operation.AFTER_COMPLETION && currentOperation != Operation.BEFORE_COMPLETION) {
                throw new ApplicationException(new RemoteException("Concurrent calls not allowed."));
            }
        }
        if (instance.getTransaction() != null) {
            if (!instance.getTransaction().equals(currentTransaction) && !instance.getLock().tryLock()) {
                throw new ApplicationException(new RemoteException("Instance is in a transaction and cannot be invoked outside that transaction.  See EJB 3.0 Section 4.4.4"));
            }
        } else {
            instance.setTransaction(currentTransaction);
        }
        // Mark the instance in use so we can detect reentrant calls
        instance.setInUse(true);
        return instance;
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) Transaction(javax.transaction.Transaction) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) EjbUserTransaction(org.apache.openejb.core.transaction.EjbUserTransaction) SystemInstance(org.apache.openejb.loader.SystemInstance) NoSuchObjectException(java.rmi.NoSuchObjectException) Operation(org.apache.openejb.core.Operation) RemoteException(java.rmi.RemoteException) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

SystemException (org.apache.openejb.SystemException)37 ApplicationException (org.apache.openejb.ApplicationException)16 OpenEJBException (org.apache.openejb.OpenEJBException)12 BeanContext (org.apache.openejb.BeanContext)10 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)9 RemoteException (java.rmi.RemoteException)7 ThreadContext (org.apache.openejb.core.ThreadContext)7 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)7 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)6 Operation (org.apache.openejb.core.Operation)6 EJBException (javax.ejb.EJBException)5 NamingException (javax.naming.NamingException)5 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)5 File (java.io.File)4 IOException (java.io.IOException)4 NotSerializableException (java.io.NotSerializableException)4 RandomAccessFile (java.io.RandomAccessFile)4 EJBAccessException (javax.ejb.EJBAccessException)4 UnavailableException (javax.resource.spi.UnavailableException)4 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)4