dbcp内存泄漏解决方案

tomcat 6.0.24以上版本关闭时报以下错误:

严重: The web application [/igame] 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.

这是因为dbcp的一个小小的内存泄漏,你可以无视它,也可以去解决它。

更多关于这个问题的讨论:

http://stackoverflow.com/questions/3320400/to-prevent-a-memory-leak-the-jdbc-driver-has-been-forcibly-unregistered

我采用的是改为使用tomcat jdbc pool。

tomcat也使用大篇幅来描述tomcat-jdbc比dbcp好的优点:

http://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html

注意:并不是所有的tomcat版本都包含了tomcat-jdbc.jar,如果发现tomcat_home/lib下没有这个包,自己手工添加上。可能mysql-connector包也需要手工加上。

另一种方法:手动注销driver:

实现ServletContextListener,在contextDestroyed添加:

Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
try {
log.info(“deregister driver :” + driver);
DriverManager.deregisterDriver(driver);
} catch (SQLException e) {
log.error(“deregister dirver error:”, e);
}
}

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>