代码之家  ›  专栏  ›  技术社区  ›  baron5

应为NamingException,得到NoSuchObjectException

  •  0
  • baron5  · 技术社区  · 13 年前

    我在Weblogic 11g上运行Java 6。

    我正在处理一个Web服务项目,该项目使用EJB与数据库进行通信。目前我正在处理错误,因此我尝试在调用web服务之前取消部署EJB。我的EJBClientHelper类如下所示:

    package mypackage.elkom.utils;
    
    import java.io.Serializable;
    import java.util.Properties;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    import mypackage.elkom.ejb.beans.session.remote.ElkomRemote;
    
    public class EJBClientHelper implements Serializable {
    
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
    
      private ElkomRemote elkomRemote;
    
      private static final String ELKOM_JNDI =   "ElkomBean#mypackage.ejb.beans.session.remote.ElkomRemote";
    
      private Context ctx;
    
      private void prepareEjb3Connection() throws PropsFileException, NamingException {
          // Here's code for getting the ejbProviderURL from propsfile //
          props.put("java.naming.factory.initial", weblogic.jndi.WLInitialContextFactory");
          props.put("java.naming.provider.url",ejbProviderURL);
          ctx = new InitialContext(props);
    
     }
    
      public void setElkomRemote(ElkomRemote elkomRemote) {
        this.elkomRemote = elkomRemote;
      }
    
      public ElkomRemote getElkomRemote() throws NamingException, PropsFileException {
        prepareEjb3Connection();
        if(elkomRemote == null) {
        elkomRemote = (ElkomRemote)ctx.lookup(ELKOM_JNDI);
       }
       return elkomRemote;
      }
    
    }
    

    但是,当我使用getElkomRemote()时;我收到的消息不是NamingException:

    SEVERE: The object identified by: '678' could not be found.  Either it was has not been exported or it has been collected by the distributed garbage collector.; 
    nested exception is: java.rmi.NoSuchObjectException: The object identified by: '678' could not be found.  
    Either it was has not been exported or it has been collected by the distributed garbage collector.
    javax.ejb.EJBException: The object identified by: '678' could not be found.  Either it was has not been exported or it has been collected by the distributed garbage collector.; 
    nested exception is: java.rmi.NoSuchObjectException: The object identified by: '678' could not be found.  
    Either it was has not been exported or it has been collected by the distributed garbage collector.
    java.rmi.NoSuchObjectException: The object identified by: '678' could not be found.  Either it was has not been exported or it has been collected by the distributed garbage collector.
    

    有人知道我为什么收到这个消息而不是NamingException吗?也许该怎么解决呢?

    1 回复  |  直到 13 年前
        1
  •  1
  •   Apostolos Emmanouilidis    13 年前

    有三种例外类别:

    • 系统异常
    • JVM异常
    • 应用程序异常

    系统异常可能是由于代码错误或配置错误的资源(如配置错误的JNDI查找)而发生的。如果企业bean面临系统错误,它通常会抛出javax.ejb.EJBException。容器将EJBExcession封装在RemoteException中,最后将RemoteException传递回客户端。这就是您的案例中发生的情况,也是您收到RemoteException的原因。

    无法从客户端应用程序处理系统异常,应将其作为未检查的异常抛出。

    有关更多详细信息,请参阅以下文章:

    我希望这对你有帮助。

    推荐文章