java javax.naming.NameNotFoundException:名称 [comp/env] 在此上下文中未绑定。无法在 Tomcat 中找到 [comp]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15790307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:52:33  来源:igfitidea点击:

javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp] in Tomcat

javatomcat7postgresql-9.2

提问by ryan

I have my code deployed in Tomcat 6.0.32 at my local machine at windows environment. And the same WAR file is deployed in Tomcat 7.0.39 at remote machine at linux environment.

我将我的代码部署在 Tomcat 6.0.32 的 Windows 环境中的本地机器上。并且相同的WAR文件部署在linux环境下远程机器上的Tomcat 7.0.39中。

Code for context.xml is as follows:

context.xml 的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
    <Context antiJARLocking="true" path="/CentralizedPush">
        <Resource auth="Container" driverClassName="org.postgresql.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/postgres" password="commonpush12#" type="javax.sql.DataSource" url="jdbc:postgresql://10.40.5.59:5444/commonpush_database" username="commonpush"/>
    </Context>

The code for DBConnection.java is as follows:

DBConnection.java 的代码如下:

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.slf4j.LoggerFactory;  


public class DBConnection {
    private static Connection con = null;

/**function is used for creating connection
 * @return Connection object
 */
public static Connection getConnection() {
    org.slf4j.Logger LOG = LoggerFactory.getLogger("Error");

    try {
        Driver d = (Driver) Class.forName(Constant.CLASS_NAME).newInstance();
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:comp/env");
        DataSource ds = (DataSource) envContext.lookup("jdbc/postgres");

        con = ds.getConnection();

    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
        LOG.error("ClassNotFoundException" );
    } catch (InstantiationException ex) {
        ex.printStackTrace();
        LOG.error("InstantiationException" );
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        LOG.error("IllegalAccessException" );
    } catch (NamingException ex) {
        ex.printStackTrace();
        LOG.error("NamingException" );
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("SQLException in db connection" );
    }

    return con;
}

I have database in postgres that I am trying to access whose details are as follows:

我在 postgres 中有我试图访问的数据库,其详细信息如下:

Name: CDCUCOMPUDB
Host: 10.40.5.59
Port: 5444
database name: commonpush_database

The problem is when I deploy the code at my local machine everything is fine, but when I deploy the code at remote server which is in Linux environment and has the different Tomcat version I get the following error:

问题是当我在本地机器上部署代码时一切正常,但是当我在 Linux 环境中的远程服务器上部署代码并且具有不同的 Tomcat 版本时,我收到以下错误:

javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].

Kindly suggest any solution.

请提出任何解决方案。

回答by user207421

Do it in a single lookup of "java:comp/env/jdbc/postgres". There's no reason to use two Contexts here.

在“java:comp/env/jdbc/postgres”的单一查找中完成。没有理由在这里使用两个上下文。

回答by Ed Pike

I was having the same error. I was trying to create a global jdbc connection pool, following instructions Tomcat DataSource JNDI Example for Servlet Web Application. In his example code he used "java:/comp/" to look up, not "java:comp/". This is problematic. Otherwise, a very helpful guide.

我有同样的错误。我试图按照Tomcat DataSource JNDI Example for Servlet Web Application 的说明创建一个全局 jdbc 连接池。在他的示例代码中,他使用“java:/comp/”来查找,而不是“java:comp/”。这是有问题的。否则,这是一个非常有用的指南。