2016-07-07 11 views
1

私はJNDIを初めて使用しています。私はdb接続を取得しようとしています。今まで運がない。 "Name [java:comp/env]がこのコンテキストにバインドされていません。[java:comp]を見つけることができません" またはタイムアウトが発生しました。Tomcat 7のJNDIデータソース設定

ここに私の現在の設定に関する情報があります。

のTomcat:Apache Tomcatの/ 7.0.29

JMV:1.7.0_06-B24

OS:勝利10プロ

のTomcat \ confに\ web.xmlの

<resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/myDatabaseName</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 

Tomcat \ conf \ context.xml

<ResourceLink type="javax.sql.DataSource" 
name="jdbc/localRemarket" 
global="jdbc/remarket" 
/> 

私も作るためのcontext.xmlにリソースを入れてみました確認してくださいそれは検索可能です:

<Resource 
type="javax.sql.DataSource" 
name="jdbc/myDatabaseName" 
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
driverClassName="com.mysql.jdbc.Driver" 
url="jdbc:mysql://localhost:3306/myDatabaseName" 
username="myUsername" 
password="myPassword" 
maxActive="1500" 
maxIdle="200" 
maxwait="-1" 
testOnBorrow="true" 
testOnReturn="true" 
testWhileIdle="true" 
validationQuery="SELECT 1" 
timeBetweenEvictionRunsMillis="2000" 
minEvictableIdleTimeMillis="15000" 
removeAbandoned="true" 
removeAbandonedTimeout="5" 
/> 

のTomcat \ confに\ server.xmlを

<Resource 
type="javax.sql.DataSource" 
name="jdbc/myDatabaseName" 
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
driverClassName="com.mysql.jdbc.Driver" 
url="jdbc:mysql://localhost:3306/myDatabaseName" 
username="myUsername" 
password="myPassword" 
maxActive="1500" 
maxIdle="200" 
maxwait="-1" 
testOnBorrow="true" 
testOnReturn="true" 
testWhileIdle="true" 
validationQuery="SELECT 1" 
timeBetweenEvictionRunsMillis="2000" 
minEvictableIdleTimeMillis="15000" 
removeAbandoned="true" 
removeAbandonedTimeout="5" 
/> 

Javaコード:

Connection conn; 

public void openMyConnection() { 

try { 

Properties props = new Properties(); 
props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory"); 

InitialContext ctx = new InitialContext(props); 
Context envCtx = (Context) ctx.lookup("java:comp/env"); // <<<<< PRB HERE 
// error message : Name [java:comp/env] is not bound in this Context. Unable to find [java:comp] 

org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) envCtx.lookup("jdbc/localDB"); 

conn = ds.getConnection(); 

} catch (Exception e) { 
System.out.println(e.getMessage()); 
} 

} 

場合私は変更する

props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory"); 
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 

ため私が取得:

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

は、私が最も有用だった次の二つを含むJNDIに関連する多くの記事を見直している

をタイムアウトしレシーブ および https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-datasource-jndi-example/

私はHow to configure jndi DataSource in Tomcat 7を読んでいますが、私の問題を解決するものではありません。

誰でもこの問題を解決できますか?

答えて

0

私はWebアプリケーション(ファイルMETA-INF/context.xmlに)で直接データソースを設定したときは、私の仕事:

<Context > 
<Resource name="jdbc/EmployeeDB" 
      auth="Container" 
      type="javax.sql.DataSource" 
      username="scott" 
      password="tiger" 
      driverClassName="oracle.jdbc.OracleDriver" 
      url="jdbc:oracle:thin:@127.0.0.1:1521:mysid" 
      maxActive="8" 
      maxIdle="4"/> 
</Context> 
関連する問題