天天看點

register the mysql_registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it wh...

問題是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()

packagecn.com.do1.component.common.jdbc;

importorg.apache.commons.dbcp.BasicDataSource;

importjava.sql.DriverManager;

importjava.sql.SQLException;

importjava.sql.SQLFeatureNotSupportedException;

importjava.util.logging.Logger;

publicclassBasicDataSourceExtextendsBasicDataSource{

@Override

publicT unwrap(Classiface)throwsSQLException{

// TODO Auto-generated method stub

returnnull;

}

@Override

publicbooleanisWrapperFor(Class>iface)throwsSQLException{

// TODO Auto-generated method stub

returnfalse;

}

@Override

publicsynchronizedvoidclose()throwsSQLException{

DriverManager.deregisterDriver(DriverManager.getDriver(url));

super.close();

}

@Override

publicLoggergetParentLogger()throwsSQLFeatureNotSupportedException{

returnnull;

}

}

然後用BasicDataSourceExt 替換spring配置檔案中的資料源bean的class