Ich bin neu in JNDI und ich versuche, meine DB-Verbindung funktioniert. Bis jetzt kein Glück. Ich bekomme entweder eine Nachricht: "Name [java: comp/env] ist in diesem Kontext nicht gebunden. Ich konnte [java: comp] nicht finden" oder ich habe eine Auszeit bekommen.JNDI DataSource-Konfiguration in Tomcat 7
Hier finden Sie Informationen zu meiner aktuellen Konfiguration.
Tomcat: Apache Tomcat/7.0.29
JMV: 1.7.0_06-b24
OS: 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"
/>
Ich habe auch versucht, die Ressource in context.xml zu setzen, um sicherzustellen, dass es auffindbar:
<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-Code:
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());
}
}
wenn Ich ändere
props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");
für
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
ich:
abgelaufen Receive
Ich habe viele Kommentare über JNDI einschließlich der folgenden zwei, die am hilfreichsten waren im Test:
http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html und https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-datasource-jndi-example/
Bitte beachten Sie, dass ich die How to configure jndi DataSource in Tomcat 7 lesen, aber es bietet keine Lösung für mein Problem.
Kann jemand bitte helfen, dieses Problem zu lösen?