关闭Tomcat时,是否要手动关闭c3p0创建出来的datasource
一个项目通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以,如果可能,任何时候请正常关闭tomcat
如果你的tomcat是被突然中断的,程序再怎么样都没用
orm框架好用
查了一些资料确实要手动在监听器里释放。 spring 等框架对数据源的管理真好