[英]porting web app to Tomcat: javax.naming.NameNotFoundException:
New user of Tomcat (8.5.9) on Linux CentOS 7 with Java SE 8. I must be making a simple mistake. 带有Java SE 8的Linux CentOS 7上的Tomcat(8.5.9)的新用户。我必须犯一个简单的错误。 This should be a textbook example how to configure a JDBC connection pool for tomcat.
这应该是一个教科书示例,说明如何为tomcat配置JDBC连接池。
I have this error: 我有这个错误:
javax.naming.NameNotFoundException: Name [jdbc/pool1] is not bound in this Context. Unable to find [jdbc]
Any idea what I could doing wrong? 知道我会做错什么吗? Tomcat states
It is NOT recommended to place <Context> elements directly in the server.xml file.
Tomcat 状态
It is NOT recommended to place <Context> elements directly in the server.xml file.
Thus, my setup: 因此,我的设置是:
$CATALINA_HOME/webapps/myapp/META-INF/context.xml is as follows: $ CATALINA_HOME / webapps / myapp / META-INF / context.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/pool1"
auth="Container"
type="javax.sql.DataSource"
username="xx"
password="xx"
driverClassName="oracle.jdbc.OracleDriver"
url="xx"
maxTotal="256"
maxIdle="8"
initialSize="4"
removeAbandonedTimeout="7200"
removeAbandonedOnBorrow="true"/>
<Resource name="jdbc/pool2"
auth="Container"
type="javax.sql.DataSource"
username="xx"
password="xx"
driverClassName="oracle.jdbc.OracleDriver"
url="xx"
maxTotal="256"
maxIdle="8"
initialSize="4"
removeAbandonedTimeout="7200"
removeAbandonedOnBorrow="true"/>
<ResourceLink name="jdbc/pool1"
global="jdbc/pool1"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/pool2"
global="jdbc/pool2"
type="javax.sql.DataSource"/>
</Context>
$CATALINA_HOME/webapps/myapp/WEB-INF/web.xml is as follows: $ CATALINA_HOME / webapps / myapp / WEB-INF / web.xml如下:
...
<resource-ref>
<description>xxx</description>
<res-ref-name>jdbc/pool1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>xxx</description>
<res-ref-name>jdbc/pool1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
The code causing the exception: 导致异常的代码:
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jdbc/pool1");
conn = ds.getConnection();
I have not modified $CATALINA_HOME/conf/server.xml at all. 我根本没有修改$ CATALINA_HOME / conf / server.xml。 Did I configure something incorrectly, or am I missing setting another file up somewhere?
我是否配置不正确,还是想在某个地方设置另一个文件?
UPDATE 1 更新1
I tried adding the above ResourceLinks to the GlobalNamingResources tag in the $CATALINA_HOME/conf/server.xml file, then stopping/starting Tomcat, but I got the same error. 我尝试将上述ResourceLinks添加到$ CATALINA_HOME / conf / server.xml文件中的GlobalNamingResources标记中,然后停止/启动Tomcat,但是遇到了相同的错误。
UPDATE 2 更新2
I then added the Resource tags from context.xml above also to the server.xml file (GlobalNamingResources tag), stopping/starting tomcat, and got same error. 然后,我还将上述context.xml中的Resource标签也添加到server.xml文件中(GlobalNamingResources标签),停止/启动了Tomcat,并得到了相同的错误。
UPDATE 3 更新3
I got everything working with Andreas' expert help (thanks!) by changing the way java calls the pool: 通过更改java调用池的方式,我得到了Andreas专家帮助的所有帮助(谢谢!):
Context initCtx = new InitialContext();
Context context = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) context.lookup("jdbc/pool1");
conn = ds.getConnection();
Also, the ResourceLinks should not be in server.xml (they simply generate a warning in tomcat log). 另外,ResourceLinks不应位于server.xml中(它们只是在tomcat日志中生成警告)。
Your $CATALINA_BASE/conf/server.xml
file should contain the full <Resource>
element. 您的
$CATALINA_BASE/conf/server.xml
文件应包含完整的<Resource>
元素。 Remember to also add the JDBC driver jar file to Tomcat's $CATALINA_BASE/lib
folder, since it is Tomcat, not your webapp, that needs it when the <Resource>
is defined in server.xml
. 记住还要将JDBC驱动程序jar文件添加到Tomcat的
$CATALINA_BASE/lib
文件夹中,因为在server.xml
定义<Resource>
时需要它的是Tomcat,而不是您的webapp。
Next, the META-INF/context.xml
is a template that is used the first time your webapp is deployed. 接下来,
META-INF/context.xml
是一个模板,该模板在首次部署Web应用程序时使用。 It gets copied to $CATALINA_BASE/conf/Catalina/localhost/myapp.xml
, and is likely not updated/refreshed if you change META-INF/context.xml
. 它被复制到
$CATALINA_BASE/conf/Catalina/localhost/myapp.xml
,如果更改META-INF/context.xml
,则可能不会更新/刷新。
The .../Catalina/localhost/myapp.xml
file should contain the <ResourceLink>
element, mapping the name used by the webapp to the name used in server.xml
. .../Catalina/localhost/myapp.xml
文件应包含<ResourceLink>
元素,将webapp使用的名称映射到server.xml
使用的名称。 Keeping those two names the same is easiest, but not required. 保持这两个名称相同是最简单的,但不是必需的。
Tomcat works fine without the <resource-ref>
elements in WEB-INF/web.xml
, but it's better if they are there, for compatibility with other Servlet containers. 在没有
WEB-INF/web.xml
的<resource-ref>
元素的情况下,Tomcat可以正常工作,但是如果它们在其中,则最好与其他Servlet容器兼容。
Note: $CATALINA_BASE
is usually the same as $CATALINA_HOME
, ie the folder where Tomcat is installed, unless you explicitly configure it otherwise. 注意:
$CATALINA_BASE
通常与$CATALINA_HOME
相同,即Tomcat的安装文件夹,除非另行明确配置。
So, $CATALINA_BASE/conf/server.xml
: 因此,
$CATALINA_BASE/conf/server.xml
:
<?xml version='1.0' encoding='utf-8'?>
<Server ...>
...
<GlobalNamingResources>
...
<Resource name="jdbc/pool1" auth="Container" type="javax.sql.DataSource" ... />
<Resource name="jdbc/pool2" auth="Container" type="javax.sql.DataSource" ... />
...
</GlobalNamingResources>
...
</Server>
and $CATALINA_BASE/conf/Catalina/localhost/myapp.xml
: 和
$CATALINA_BASE/conf/Catalina/localhost/myapp.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink name="jdbc/pool1" global="jdbc/pool1" type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/pool2" global="jdbc/pool2" type="javax.sql.DataSource"/>
</Context>
and place ojdbcXXX.jar
in $CATALINA_BASE/lib
. 并将
ojdbcXXX.jar
放在$CATALINA_BASE/lib
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.