简体   繁体   中英

handling EJBException in client code

I've been reading about EJB exception handling and this is my understanding so far:

Assume Bean B (server) transaction attribute is REQUIRED:

  • System exceptions should be thrown as EJBException to the caller.
  • Container intercepts EJBException (or any other unchecked exception) wraps it and throws it to the caller
  • if the caller is running within a transaction the bean instance gets discarded and the transaction gets rolled back, the caller gets the EJBException wrapped inside an EJBTransactionRolledBackException.
  • if caller is not in a transaction the bean instance gets discarded, the caller gets the EJBException wrapped inside a RemoteException.

When I run it with caller trans. attribute set to REQUIRED I do get the EJBException wrapped inside an EJBTransactionRolledBackException.

When I run it with caller trans. attribute set to NEVER, I catch the original EJBException in the caller, not wrapped in a RemoteException

Why am I not getting the RemoteException in the second case?

Here's a simplified view of my caller and server beans:

    @Stateless
    public class BeanA implements BeanAInterface{

    @Override
    public void callBeanB() {

    BeanBInterface beanBInterface;
    try{
     beanBInterface = (BeanBInterface) new InitialContext().lookup(BeanBInterface.class.getName()); 
     beanBInterface.getResponse();
    } catch (Exception e){
         log.error("Caught Exception: " + e);
      }
     }
    }




    @Stateless
    public class BeanB implements BeanBInterface{

     @Override
     public void getResponse(){

         throw EJBException("Catch me.");

     }
    }

EDIT:

I just read here that EJBException is thrown to local clients and RemoteException to remote clients. The caller bean in my example is running in the same server instance as the server bean but it's connecting via the server bean remote interface. I guess I'm not clear on the definition of remote vs local client either but it appears that I'm not getting the RemoteException because my client is not remote.

Get sure that the container is really using the remote and not the local interface.
Maybe you think you are calling the remote, since is the remote you are refering, but the container in fact is "optimizing" for you treating the call as in was local.

I remember I got stuck on something similar, and only after some time I recognized that Jboss was treating remote calls as they were local calls when both EJBs were in the same VM. In my case when I was using the remote interface the container was not using passed-by-value (serialized object), but was passing the reference.

So my advice: be sure you are in a "remote scenario". Try to use two different application servers two jbosses i and see if something changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM