【问题标题】:Glassfish jdbc/database lookup failedGlassfish jdbc/数据库查找失败
【发布时间】:2014-02-03 21:49:29
【问题描述】:

我希望我不会因为找不到答案而提出重复的问题。 我收到此错误:

javax.naming.NamingException: 在 SerialContext 中查找 'jdbc/osclassDB' 失败

这就是我所做的:我设置了一个 JDBC 连接池 和一个指向该池的 JDBC 资源(都在 Glassfish 中)。

然后我告诉我的 web.xml 有一个 JDBC 资源:

<resource-ref>
    <res-ref-name>jdbc/osclassDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>                
</resource-ref>

然后我尝试在 Servlet 中使用该资源:

  Connection connection = null;

  try {        
     InitialContext initialContext = new InitialContext();
     //Context dbContext = (Context) initialContext.lookup("java:comp/env");

     DataSource dataSource = (DataSource) initialContext.lookup("jdbc/osclassDB");
     connection = dataSource.getConnection();

     if (connection == null) {
         throw new SQLException("Error establishing connection!");
     }
     // some queries here
  } 
  // catch and finally close connection

但是当我调用 Servlet 时,它会抛出 NamingException 并告诉我 Lookup failed for 'jdbc/osclassDB' in SerialContext

我在这里做错了什么?是 web.xml 吗?我错过了什么? 谢谢您的帮助!

【问题讨论】:

    标签: java servlets jdbc glassfish web.xml


    【解决方案1】:

    解决了问题:

    1st 通过添加一个 sun-web.xml 链接 web.xml 中的 资源参考到一个实际的 jndi-name(我在 Glassfish 上设置的那个)。通常,这不应该是必要的(甲骨文说)但我还是这样做了 [编辑: 事实证明,这真的没有必要! ]

    第二次我省略了“jdbc”。在 servletweb.xmlsun-web.xml 中,它现在只称为“osclassDB”(我的资源名称) “jdbc/osclassDB”

    现在看起来像这样:

    web.xml

    <resource-ref>
        <res-ref-name>osclassDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>                
    </resource-ref>
    

    sun-web.xml

    <resource-ref>
        <res-ref-name>osclassDB</res-ref-name>
        <jndi-name>osclassDB</jndi-name>  
    </resource-ref> 
    

    在 servlet 中

    Context dbContext = (Context) initialContext.lookup("java:comp/env");
    DataSource dataSource = (DataSource) dbContext.lookup("osclassDB");
    connection = dataSource.getConnection();
    

    [Edit:] sun-web.xml 真的没必要

    【讨论】:

      猜你喜欢
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多