异常:A web application registered the JBDC driver

异常信息##

2011-9-1 0:15:11 org.apache.catalina.startup.Catalina start
信息: Server startup in 35866 ms
2011-9-1 2:05:43 org.apache.coyote.http11.Http11Protocol pause
信息: Pausing Coyote HTTP/1.1 on http-8080
2011-9-1 2:05:44 org.apache.catalina.core.StandardService stop
信息: Stopping service Catalina
2011-9-1 2:05:44 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [] 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.
2011-9-1 2:05:45 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080

一个web应用程序注册的JBDC驱动程序[com.mysql.jdbc.Driver],但Web应用程序时停止时未能注销。为了防止内存泄漏,JDBC驱动程序已被强行注册。

解决办法##

使用Sping org.apache.commons.dbcp.BasicDataSource 配置数据源时警告:

SEVERE: A web application registered the JBDC 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.

问题是tomcat的版本问题,tomcat新检测机制导致的这个问题,换版本可以解决问题,但不建议这么做,租用服务器不是你说换就换的。

其实问题根源是BasicDataSource,BasicDataSource类close()的一个Bug。

BasicDataSource's method close() doesn't deregister JDBC driver. This causes permgen memory leaks in web server environments, during context reloads. For example, using Tomcat 6.0.26 with Spring, and BasicDataSource declared in Spring context, there is a message printed at web application reload:
SEVERE: A web application registered the JBDC 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.

继承org.apache.commons.dbcp.BasicDataSource 重写close():

import java.sql.DriverManager; 
import java.sql.SQLException; 

import org.apache.commons.dbcp.BasicDataSource; 

public class FixedBasicDataSource extends BasicDataSource { 

    @Override 
    public  T unwrap(Class iface) throws SQLException { 
        // TODO Auto-generated method stub 
        return null; 
    } 
    
    @Override 
    public boolean isWrapperFor(Class iface) throws SQLException { 
        // TODO Auto-generated method stub 
        return false; 
    } 
    @Override  
    public synchronized void close() throws SQLException {
        // 手动注销
        DriverManager.deregisterDriver(DriverManager.getDriver(url));  
        super.close();  
    }  
} 

如果你不知道url,那就这样:

Enumeration drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
    Driver driver = drivers.nextElement();
    try {
        // 手动注销
        DriverManager.deregisterDriver(driver);
        Logger.getLogger(this.class.getName()).log(Level.INFO,String.format("deregistering jdbc driver: %s",driver), driver);
    } catch (SQLException e) {
        Logger.getLogger(this.class.getName()).log(Level.SEVERE,String.format("Error deregistering driver %s",driver), e);
    }
}

然后用 FixedBasicDataSource 替换spring配置文件中的数据源bean的class【我用的c3p0的数据库连接池的包】。

你可能感兴趣的:(异常:A web application registered the JBDC driver)