I have a web application which gets database connection via a jndi resource tag in either <GlobalNamingResources>
tag in server.xml
or <Context>
tag in context.xml
.
But even either of the cases, the resource gets invoked for every web app in the Tomcat, Is there a way to define the resource to load only for a specific web app.
I am also restricted to have the resource defined in the applications context.xml
, as I am asked to have the DB config outside the application/WAR file.
Presently, I have kept it in <GlobalNamingResources>
tag and its gets loaded for all web apps.
Any ideas?
As far as I know, in general what you put outside the war is accessible from outside the war.
Yes, JNDI is a kind of "easy" way to define datasource. However this means, that the DB driver also will be deployed outside the war. Usually this approach is good when there is a need to share the driver connections between different WARs. The resource gets loaded by Tomcat's internal classloader and not by a classloader assigned to WAR.
I'm not aware of any way or restriction by WAR of such a global resource.
So, I think you don't have many choices here: if you want to define a "private" application's datasource you'll have to define it inside the war.
You said also that "you're restricted to have the resource defined in the application's context.xml" and store db config outside the WAR.
Why do you have such a requirement?
In general its possible to maintain some external configuration (say, properties file) outside the WAR that will contain the host/user/password and the rest of connection properties for the database outside the WAR, but define the DataSource itself inside the WAR (say in Spring or directly) with connection pool and everything.
If you need to chose from just a couple of possible configurations, its also possible to use profiles in Spring, but since I don't know whether you use Spring at all, its hard to make any statements and give concrete recommendations here.
Maybe its a more preferred approach in your case?