我在 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 ,但该问题已在不久前得到解决。
最佳答案
您可以简单地忽略它,或者您可以创建一个 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注解。
关于java - 如何修复 org.apache.derby.jdbc.AutoloadedDriver 在 Tomcat 关闭时注销失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768122/