带有纯 JDBC-JNDI:javax.naming.NameNotFoundException 的 Tomcat 8.5:名称 [jdbc/KDB] 未绑定在此上下文中。找不到 [jdbc]
Posted
技术标签:
【中文标题】带有纯 JDBC-JNDI:javax.naming.NameNotFoundException 的 Tomcat 8.5:名称 [jdbc/KDB] 未绑定在此上下文中。找不到 [jdbc]【英文标题】:Tomcat 8.5 with pure JDBC-JNDI:javax.naming.NameNotFoundException: Name [jdbc/KDB] is not bound in this Context. Unable to find [jdbc] 【发布时间】:2017-06-26 05:30:17 【问题描述】:情况
你好,
我在配置纯 JDBC-JNDI 连接时遇到问题。我没有使用任何特殊的框架*(没有 Spring,没有 Hibernate)。我在我的计算机(本地主机)上运行 Tomcat 8.5.15。数据库位于远程服务器中,我已验证连接正常。我已将 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 网站上提到的指南。我也关注了 *** 关于这个问题的大部分问题,但这条消息仍然出现。请帮忙!
环境
Tomcat 8.5.15 Java jdk 1.8.0_102 上的 Eclispe JavaEE Neon.3 运行时环境:Java jre 1.7.0_80(Java8 似乎对 Tomcat 有一些问题) 马文文件
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/main/webapp/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/main/webapp/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]
任何建议或解决方案将不胜感激。谢谢。
*附注我也在使用 Restlet API、GSON、DbUtils 和 log4j2,但我认为这不会导致任何问题,因为它在代码中的数据库实现之前没有。
【问题讨论】:
【参考方案1】:您是否尝试过更改此行
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/KDB");
这个?
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/KDB");
(没有第一个斜线)
【讨论】:
这没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review 为什么说这不是答案??在 Tomcat 的文档 (tomcat.apache.org/tomcat-7.0-doc/…) 中指定了使用 JNDI 资源的语法应该是我建议的语法,这肯定是这里的问题。以上是关于带有纯 JDBC-JNDI:javax.naming.NameNotFoundException 的 Tomcat 8.5:名称 [jdbc/KDB] 未绑定在此上下文中。找不到 [jdbc]的主要内容,如果未能解决你的问题,请参考以下文章
通过 SMTP 发送带有附件、纯文本/文本和文本/html 的电子邮件
带有文本/纯文本响应的 API 因“不可接受的内容类型”而失败
您如何使用带有 require 和模块导出的 npm 包作为纯 JS 库
CodeIgniter - 控制器不工作,只加载带有纯文本的索引页面