关闭Tomcat时,是否要手动关闭c3p0创建出来的datasource
时间: 2017-02-13来源:开源中国
前景提要
HDC调试需求开发(15万预算),能者速来!>>>
一个项目通过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??

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行