How does the Resource name attribute in Tomcat'

2019-07-07 01:29发布

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).

This works fine for an application.

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>

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

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

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.

Thanks.

标签: tomcat jndi
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-07 02:01

You can basically read it as follow:

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

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

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

查看更多
登录 后发表回答