Bug 50068 - Memory leak with Driver registration
Summary: Memory leak with Driver registration
Alias: None
Product: Taglibs
Classification: Unclassified
Component: Standard Taglib (show other bugs)
Version: 1.2.0
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2010-10-09 18:42 UTC by Jeremy Boynes
Modified: 2010-12-21 22:51 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Jeremy Boynes 2010-10-09 18:42:03 UTC
The <sql:setDataSource> tag may instantiate a new instance of a JDBC Driver from a Class name supplied as a tag attribute. This is done using the application's ClassLoader. The JDBC specification recommends that implementations register instances of their Driver with the DriverManager during class (static) initialization. As a result, the DriverManager will retain references to classes loaded by the application ClassLoader leading to a memory leak.
Comment 1 Henri Yandell 2010-11-07 02:36:43 UTC
Seems like there are three paths:

1) Document that this exists and point out that people should be using the dataSource parameter anyway.
2) Document and provide a flag that disables this part of the spec - 'DISABLE_SHODDY_SPEC_FEATURES' or its ilk. We could disable x:forEach too :) There are other items in the FAQ, so it's possible there are other crufty pieces of code that should be disabled.
3) Solve the problem while maintaining the feature.

I don't see any solutions for 3, but maybe someone else does.
Comment 2 Konstantin Kolinko 2010-11-07 12:00:40 UTC
1. The only way that I see that this can be solved is to define a listener.

Even if the tag was the first to register a JDBC driver, it cannot unload it immediately after use, but can do it only when the webapp stops. That would be a task for a listener, not for a tag. A tag library can define listeners in its TLD.

(Or a webapp can define its own listener for the purpose of [registering and] deregistering its JDBC drivers.)

2. The JSTL spec explicitly says that such usage is not recommended.

Reading jstl-1_2-mrel2-spec.pdf, chapter 10.6 "<sql:setDataSource>":

As mentioned in Section 10.1.1, using JDBC’s DriverManager class to access a
database is intended for prototyping purposes only because it does not provide
connection management features one can expect from a properly designed
DataSource object.
Comment 3 Jeremy Boynes 2010-12-21 22:51:28 UTC
Resolving per Henri's option 1 that this is a "feature" and the advice is to use a DataSource from the container rather than a raw JDBC Driver.