How does the resource attribute work in Tomcat context.xml?

We are using JNDI to find our database connection. In the global Tomcat context_xml file, we have something similar to the following:

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

      

(I just showed the 'name' attribute higher than I'm interested in).

This is great for the application.

Now we want to add another resource for another application. Our .xml context looked like this:

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

</context>

      

Now, with this additional resource defined with this name, Tomcat will not start. Basically all of 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 someone explain how the name attribute works? Why did I receive an error earlier? Looking at the documentation here didn't give me much.

Thank.

+3


source to share


1 answer


You can basically read it like this:

for <Resource name="jdbc/MyDB" ... />

: you create a JDBC resource named MyDB,



for <Resource name="bean/MyBean" ... />

: you create a Java Bean resource named MyBean.

In other words, the name is encoded as "Resource_Type / Resource_Name"

0


source







All Articles