为避免停止服务器实例时警告XXX未能停止,很可能会造成内存泄漏,手动关闭数据库相关线程。
一、关闭c3p0有2种办法
第1种是applicationContext.xml(spring配置文件)中配置c3p0数据源时添加destroy-method="close"
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
第2种是C3P0Registry.getPooledDataSources()获取数据源,遍历调用DataSources.destroy(pooledDataSource)关闭。
for(Object o:C3P0Registry.getPooledDataSources()){
PooledDataSource pooledDataSource=(PooledDataSource)o;
DataSources.destroy(pooledDataSource);
}
仅仅关闭c3p0是不够的,还要关闭jdbc driver和mysql-cj-abandoned-connection-cleanup。
二、一起关闭c3p0,jdbc,mysql线程
1.自定义一个ContextListener类,在contextDestroyed方法中写手动关闭。
2.在web.xml中添加该监听器。
public class ContextListener implements ServletContextListener