tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Should a webapp close a DBCP datasource obtained through JNDI?
Date Fri, 25 Mar 2011 19:13:22 GMT
Hash: SHA1


On 3/25/2011 1:53 PM, Paul Gifford wrote:
> We're having a disagreement in the office about the JNDI datasources.  We're
> running Tomcat 6 and using the built-in DBCP as our connection pool.  DBCP
> is configured in the server's context.xml.  Our webapp gets a datasource
> using JNDI via Spring JDBC.
> There are two schools of thought on the team.  One says we should close the
> datasource in a listener's contextDestroyed, something like this:
> ((BasicDataSource)datasource).close();
>  The other school of thought says that's not necessary, since Tomcat is
> managing the data source and the resource is not instantiated in the
> listener's contextInitialized method.
>   What's the right answer?  I haven't seen anything that says one way or the
> other.

Interestingly enough, I'm on both sides of the fence on this one. Here's

On the one hand, I maintain that since Tomcat creates the DataSource, it
should destroy the DataSource.

On the other, the Java EE spec says that JNDI resources should be
created each time they are looked-up. This was very surprising to me,
especially for JNDI DataSources. Of course, this makes much more sense
in the context of, say, a JNDI Mail Session.

See bugs and

So, if you follow the spec (always the right answer, IMO), then your
webapp is expected to close the DataSource. In this particular instance,
though, the spec-defined behavior seems entirely impractical.

Tomcat is very unlikely to be modified to change this behavior, so you
should probably go ahead and write a ServletContextListener that does
nothing in init() and closes the DataSource in destroy().

- -chris
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla -


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

View raw message