异常: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> T unwrap(Class<T> 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<Driver> 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的数据库连接池的包】。

转载于:https://my.oschina.net/xianggao/blog/391614

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值