Tomcat的context.xml中的“资源名称"属性如何工作? [英] How does the Resource name attribute in Tomcat's context.xml work?

查看:203
本文介绍了Tomcat的context.xml中的“资源名称"属性如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用JNDI查找我们的数据库连接.在Tomcat的全局context.xml文件中,我们类似于以下内容:

We are using JNDI to lookup our database connection. In Tomcat's global context.xml file, we have something similar to the following:

<context>
    <Resource
        ...
        name="jdbc/mysql"
        ....
    />
</context>

(我刚刚在我感兴趣的上方显示了名称"属性).

(I have just displayed the 'name' attribute above I'm interested in).

这对应用程序很好.

我们现在要为其他应用程序添加另一个资源.然后,我们的context.xml如下所示:

We now want to add another resource for a different application. Our context.xml then looked as follows:

<context>
    <Resource
        ...
        name="jdbc/mysql"
        ....
    />
    <Resource
        ...
        name="jdbc/mysql/otherapp"
        ....
    />

</context>

现在,使用此名称定义的这个额外资源后,Tomcat无法启动.基本上我们所有的Web应用程序都会失败.它给出了以下错误:

Now, with this extra Resource defined with that name, Tomcat does not start up. Basically all our web applications fail. It gives the following error:

SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/CallCycleSystem##1.0.4.201410241335]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:962)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1603)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    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:724)
Caused by: java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.naming.Context
    at org.apache.catalina.core.NamingContextListener.createSubcontexts(NamingContextListener.java:1249)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1051)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:671)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:270)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 11 more

如果我将第二个资源的名称属性从"jdbc/mysql/otherapp"更改为"jdbc/otherapp",则Tomcat可以正常启动.

If I change the name attribute of the second Resource from "jdbc/mysql/otherapp" to "jdbc/otherapp", Tomcat starts up fine.

任何人都可以详细说明name属性的工作原理吗?为什么以前会收到错误消息? 在此处中查看文档并没有给我太多帮助.

Can anyone please elaborate on how the name attribute works? Why did I receive an error previously? Looking at the documentation here did not give me much.

谢谢.

推荐答案

您基本上可以按如下方式阅读它:

You can basically read it as follow:

对于<Resource name="jdbc/MyDB" ... />:您正在创建名为 MyDB

for <Resource name="jdbc/MyDB" ... /> : You are creating a JDBC resource with name MyDB,

对于<Resource name="bean/MyBean" ... />:您正在创建一个名为 MyBean Java Bean 资源.

for <Resource name="bean/MyBean" ... /> : You are creating a Java Bean resource with name MyBean.

换句话说,名称被编码为" Resource_Type / Resource_Name "

In other words the name is coded as "Resource_Type/Resource_Name"

这篇关于Tomcat的context.xml中的“资源名称"属性如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆