From 3a07a59e00a177d1adc179575b1ded8d763ea292 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 29 Dec 2015 00:22:35 +0300 Subject: try unregister jdbc --- src/main/java/com/juick/CrosspostComponent.java | 22 ++++++++++++++++++++++ src/main/java/com/juick/PushComponent.java | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index 0b5a5bc2..4e4264cb 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -36,8 +36,10 @@ import java.net.URL; import java.net.URLEncoder; import java.security.Key; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.Enumeration; import java.util.Properties; import java.util.UUID; import java.util.concurrent.ExecutorService; @@ -97,6 +99,26 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea @Override public void contextDestroyed(ServletContextEvent sce) { + // Now deregister JDBC drivers in this context's ClassLoader: + // Get the webapp's ClassLoader + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + // Loop through all drivers + Enumeration drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver driver = drivers.nextElement(); + if (driver.getClass().getClassLoader() == cl) { + // This driver was registered by the webapp's ClassLoader, so deregister it: + try { + logger.info(String.format("Deregistering JDBC driver %s", driver.toString())); + DriverManager.deregisterDriver(driver); + } catch (SQLException ex) { + logger.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); + } + } else { + // driver was not registered by the webapp's ClassLoader and may be in use elsewhere + logger.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); + } + } executorService.shutdown(); logger.info("component destroyed"); } diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index be089928..9cc045e1 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -43,9 +43,11 @@ import java.net.HttpURLConnection; import java.net.Socket; import java.net.URL; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Enumeration; import java.util.List; import java.util.Properties; import java.util.concurrent.ExecutorService; @@ -97,6 +99,26 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe @Override public void contextDestroyed(ServletContextEvent sce) { + // Now deregister JDBC drivers in this context's ClassLoader: + // Get the webapp's ClassLoader + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + // Loop through all drivers + Enumeration drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver driver = drivers.nextElement(); + if (driver.getClass().getClassLoader() == cl) { + // This driver was registered by the webapp's ClassLoader, so deregister it: + try { + logger.info(String.format("Deregistering JDBC driver %s", driver.toString())); + DriverManager.deregisterDriver(driver); + } catch (SQLException ex) { + logger.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); + } + } else { + // driver was not registered by the webapp's ClassLoader and may be in use elsewhere + logger.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); + } + } executorService.shutdown(); logger.info("component destroyed"); } -- cgit v1.2.3