gpt4 book ai didi

java - Tomcat 7 中的 JNDI 数据源配置

转载 作者:行者123 更新时间:2023-11-28 22:01:42 25 4
gpt4 key购买 nike

我是 JNDI 的新手,我正在尝试让我的数据库连接正常工作。到目前为止没有运气。我要么收到一条消息:“名称 [java:comp/env] 未绑定(bind)在此上下文中。无法找到 [java:comp]”或者我超时了。

这是关于我当前配置的信息。

Tomcat:Apache Tomcat/7.0.29

JMV:1.7.0_06-b24

操作系统:Win 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");

我得到:

Receive timed out

我看过许多与 JNDI 相关的帖子,包括以下两个最有帮助的帖子:

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

请注意,我阅读了 How to configure jndi DataSource in Tomcat 7但它没有为我的问题提供解决方案。

谁能帮忙解决这个问题?

最佳答案

当我直接在 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>

关于java - Tomcat 7 中的 JNDI 数据源配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com