嗨,
我在配置纯JDBC-JNDI连接时遇到问题。我没有使用任何特殊的框架*(没有Spring,没有Hibernate)。我在我的计算机上运行Tomcat 8.5.15(localhost)。数据库位于远程服务器中,我已验证连接是否正常工作。我已将JDBC驱动程序jar文件放在apache-tomcat-8.5.15/lib/driver_lib
。
我遇到的错误是:
javax.naming.NameNotFoundException: Name [jdbc/KDB] is not bound in this Context. Unable to find [jdbc].
我不确定为什么收到这条消息;我试过遵循Tomcat网站上提到的指南。我也关注了StackOverflow关于这个问题的大部分问题,但是这个消息仍然出现了。请帮忙!
Apache的Tomcat的8.5.15 / CONF / 的server.xml
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<!-- confidential -->
<Resource name="jdbc/KDB"
global="jdbc/KDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="xxxx"
url="xxxx"
username="xxxx"
password="xxxx"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
[项目] 的/ SRC /主/ web应用/ WEB-INF / 的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false" version="3.0">
<display-name>Servlet 3.0 Web Application</display-name>
<!-- Application class name -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>kk.MainApplication</param-value>
</context-param>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- JDNI Resource -->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/KDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>/user</welcome-file>
</welcome-file-list>
</web-app>
[项目] 的/ SRC /主/ web应用/ META-INF / 的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<ResourceLink name="jdbc/KDB" global="jdbc/KDB"
auth="Container" type="javax.sql.DataSource" />
</Context>
用于运行查询的代码(在 Database.java 中)
public List<T> runQuery(String query) throws SQLException {
List<T> result = null;
Connection con = null;
Context ctx = null;
Statement stmt = null;
ResultSet rs = null;
try {
result = new ArrayList<T>();
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/KDB");
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(query);
rs.beforeFirst();
while (!rs.next()) {
T user = dtoClass.newInstance();
for (Entry<String, String> entry : dtoFieldMap.entrySet()) {
String value = rs.getString(entry.getValue());
user.setField(entry.getKey(), value);
}
result.add(user);
}
log.trace("runQuery: query completed successfully");
log.info("Driver Name: " + con.getMetaData().getDriverName());
} catch (NamingException | SQLException | InstantiationException | IllegalAccessException ex) {
log.error("An Exception occurred in runQuery(String)", ex);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException ex) {
log.error("Field-related Exception", ex);
ex.printStackTrace();
} finally {
DbUtils.close(rs);
DbUtils.close(stmt);
DbUtils.close(con);
}
return result;
}
控制台输出
11:30:55.368 [http-nio-8090-exec-2] ERROR kk.db.Database - An Exception occurred in runQuery(String)
javax.naming.NameNotFoundException: Name [jdbc/KDB] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:816) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:173) ~[catalina.jar:8.5.15]
at org.apache.naming.factory.ResourceLinkFactory.getObjectInstance(ResourceLinkFactory.java:152) ~[catalina.jar:8.5.15]
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321) ~[?:1.8.0_131]
at org.apache.naming.NamingContext.lookup(NamingContext.java:839) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:159) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:827) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:159) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:827) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:159) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:827) ~[catalina.jar:8.5.15]
at org.apache.naming.NamingContext.lookup(NamingContext.java:173) ~[catalina.jar:8.5.15]
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163) ~[catalina.jar:8.5.15]
at javax.naming.InitialContext.lookup(InitialContext.java:417) ~[?:1.8.0_131]
at kk.db.Database.runQuery(Database.java:154) [classes/:?]
at kk.UserResource.getQueryResult(UserResource.java:137) [classes/:?]
at kk.UserResource.toHtml(UserResource.java:63) [classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:511) [org.restlet-2.3.10.jar:?]
at org.restlet.resource.ServerResource.get(ServerResource.java:723) [org.restlet-2.3.10.jar:?]
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:603) [org.restlet-2.3.10.jar:?]
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:662) [org.restlet-2.3.10.jar:?]
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:348) [org.restlet-2.3.10.jar:?]
at org.restlet.resource.ServerResource.handle(ServerResource.java:1020) [org.restlet-2.3.10.jar:?]
at org.restlet.resource.Finder.handle(Finder.java:236) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Router.doHandle(Router.java:422) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Router.handle(Router.java:641) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202) [org.restlet-2.3.10.jar:?]
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:77) [org.restlet-2.3.10.jar:?]
at org.restlet.Application.handle(Application.java:385) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Router.doHandle(Router.java:422) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Router.handle(Router.java:641) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Router.doHandle(Router.java:422) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Router.handle(Router.java:641) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.10.jar:?]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.10.jar:?]
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202) [org.restlet-2.3.10.jar:?]
at org.restlet.Component.handle(Component.java:408) [org.restlet-2.3.10.jar:?]
at org.restlet.Server.handle(Server.java:507) [org.restlet-2.3.10.jar:?]
at org.restlet.engine.connector.ServerHelper.handle(ServerHelper.java:63) [org.restlet-2.3.10.jar:?]
at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:143) [org.restlet-2.3.10.jar:?]
at org.restlet.ext.servlet.ServerServlet.service(ServerServlet.java:1117) [org.restlet.ext.servlet-2.3.10.jar:?]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [servlet-api.jar:?]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.15]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.15]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [catalina.jar:8.5.15]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:8.5.15]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [catalina.jar:8.5.15]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:8.5.15]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [catalina.jar:8.5.15]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) [catalina.jar:8.5.15]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:8.5.15]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:8.5.15]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-coyote.jar:8.5.15]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:8.5.15]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-coyote.jar:8.5.15]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-coyote.jar:8.5.15]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:8.5.15]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.5.15]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
非常感谢任何建议或解决方案。感谢。
* P.S。我也在使用Restlet API,GSON,DbUtils和log4j2,但我不认为这会导致任何问题,因为它在代码中没有数据库实现之前。
答案 0 :(得分:1)
您是否尝试过更改此行
DataSource ds =(DataSource)ctx.lookup(&#34; java:/ comp / env / jdbc / KDB&#34;);
到此?
DataSource ds =(DataSource)ctx.lookup(&#34; java:comp / env / jdbc / KDB&#34;);
(没有第一个斜线)