tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kirill Ilyukhin <>
Subject Tomcat JNDI custom resource factory questions
Date Wed, 12 Sep 2012 05:52:35 GMT

I am using Tomcat 7.0.29 with a custom JNDI resource factory
The factory creates a connection to data server which then can be
shared between servlets.
Two questions regarding the resource usage.

1. Creation of the resource factory, isn't is supposed to be thread-safe?
The resource (connection) is used by two servlets which obtains it on
start-up. It works fine if the servlets are loaded one after another -
the first servlet asks JNDI for a connection, JNDI creates a factory,
the factory creates a connection, JNDI gives the connection to the
servlet; the second one ask JNDI for a connection and immediately gets
the same connection, without calling the factory. But if the servlets
are being started exactly at the same time, they get two different
connections created by two different factories. Shouldn't the factory
singleton-ness be managed by Tomcat?

2. What is the correct way to close the resource?
How should I close the resource if the webapp is being
stopped/undeployed? My factory creates a connection on "please create
a resource" call somewhere from inside JNDI, lets the connection go
and doesn't know how many servlets use it. Users of the resource are
independent, they do not know how many of them are and do not know
when the resource can be closed. So I would expect some kind of "it's
time to close the resource(s)" call from JNDI. Is there one?
I know that I can track all the resources creation in a
ServletContextListener and then close them in contextDestroyed(), but
I hope there is a nicer way.

Thanks in advance,

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message