EJBException in EJB local calls

Discussions

EJB programming & troubleshooting: EJBException in EJB local calls

  1. EJBException in EJB local calls (1 messages)

    Hi i've been looking everywhere for the answer to the following question and found no answer. I hope, ServerSiders can help me.

    I have 2 EJBs: (1) a statless session bean with remote interfaces, acting as a facade.
                   (2) an entity bean with local interfaces accessed by (1).
    If a system exception occurs in a method from (2) being called by (1), (2) should throw an EJBException (or a subclass). What happens then? Is the EJBException propagated trhough (2) to the client as a RemoteException?
    From what i've read, (1) instance is removed... does the same happen to (2)'s instance?

    Thanks in advance

    Nicolás
  2. Is the EJBException propagated through to the client as a RemoteException?
    The EJB standard does not require that System Exceptions (EJBException or any other Runtime exception) be propogated. What actually happens depends on what server you using. The client will usually get some kind of RemoteException, but there are no guarantees that it will contain any of the information from the original EJBException.

    If you want to propogate error messages from an EJB to the client, you need to use Application Exceptions (any exception that is not a RuntimeException). Application Exceptions are propogated "as is" to the client, but (a) the EJB will not be discarded and (b) the transaction is not rollbacked by default.

    To ensure that transactions are rolled back for Application Exceptions, you must use the Entity/SessionContext's setRollbackOnly() method:

    try {
      // Stuff that might fail ...
    } catch (Exception ex) {
      entityContext.setRollbackOnly();
      throw new ApplicationException();
    }