the JDBC Driver has been forcibly unregistered


My Tomcat instance keeps complaining about de-registration failures of JDBC drivers whenever my web app re-deployed.

The web application [some-fine-webapp] registered the JDBC driver [some.nasty.JdbcDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

This is actually not a problem. That message means that the Tomcat took care of something by himself.

I pay or I owe.

@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(final ServletContextEvent sce) {
    }

    @Override
    public void contextDestroyed(final ServletContextEvent sce) {
        // Is this safe? De-registering while enumerating?
        for (final Enumeration<Driver> drivers = DriverManager.getDrivers();
             drivers.hasMoreElements();) {
            try {
                DriverManager.deregisterDriver(drivers.nextElement());
            } catch (final SQLException sqle) {
            }
        }
    }
}

Well, well, well.

Collections.list(DriverManager.getDrivers()).forEach(d -> {
    try {
        DriverManager.deregisterDriver(d);
    } catch (final SQLException sqle) {
    }
});
Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s