【问题标题】:Name is not bound in this Context... Datasource not found名称未在此上下文中绑定...未找到数据源
【发布时间】:2016-05-28 14:33:07
【问题描述】:

我正在构建一个小型 Jersey (1.9) REST 服务,并有一个 Java 类作为我连接到本地数据库 (Postgres 9.3) 的子资源。

对于数据源,我已经在 Context.xml 中添加了条目:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/userProfile">
   <Resource
      auth="Container"
      driverClassName="org.postgresql.Driver"
      maxActive="100"
      maxIdle="30"
      maxWait="10000"
      name="jdbc/apiUserProfile"
      password="postgres"
      type="javax.sql.DataSource"
      url="jdbc:postgresql://localhost:5432/apiUserProfile"
      username="postgres"/>
</Context>

当我运行应用程序并调用以下资源时:

http://localhost:8084/userProfile/rest/user/conn

页面为空白 - 没有内容 - 并且 netbeans (8.1) 上的 tomcat (8.0) 抛出错误:空指针异常

javax.naming.NameNotFoundException: Name [jdbc/apiUserProfile] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at net.rest.dao.DbConn.apiUserProfileConn(DbConn.java:23)
at net.rest.service.userProfile.returnDatabaseStatus(userProfile.java:51)

我在库中也已经有了 JAR 文件:

lib/mysql-connector-java-5.1.39-bin.jar
lib/postgresql-9.3-1100-jdbc4.jar

这里是数据源连接的子资源类:

package net.rest.dao;

import javax.naming.*;
import javax.sql.*;

public class DbConn {
    private static DataSource DbConn = null;
    private static Context context = null;
    public static DataSource apiUserProfileConn() throws Exception {
        if(DbConn != null){
           return DbConn;
        }
        try {
            if(context == null){
               context = new InitialContext();
            }
            DbConn = (DataSource) context.lookup("jdbc/apiUserProfile");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return DbConn;
    }
}

任何想法请。如何解决这个问题..

非常感谢

a.kasbi

【问题讨论】:

    标签: jersey datasource postgresql-9.3 tomcat8


    【解决方案1】:

    问题现已解决。Apache Tomcat Doc 非常有帮助:

    http://localhost:8080/docs/jndi-datasource-examples-howto.html
    http://localhost:8080/docs/jndi-datasource-examples-howto.html#PostgreSQL
    

    我的解决方案是将以下内容添加到 context.xml 下:META-INF

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/apiRest">
       <Resource name="jdbc/apiUserProfile" auth="Container"
          type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://127.0.0.1:5432/apiUserProfile"
          username="postgres" password="postgres" maxTotal="20" maxIdle="10"
          maxWaitMillis="-1"/>
    </Context>
    

    以及 web.xml 中的以下内容:

    <resource-ref>
       <description>postgreSQL Datasource example</description>
       <res-ref-name>jdbc/apiUserProfile</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>
    

    数据源连接的 java 类中的查找参数:

    ...
    DbConn = (DataSource) context.lookup("java:/comp/env/jdbc/apiUserProfile");
    ...
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2013-05-20
      • 2015-12-24
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      相关资源
      最近更新 更多