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

发布于 2021-12-04 21:58:54 字数 4328 浏览 852 评论 4

一个项目通过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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

緦唸λ蓇 2021-12-06 12:04:49

所以,如果可能,任何时候请正常关闭tomcat

不再见 2021-12-06 12:03:54

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

挽清梦 2021-12-06 11:51:51

orm框架好用

不乱于心 2021-12-05 19:17:26

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文