zoukankan      html  css  js  c++  java
  • Tomcat中的c3p0数据库连接池的释放

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

    主要是这句:

    警告: 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)
    

    与这句

    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官方文档

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

    解决方案

    写一个监听器,在其public void contextDestroyed方法中进行对c3p0数据源进行关闭就可以解决相关问题。

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

    如果运行在Tomcat中的代码启动了Executor线程池,也需要在适当的地方(比如监听器中的contextDestroyed方法)进行shutdown,否则会造成资源泄露。

    从这个例子中也可以看出,直接使用spring、mybatis等管理数据源是多么省事。

  • 相关阅读:
    synchronized关键字jvm实现及各种锁
    zookeeper选举算法
    git远程操作
    git分支的创建与分支之间合并的底层原理
    员工贷项目总结
    Python之字符串操作
    Python实现购物车的功能
    Python列表学习笔记
    Python中如何使用boolean类型的数据
    Python实现用户登录账户
  • 原文地址:https://www.cnblogs.com/zhrb/p/jsp.html
Copyright © 2011-2022 走看看