views:

73

answers:

2

I've been attempting to pull hard-coded configuration data from a Tomcat web-app which I'm working on, into JNDI (web.xml) to make the app more configurable. Instead of continuously doing lookups into the InitialContext, I'm caching the lookups and the InitialContext using a singleton.

Of course, when I stop and start the application, the static instance of my singleton remains alive -- therefore, the cache doesn't get "refreshed". My question is dual -- I'm isolating this singleton to a single web application -- are there any unforeseen effects in Tomcat [I've read that singleton's & tomcat don't mix]? And what is the easiest way to clear the singleton instance when the application is stopped (I was thinking of writing a separate ServerContextListener which would clear the instance).

(Note: I apologize if I used the terminology incorrectly -- please correct -- this is my first foray into programming with Tomcat)

A: 

How your singleton is created?

If you load the singleton from your webapp, it will be isolated to a single webapp because it's loaded by the webapp's own classloader. And it will be destroyed when you undeploy the webapp. Instead of stopping your application, you should undeploy it.

ZZ Coder
Thanks, I wanted to be able to stop and start and have the application destroy the cache within the Singleton, but this may be too much effort for not enough gain.
Watson
+1  A: 

Instead of continuously doing lookups into the InitialContext, I'm caching the lookups and the InitialContext using a singleton

Ok, so you implemented a Service Locator with a cache, that [is|was] a common practice for the web tier. Here is an example.

Of course, when I stop and start the application, the static instance of my singleton remains alive

Hmm... unless you are deploying your singleton in the Common class loader ($CATALINA_HOME/lib) it shouldn't. You're not doing this, right?

I'm isolating this singleton to a single web application -- are there any unforeseen effects in Tomcat

Nothing I'm aware of.

And what is the easiest way to clear the singleton instance when the application is stopped

Deploy your singleton in the webapp class loader (i.e. in /WEB-INF/classes or /WEB-INF/lib) and undeploy the application as suggested.

Pascal Thivent
Yes, that exactly what I did, a primitive Service Locator, I'm not deploying it in the Common class loader, but in the webapp class loader. Looks like I'll have to rely on deploying and undeploying the application to refresh the cache. Thanks!
Watson