如何修复org.apache.derby.jdbc.AutoloadedDriver无法在Tomcat关闭时取消注册?

时间:2015-01-04 17:12:10

标签: java tomcat jdbc memory-leaks derby

我在Tomcat关闭期间看到以下错误:

Jan 04, 2015 4:14:31 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [] registered the JDBC driver [org.apache.derby.jdbc.AutoloadedDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

我理解Tomcat强行取消注册驱动程序以防止内存泄漏。

但是,如何修复以及谁负责解决此问题 - 我,Tomcat DBCP或Derby?

我正在使用 Tomcat 7.0.47 Derby 10.11.1.1

Tomcat的context.xml:

<Context>
  <Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"
               driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
               factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
               maxActive="100"
               minIdle="2"
               maxIdle="30"
               maxWait="10000"
               validationQuery="VALUES 1"
               testOnBorrow="true"
               removeAbandoned="true"
               url="jdbc:derby:dev;create=true"
               username="root"
               password="root"
               />
</Context>

我在Google上发现的类似问题引用DERBY-4895,但该问题已在不久前修复过。

1 个答案:

答案 0 :(得分:0)

你可以简单地忽略它,或者你可以创建一个ServletContextListener来取消注册应用程序关闭时的驱动程序:

public class MyContextListener implements ServletContextListener{

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("App shutdown ...");

    System.out.println("Deregistering SQL-Drivers ...");
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        try {
            DriverManager.deregisterDriver(driver);
            System.out.println(driver.getClass().getName());
        } catch (SQLException e) {
            System.err.println("Error deregistering driver " + driver.getClass().getName());
        }
    }
 }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
 }
}

当然,您需要将侦听器添加到web.xml,或使用@WebListener对其进行注释。