2016-07-07 111 views
1

我是JNDI的新手,我試圖讓我的db連接正常工作。到目前爲止沒有運氣。 我要麼收到一條消息:「名稱[java:comp/env]沒有綁定在這個上下文中,找不到[java:comp]」 或者我收到了超時。Tomcat中的JNDI數據源配置7

以下是關於我當前配置的信息。

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

我得到:

收到超時

我已經審查了與JNDI很多職位,包括以下兩個說是最有幫助的:

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html and https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-datasource-jndi-example/

請注意,我閱讀How to configure jndi DataSource in Tomcat 7,但它不能解決我的問題。

任何人都可以請幫忙解決這個問題嗎?

回答

0

它的工作對我來說,當我直接在webapp配置的數據源(文件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>