javax.naming.NameNotFoundException: Name [jdbc/rhwebDB] 在此上下文中未绑定。找不到 [jdbc]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50482786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.naming.NameNotFoundException: Name [jdbc/rhwebDB] is not bound in this Context. Unable to find [jdbc]
提问by Sergio
I know there's many questions regarding this exception, however, I believe I've tried everything for many days, without any luck yet. As this is a production server, I can only work on this after midnight :(
我知道关于这个例外有很多问题,但是,我相信我已经尝试了很多天,但没有任何运气。由于这是一个生产服务器,我只能在午夜之后进行处理:(
I have a Tomcat app. Recently, I've updated the connection pool, in order to use Tomcat's jdbc-connection pool. In my Windows developing machine, everything works fine, but now that I'm trying to implement this on my Linux Server, I get this exception (see title) when ever my app tries to connect to MySQL.
我有一个 Tomcat 应用程序。最近更新了连接池,为了使用Tomcat的jdbc-connection pool。在我的 Windows 开发机器上,一切正常,但现在我试图在我的 Linux 服务器上实现它,每当我的应用程序尝试连接到 MySQL 时,我都会收到这个异常(见标题)。
I'm using "Easy Tomcat 7" which is supposed to be the same as the regular version of Tomcat, only it comes with the CPanel software.
我正在使用“Easy Tomcat 7”,它应该与 Tomcat 的常规版本相同,只是它带有 CPanel 软件。
I just need this DB to be available for this app (not multiple app's).
我只需要此数据库可用于此应用程序(而不是多个应用程序)。
This is my java DB class:
这是我的 Java DB 类:
public class DBUtil {
static DataSource ds;
static {
try {
Context context = new InitialContext();
ds = (DataSource)context.lookup("java:comp/env/jdbc/rhwebDB");
} catch (NamingException e) {
e.printStackTrace();
System.out.println("DBUtil.NamingException" + e);
} catch(Exception e) {
System.out.println(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
This is my context.xml file, which is located on myAppDirectory/META-INF
这是我的 context.xml 文件,它位于 myAppDirectory/META-INF
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/rhwebDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="20"
minIdle="3"
maxIdle="15"
maxWait="10000"
initialSize="3"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
username="dbuser"
password="dbpwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/rhmexweb_rhweb"/>
</Context>
I don't use WAR files. I just upload files to my server and re-start tomcat when needed, which normally works just fine.
我不使用 WAR 文件。我只是将文件上传到我的服务器并在需要时重新启动 tomcat,这通常可以正常工作。
In case this is relevant too, Tomcat's server.xml file has this settings for this website. I've also tried adding parameter copyXML="true", with no luck so far:
如果这也是相关的,Tomcat 的 server.xml 文件有这个网站的设置。我也试过添加参数 copyXML="true",到目前为止没有运气:
<Host name="rhweb.net" appBase="webapps">
<Alias>www.rhweb.net</Alias>
<Context path="" reloadable="false" docBase="/home/rhweb/public_html" debug="1"/>
</Host>
Here's the full stacktrace I get when my app tries to establish a connection with MySQL:
这是我的应用程序尝试与 MySQL 建立连接时获得的完整堆栈跟踪:
javax.naming.NameNotFoundException: Name [jdbc/rhwebDB] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
at javax.naming.InitialContext.lookup(InitialContext.java:415)
at util.DBUtil.<clinit>(DBUtil.java:23)
at Solutio.AdminRH.Entity.ISeguridadAdminDB.verificaUsuario(ISeguridadAdminDB.java:142)
at org.apache.jsp.menu_jsp._jspService(menu_jsp.java:115)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
I've also tried adding these lines in my app's web.xml file (which I hadn't had to include in my windows machine)
我还尝试在我的应用程序的 web.xml 文件中添加这些行(我不必将其包含在我的 Windows 机器中)
<resource-ref>
<description>MySQL RhWeb Datasource</description>
<res-ref-name>jdbc/rhwebDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And when I include those lines, the exception I get is this one:
当我包含这些行时,我得到的例外是这一行:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
It seems that Tomcat can't find my META-INF/context.xml file, or it doesn't recognize this link:
Tomcat 似乎找不到我的 META-INF/context.xml 文件,或者它无法识别此链接:
ds = (DataSource)context.lookup("java:comp/env/jdbc/rhwebDB");
ds = (DataSource)context.lookup("java:comp/env/jdbc/rhwebDB");
My linux, Tomcat and JDK versions are:
我的 linux、Tomcat 和 JDK 版本是:
CentOS release 6.8 (Final)
Apache Tomcat/7.0.42
java version "1.7.0_131"
CentOS release 6.8 (Final)
Apache Tomcat/7.0.42
java 版本“1.7.0_131”
I don't know what else can I try to fix this. Any help will be really appreciated
我不知道我还能尝试什么来解决这个问题。任何帮助将不胜感激
UPDATE
更新
I still haven't found a solution yet. I've realized some probable configuration issues that may be related to all this:
我还没有找到解决办法。我已经意识到一些可能与所有这些相关的配置问题:
The app is located here:
该应用程序位于:
/home/rhmexweb/public_html
/home/rhmexweb/public_html
However, on the Tomcat's server.xml file the host definition for this website is:
但是,在 Tomcat 的 server.xml 文件中,该网站的主机定义是:
<Host name="rhweb.mx" appBase="webapps">
<Context path="" reloadable="false" docBase="/home/rhmexweb/public_html" />
</Host>
The reason why is not located on the webapps directory, is because CPanel automatically creates every account in /home/account-name/public_html, so that's why a context element is needed here (I guess). If I remove the whole Contexttag, and instead I use appBase="/home/rhmexweb/public_html", then Tomcat won't find all the jars and content from the WEB-INF folder (/home/rhmexweb/public_html/WEB-INF). I mentioned this because I added the parameter copyXML="true"just to see where would tomcat tries to copy context.xml file, and if it changes it's name, but it turns out that there are two different paths. First I got a writing permissions exception when I first include that parameter, and it was trying to create a folder here:
之所以不在 webapps 目录下,是因为 CPanel 会自动在 /home/account-name/public_html 中创建每个帐户,所以这就是这里需要上下文元素的原因(我猜)。如果我删除整个Context标记,而是使用 appBase="/home/rhmexweb/public_html",那么 Tomcat 将无法从 WEB-INF 文件夹 ( /home/rhmexweb/public_html/WEB-中档)。我提到这一点是因为我添加了参数copyXML="true"只是为了查看 tomcat 会尝试复制 context.xml 文件的位置,如果它更改了它的名称,但事实证明有两个不同的路径。首先,当我第一次包含该参数时,我遇到了写入权限异常,它试图在此处创建一个文件夹:
/var/lib/easy-tomcat7/webapps/
/var/lib/easy-tomcat7/webapps/
This path, only contains folders of the default tomcat app's (manager, examples & host-manager)
此路径仅包含默认 tomcat 应用程序的文件夹(管理器、示例和主机管理器)
The folder where all the websites are listed and all the compiled jsp files are is this one:
列出所有网站和所有编译的jsp文件的文件夹是这个:
/usr/local/easy/etc/easy-tomcat7/Catalina/
/usr/local/easy/etc/easy-tomcat7/Catalina/
however, in /usr/local/easy/etc/easy-tomcat7/Catalina/rhweb.mx/ there is no WEB-INF directory.
但是,在 /usr/local/easy/etc/easy-tomcat7/Catalina/rhweb.mx/ 中没有 WEB-INF 目录。
Anyway, what I'd like to know is if having a Context element in the host definition might be a problem for Tomcat in order to find the appPath/META-INF/context.xml file.
无论如何,我想知道的是,为了找到 appPath/META-INF/context.xml 文件,在主机定义中是否有一个 Context 元素可能是 Tomcat 的问题。
采纳答案by Sergio
I finally solved it!!
我终于解决了!!
Maybe it's a workaround but it works.
也许这是一种解决方法,但它有效。
I copied the app-directory/META-INF/context.xml file to:
我将 app-directory/META-INF/context.xml 文件复制到:
$CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default(notice the "default" part of the filename)
$CATALINA_BASE/conf/[enginename]/[hostname]/ context.xml.default (注意文件名的“默认”部分)
As it's stated in the Tomcat's documentation
Now I don't need to add any entry in my app's web.xml file, neither a META-INF/context.xml file in my app directory (which is notlocated on the Tomcat's/webapps directory)
现在我不需要在我的应用程序的 web.xml 文件中添加任何条目,也不需要在我的应用程序目录中添加 META-INF/context.xml 文件(它不在Tomcat 的/webapps 目录中)
All I need is to put a context.xml.default file (containing it's DB information) in every folder listed in this path:
我只需要在此路径中列出的每个文件夹中放置一个 context.xml.default 文件(包含它的数据库信息):
$CATALINA_BASE/conf/[enginename]/
$CATALINA_BASE/conf/[引擎名称]/
which in my server is:
在我的服务器中是:
/usr/local/easy/etc/easy-tomcat7/Catalina/
/usr/local/easy/etc/easy-tomcat7/Catalina/
so for this website, I copied it to:
所以对于这个网站,我把它复制到:
/usr/local/easy/etc/easy-tomcat7/Catalina/myDomain.com/
/usr/local/easy/etc/easy-tomcat7/Catalina/myDomain.com/
You'll notice my path is different from the one specified in Tomcat's documentation, and that's because I'm using CPanel's easy-tomcat 7 which has a different structure than the regular Tomcat. That fact made everything more complicated when looking for a solution, even if it's called easy-tomcat.
您会注意到我的路径与 Tomcat 文档中指定的路径不同,这是因为我使用的是 CPanel 的 easy-tomcat 7,它的结构与常规 Tomcat 不同。这个事实使寻找解决方案时的一切变得更加复杂,即使它被称为easy-tomcat。
About why Tomcat could never read the /META-INF/context.xml, that remains a mistery, but honestly, I don't care much since this method it's easier (one file instead of two)
关于为什么 Tomcat 永远无法读取 /META-INF/context.xml,这仍然是个谜,但老实说,我不太在意,因为这种方法更容易(一个文件而不是两个文件)
回答by Karol Dowbecki
It looks like there is something wrong with the context.xml
setup. Try server.xml
or /META-INF/context.xml
to see if it behaves any different.
看起来context.xml
设置有问题。尝试server.xml
或/META-INF/context.xml
查看它的行为是否有任何不同。
You can try moving the configuration to the server.xml
as per Tomcat 7 docs:
您可以尝试移动配置到server.xml
为每个Tomcat的7文档:
<GlobalNamingResources>
<Resource
name="jdbc/rhweb_rhwebDB"
auth="Container"
type="javax.sql.DataSource"
...
/>
</GlobalNamingResources>
link to global resource in the context.xml
:
链接到全球资源context.xml
:
<Context>
<ResourceLink
global="jdbc/rhweb_rhwebDB"
name="jdbc/rhwebDB"
type="javax.sql.DataSource"
/>
</Context>
and then refer to it in /WEB-INF/web.xml
like you already do:
然后/WEB-INF/web.xml
像你已经做的那样引用它:
<resource-ref>
<res-ref-name>jdbc/rhwebDB</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>
If this won't work you can check few other approaches discussed in this answer.
如果这不起作用,您可以检查此答案中讨论的其他一些方法。