tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Mikusa <>
Subject Re: Tomcat JNDI custom resource factory questions
Date Wed, 12 Sep 2012 13:28:08 GMT
On Sep 12, 2012, at 1:52 AM, Kirill Ilyukhin wrote:

> Hi!
> 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?

As I understand it, your ObjectFactory implementation needs to be thread safe.  See section
4.5 of the link below.

> 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?

Look at the resource definition...


"Specify whether this resource definition is for a singleton resource, i.e. one where there
is only a single instance of the resource. If this attribute is true, multiple JNDI lookups
for this resource will return the same object. If this attribute is false, multiple JNDI lookups
for this resource will return different objects. This attribute must be true for javax.sql.DataSource
resources to enable JMX registration of the DataSource. The value of this attribute must be
true or false. By default, this attribute is true."

> 2. What is the correct way to close the resource?
> How should I close the resource if the webapp is being
> stopped/undeployed?

Again, look at the resource definition...


Name of the zero-argument method to call on a singleton resource when it is no longer required.
This is intended to speed up clean-up of resources that would otherwise happen as part of
garbage collection. This attribute is ignored if the singleton attribute is false. If not
specificed, no default is defined and no close method will be called.


> 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,
> Kirill
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

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

View raw message