Web应用程序停止时无法注销JDBC

时间:2015-09-13 18:44:39

标签: jsp tomcat servlets jdbc

WARNING [http-nio-8181-exec-550] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc 
The web application [comparateur] registered the JDBC driver [com.mysql.jdbc.Driver] 
but failed to unregister it when the web application was stopped. 
To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

我用过这个课程

package servlets;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.servlet.ServletContextEvent;
import javax.servlet.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) {
 }
 }

和我与DB的联系

private String dbName = "NameDB";
private String user = "root";
private String pass = "PWD";   
private String server = "localhost";

我有同样的问题,但有这个消息   INFO [http-nio-8181-exec-579] org.apache.catalina.core.ApplicationContext.log   HTMLManager:list:列出虚拟主机&#39; localhost&#39;

的上下文

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

我觉得好像你的ServletContextListener实现没有被Tomcat选中。

如果您希望Web应用程序容器选择ServletContextListener实现,则需要执行以下两项操作之一:

  • @WebListener注释添加到班级。
  • 将以下元素添加到web.xml文件中。此元素应该是根<web-app>元素的子元素。

    <listener>
        <listener-class>servlets.MyContextListener</listener-class>
    </listener>