Java Tomcat7中的JNDI数据源配置

Java Tomcat7中的JNDI数据源配置,java,apache,tomcat,jndi,Java,Apache,Tomcat,Jndi,我是JNDI新手,我正在尝试让我的db连接正常工作。到目前为止没有运气。 我或者收到一条消息:“名称[java:comp/env]未绑定在此上下文中。找不到[java:comp]” 或者我被暂停了 以下是有关我当前配置的信息 Tomcat:ApacheTomcat/7.0.29 JMV:1.7.0_06-b24 OS:Win10职业选手 Tomcat\conf\web.xml <resource-ref> <description>DB Connection</de

我是JNDI新手,我正在尝试让我的db连接正常工作。到目前为止没有运气。 我或者收到一条消息:“名称[java:comp/env]未绑定在此上下文中。找不到[java:comp]” 或者我被暂停了

以下是有关我当前配置的信息

Tomcat:ApacheTomcat/7.0.29

JMV:1.7.0_06-b24

OS:Win10职业选手

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>
为了

我得到:

接收超时

我回顾了许多与JNDI相关的帖子,包括以下两篇最有帮助的帖子:

请注意,我阅读了,但它没有为我的问题提供解决方案


有人能帮我解决这个问题吗?

当我直接在webapp(文件META-INF/context.xml)中配置数据源时,它对我起到了作用:


<ResourceLink type="javax.sql.DataSource"
name="jdbc/localRemarket"
global="jdbc/remarket"
/>
<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"
/>
<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"
/>
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");
<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>