On Monday 21 December 2009 12:04:59 Mark Thomas wrote:
> On 21/12/2009 10:53, Rainer Frey wrote:
[...]
> > but I hoped that someone would take a
> > look at issue https://issues.apache.org/bugzilla/show_bug.cgi?id=48214
> > before a release is made 
>
> I hadn't forgotten that one - I just hadn't got around to looking at it.
> I have a few ideas about what might be going on.
I assumed so, but I was afraid there wouldn't be time before the release.

> My guess is that you are relying on the auto-driver registration
> process. It is this process that triggers the memory leak so Tomcat now
> forcibly de-registers any drivers the JVM auto-registers.

I do, mostly on the presence of a static initializer block in the driver 
class, as most drivers that we use do not yet implement JDBC4 fully.
What exactly is the memory leak? Is relying on the auto-registration 
discouraged altogether, or is the service definition method of JDBC 4 safe?

> If you get a chance before I get to it later today, try using a context
> listener to register and de-register the driver when your app starts and
> stops and let us know how you get on.

I implemented the ServletContextListener below, and I don't get any exceptions 
anymore. But I'm not sure about this thing. Are drivers required by spec to 
provide a public parameterless constructor, and is it safe to create and 
manage instances of a Driver on my own? Or do I need a separate instantiation 
mechanism for each supported driver? I always regarded  these things as 
internal to the driver implementation. Maybe you were thinking of a different 
approach to deregister and register a driver that I just don't see?

Here's the code (minus exception handling and logging):
public class JdbcDriverRegistrationListener implements ServletContextListener
{
  private Driver driver;

  public void contextInitialized( ServletContextEvent evt )
  {
    ServletContext context = evt.getServletContext();
    String driverName = context.getInitParameter( "jdbc.driver" );
    try
    {
      Class<Driver> driverClass = (Class<Driver>)Class.forName( driverName );
      this.driver = driverClass.newInstance();
      DriverManager.registerDriver( this.driver );
    }
    catch( Exception x )
    {}          
 }

  public void contextDestroyed( ServletContextEvent evt )
  {
     DriverManager.deregisterDriver( this.driver );
  }
}

> Cheers,
>
> Mark

Rainer

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

Reply via email to