javax.naming.NameNotFoundException : Name [jdbc/FsEDBUser] is not bound in this Context

标签 java tomcat jdbc tomcat8

我之前已经设置过 JNDI 资源,但我遇到了一个问题,我不确定如何在我的 apache-tomcat-8.0.36 服务器上纠正。

我的 context.xml 文件包含以下内容:

<ResourceLink name="jdbc/FsEDBAdmin" global="jdbc/FsEDBAdmin" type="javax.sql.DataSource" />
<ResourceLink name="jdbc/FsEDBUser" global="jdbc/FsEDBUser" type="javax.sql.DataSource" />
<Resource name="jdbc/FsEDBAdmin" auth="Container"
          type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://location"
          username="user_admin" password="pass"
          maxActive="20" maxIdle="10" maxWait="-1"/>

<Resource name="jdbc/FsEDBUser" auth="Container"
          type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://location"
          username="user_user" password="pass"
          maxActive="20" maxIdle="10" maxWait="-1"/>

我的web.xml:

<resource-ref>
    <description>Admin Connection</description>
    <res-ref-name>jdbc/FsEDBAdmin</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
    <description>User Connection</description>
    <res-ref-name>jdbc/FsEDBUser</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

我还使用这些连接来定义用于身份验证的Realm: server.xml:

<Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
         resources under the key "UserDatabase".  Any edits
         that are performed against this UserDatabase are immediately
         available for use by the Realm.  -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase"/>
    <Realm className="org.apache.catalina.realm.DataSourceRealm"
        digest="digest_method" dataSourceName="jdbc/FsEDBAdmin" userTable="schema.table_name"
        userNameCol="name_col" userCredCol="pass_col"
        userRoleTable="schema.table_name" roleNameCol="rolename_col"
        localDataSource="true"/>
  </Realm>

但是当我启动我的应用程序时,我收到标题中提到的错误。如果需要,我可以提供完整的堆栈跟踪。

我想补充一点,这在某一点上是有效的,最近的变化是使用Realm进行身份验证。显然,我在一个或另一个位置定义这些资源时犯了一个错误,因此,如果有另一双眼睛告诉我在哪里,我将不胜感激。谢谢。

编辑:这就是我调用资源的方式:

import path.to.CommonTools;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;

/**
 *
 * @author Michael Potts
 */
public enum Resource {
    INSTANCE;

private static BasicDataSource basicAdmin = null;
private static BasicDataSource basicUser = null;
private static final String JNDI_PREFIX = "java:/comp/env";
private static final String ADMIN_DB_NAME = "jdbc/FsEDBAdmin";
private static final String USER_DB_NAME = "jdbc/FsEDBUser";

/**
 * Secure method that gives a connection from the pool for an Admin user
 * @return java.sql.Connection
 * @throws Exception Throws if Context isn't properly defined on Server 
 */
public static Connection getAdminConnection() throws Exception {
    try {
        if(basicAdmin == null) { //1st time load
            Context dbContext = (Context) new InitialContext().lookup(JNDI_PREFIX);
            basicAdmin = (BasicDataSource) dbContext.lookup(ADMIN_DB_NAME);
        }
        return basicAdmin.getConnection();
    } catch (NamingException | SQLException e) {
        throw new RuntimeException("\nInvalid JNDI resource: " + ADMIN_DB_NAME + CommonTools.getStackTrace(e));
    }
}

/**
 * Secure method that gives a connection from the pool for a standard user
 * @return java.sql.Connection
 * @throws Exception Throws if Context isn't properly defined on Server 
 */
public static Connection getUserConnection() throws Exception {
    try {
        if(basicUser == null) { //1st time load
            Context dbContext = (Context) new InitialContext().lookup(JNDI_PREFIX);
            basicUser = (BasicDataSource) dbContext.lookup(USER_DB_NAME);
        }
        return basicUser.getConnection();
    } catch (NamingException | SQLException e) {
        throw new RuntimeException("\nInvalid JNDI resource: " + USER_DB_NAME + CommonTools.getStackTrace(e));
    }
}
}

最佳答案

因此,我的解决方案是删除 context.xml 中除基本 Resource 定义之外的所有条目。这包括删除 ResourceLink 条目。因此,如果您已尝试了所有方法,但遇到此错误,就像我忽略所有文档并仅在 Context.xml 中定义一样。

关于javax.naming.NameNotFoundException : Name [jdbc/FsEDBUser] is not bound in this Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729141/

相关文章:

java - 如何在JAVA jdbc中运行SQL(MYSQL)存储过程?

java - 如何在java中解析二维JSON数组

java - Spring websocket 示例 - 错误 - 您是否在支持 J SR-356 的 Servlet 容器中运行?

java - jsf:请求的资源不可用

java - 如何为tomcat编译servlet和现有包

hibernate - 记录 JDBC/Hibernate/JPA 事务隔离级别

超过 1 个对象的 java 同步块(synchronized block)?

java - 带有 Java 的 Selenium WebDriver : Can't accept alert

java - Java Swing 中的自动完成文本框

postgresql - Squeryl 中的显式 jsonb 类型转换