Search code examples
javatomcatweb-fragment

JNDI Resources in web-fragment


I'm working on a web-fragment to isolate a specialized process needed in only one part of our overall production process. I'm trying to define a JNDI resource specific to the web-fragment, but I keep running into a brick wall.

The application is running in Tomcat 9. I am using Web-Fragment specification 4.0.

The working model, right now, is I have the database connection defined as a global resource in the server.xml as such:

        <Resource name="localappserver"
                auth="Container"
                driverClassName="oracle.jdbc.OracleDriver" 
                factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory" 
                initialSize="5" 
                logAbandoned="true" 
                minEvictableIdleTimeMillis="30000" 
                maxTotal="15" 
                maxIdle="3" 
                maxWaitMillis="1000" 
                removeAbandonedOnBorrow="true" 
                removeAbandonedOnMaintenance="true" 
                removeAbandonedTimeout="15" 
                testOnBorrow="true" 
                testWhileIdle="true" 
                timeBetweenEvictionRunsMillis="300000" 
                type="javax.sql.DataSource"  
                username="${jdbc.user}"
                password="${jdbc.password}"
                url="${jdbc.url}" />

and is referenced in the larger web application context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="..." path="/..." reloadable="true" swallowOutput="true">
    
    ...
    <ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" />
    ...
    
</Context>

This works as expected and I can connect to the resource and retrieve data. However, I do not want to pollute the main application's context.cml with items that do not pertain to the main application.

I've tried moving the resource link from the main context.xml to the web-fragment's context.xml but that results in the following error:

INFO: javax.naming.NameNotFoundException: Name [localappserver] is not bound in this Context. Unable to find [localappserver].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:833)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:174)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
    at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
    at com.obj.Data.Connect(Data.java:141)

I've moving the resource definition from the server.xml to the web-fragment's web-fragment.xml and even the web-fragment's context.xml. Both instances return the same error.

While I can live with this one instance, I know we will be adding and/or migrating more pieces to web-fragments and I really want to separate the resource references to their specific jar.

EDIT I should mention I am testing all of this while running tomcat from inside Eclipse. I don't think that will make a difference, but I want to mention it

Is this even possible?


Solution

  • We have decided to containerize the application. Thus, as we have multiple locations in our production process, this will become a non-issue.

    By placing the resource link <ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" /> in the individual location's [TOMCAT_HOME]/conf/context.xml we eliminate the need to clutter the main application's /META-INF/context.xml

    This should work as we divide up each location's requirements into individual web-fragment projects.