关闭Tomcat时,是否要手动关闭c3p0创建出来的datasource

乐山ing 发布于 2017/02/13 16:01
阅读 518
收藏 0

一个项目通过c3p0获得连接池,相关代码如下:

public class JdbcUtil {
	
	// 连接池的核心类
	private static ComboPooledDataSource dataSource;
	
	//初始化连接池相关参数
	static{
		try {
			dataSource = new ComboPooledDataSource();
			dataSource.setDriverClass(PropertiesUtil.getValue("jdbcName"));
			dataSource.setJdbcUrl(PropertiesUtil.getValue("dbUrl"));
			dataSource.setUser(PropertiesUtil.getValue("dbUserName"));
			dataSource.setPassword(PropertiesUtil.getValue("dbPassword"));
			dataSource.setInitialPoolSize(5);
			dataSource.setMinPoolSize(5);
			dataSource.setMaxPoolSize(20);
			// 以下两句用于设置自动重连
			dataSource.setIdleConnectionTestPeriod(10);
			dataSource.setTestConnectionOnCheckin(true);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

        //下面是getDataSource,getConnection等方法
}

关闭Tomcat的时候提示:

警告: The web application [uavmonitor] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.util.TimerThread.mainLoop(Timer.java:552)
 java.util.TimerThread.run(Timer.java:505)
二月 13, 2017 3:51:45 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [uavmonitor] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
二月 13, 2017 3:51:45 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [uavmonitor] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
二月 13, 2017 3:51:45 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [uavmonitor] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)

主要是这几句:

1.

警告: The web application [uavmonitor] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)

 

2.

The web application [uavmonitor] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.util.TimerThread.mainLoop(Timer.java:552)
 java.util.TimerThread.run(Timer.java:505)

 

查询c3p0官方文档http://www.mchange.com/projects/c3p0/#cleaning

说是“ c3p0 spawns a variety of Threads (helper threads, java.util.Timer threads) ”,结合上面的出错信息,推断应该是Tomcat关闭的时候没有关闭JdbcUtil类创建出来的c3p0的datasource。

写了一个监听器

try {
			
			DataSources.destroy(JdbcUtil.getDataSource());
			System.out.println("关闭数据库连接池成功!");
		} catch (SQLException e) {
			e.printStackTrace();
		}

可以解决相关问题。

 

然而Tomcat关闭前清理c3p0的datasource应该是一个很常见的需求,有没有更优雅一点的方法来做这种清理。或者说应该在哪里配置一下,就可以让tomcat关闭前自动destory这个datasource??

加载中
1
akeng
akeng
Runtime.getRuntime().addShutdownHook(new Thread(){
   
});

添加一个虚拟机关闭的钩子函数,来关闭数据源??

乐山ing
乐山ing
查了一些资料确实要手动在监听器里释放。 spring 等框架对数据源的管理真好
1
moliunian
moliunian

orm框架好用

0
Eric_林
Eric_林

如果你的tomcat是被突然中断的,程序再怎么样都没用

乐山ing
乐山ing
所以,如果可能,任何时候请正常关闭tomcat
返回顶部
顶部