2016-05-25 215 views
0

正在構建一個小澤西島(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文件中librairies:

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

回答

0

的問題已經解決..是Apache Tomcat文檔是非常有益的:

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"); 
... 

由於

相關問題