2016-05-25 106 views
0

ローカルのデータベース(Postgres 9.3)に接続するJavaクラスをサブリソースとして持つ小さなJersey(1.9)REST Serviceを構築しています。私は、アプリケーションを実行し、次のリソースを呼び出すときこのコンテキストでは名前がバインドされていません...データソースが見つかりません

<?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 

ページが空白のを私はすでにのcontext.xmlのエントリを追加しているデータソースの

NULLポインタ例外

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) 

: - とNetBeans(8.1)上のTomcat(8.0)がエラーを投げていますまた、私はすでにlibrairiesに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; 
    } 
} 

任意のアイデアPLS:

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

、ここでは、データソース接続用のサブリソースクラスです。この問題を解決する方法..

多くのおかげ

a.kasbi

問題が解決されました。.. Apache Tomcatのドキュメントは非常に有用だった

答えて

0

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

おかげ

関連する問題