![](/img/trans.png)
[英]How to Configure JNDI DataSource in Tomcat 8 with Java Configuration:
[英]JNDI DataSource configuration in Tomcat 7
我是 JNDI 的新手,我正在嘗試讓我的數據庫連接正常工作。 到目前為止沒有運氣。 我要么收到一條消息,指出:“名稱 [java:comp/env] 未在此上下文中綁定。無法找到 [java:comp]”,或者我收到超時。
這是有關我當前配置的信息。
Tomcat:Apache Tomcat/7.0.29
JMV:1.7.0_06-b24
操作系統:Win 10 Pro
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"
/>
代碼:
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和https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-datasource-jndi-example/
請注意,我閱讀了如何在 Tomcat 7 中配置 jndi 數據源,但它沒有為我的問題提供解決方案。
任何人都可以幫助解決這個問題嗎?
當我直接在 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.