3

I'm working on a Java webapp that is to be deployed among others on a common servlet container (Tomcat 7 in this case).

That cleanup code deregisters all its registered JDBC drivers (held in a local variable of a custom database connection manager).

But if I call DriverManager.getDrivers() after that, I see that other drivers are still returned.

Is it fine to deregister those aswell, or is that method returning drivers registered by other applications within the same servlet container (Tomcat in this case)?

watery
  • 5,026
  • 9
  • 52
  • 92
  • Technically you shouldn't touch registered drivers, although things get trickier in the case of managed containers. This is one of the reasons you should use a JNDI `DataSource` or JCA connector instead of `DriverManager` directly from your web application. Also note that `DriverManager` will automatically reload JDBC 4+ compliant drivers on the classpath. – Mark Rotteveel Oct 03 '14 at 15:05
  • 1
    I know that if a tomcat instance contains multiple web applications, and registered itself a driver, it is available for every application. And if one of them unregisters that driver, it become unavailable for all the other applications. That's the reason why I no longer register or unregister anything and tend to use JNDI datasources in production environment. – Serge Ballesta Oct 03 '14 at 15:13
  • @MarkRotteveel Thank you for the suggestion about `Datasource`s, but the database connectivity code is already given and I'm not going to touch it, not now at least. I'm unregistering drivers at application shutdown through a ServletContextListener, but thanks for the tip about autoreloading. – watery Oct 03 '14 at 19:01
  • @SergeBallesta So, `DriverManager` wouldn't be tied to the application classloader? How can I tell which classloader is being used for a specific class? – watery Oct 03 '14 at 19:02
  • @watery The DriverManager *itself* is loaded by that class loader. – user207421 Oct 04 '14 at 01:24
  • @EJP Excuse me, but which one are you talking about? The webapp classloader? – watery Oct 04 '14 at 12:05

1 Answers1

1

Unless Tomcat is complaining about a memory leak due to one of those registered drivers, leave them alone. If Tomcat is complaining then - assuming nothing else is using them - deregistered the offending driver(s).

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
  • Yes, in fact it is. But by looking at [this answer](http://stackoverflow.com/a/7198049/3127111), I see that I shouldn't unregister them from `DriverManager`, since that class is shared between all the web applications. – watery Oct 03 '14 at 20:04