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
> (http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#Adding_Custom_Resource_Factories).
> 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.

  
http://docs.oracle.com/javase/1.3/docs/guide/jndi/spec/spi/jndispi.fm.html#1007939

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

singleton

"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."

https://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions

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

closeMethod     

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.

https://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions

Dan


> 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: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to