【问题标题】:javax.naming.NameNotFoundException: Name [jdbc/FsEDBUser] is not bound in this Contextjavax.naming.NameNotFoundException:名称 [jdbc/FsEDBUser] 未绑定在此上下文中
【发布时间】:2016-12-08 07:43:47
【问题描述】:

我以前设置过 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));
    }
}
}

【问题讨论】:

    标签: java tomcat jdbc tomcat8


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-09-22
      • 2018-09-12
      • 2018-11-02
      • 2017-07-17
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 2013-05-20
      相关资源
      最近更新 更多