首页 > 其他分享 >线程池c3p0和dbcp2的配置初始化实例

线程池c3p0和dbcp2的配置初始化实例

时间:2023-01-04 15:11:57浏览次数:47  
标签:c3p0 Connection static dataSource dbcp2 线程 true public conn

一、c3p0

复制代码
public class ConnectionManager {

    public static ComboPooledDataSource dataSource;
    static {
        try {
            dataSource = new ComboPooledDataSource();
            dataSource.setUser("freeswitch");
            dataSource.setPassword("freeswitch");
            dataSource.setJdbcUrl("jdbc:postgresql://数据库地址:数据库端口/freeswitch");
            dataSource.setDriverClass("org.postgresql.Driver");
            dataSource.setInitialPoolSize(10);
            dataSource.setMinPoolSize(5);
            dataSource.setMaxPoolSize(50);
            dataSource.setMaxStatements(100);
            dataSource.setMaxIdleTime(60);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection3() {
        Connection conn = null;
        if (null != dataSource) {
            try {
                conn = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }
}
复制代码

二、dbcp2

复制代码
public class DataBaseHelper {

    // 保证一个线程一个Connection,线程安全
    private static final ThreadLocal<Connection> connHolder;
    // 线程池
    private static final BasicDataSource dataSource;
    static {
        connHolder = new ThreadLocal<Connection>();
        dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://数据库地址:数据库端口/freeswitch");
        dataSource.setUsername("freeswitch");
        dataSource.setPassword("freeswitch");
        /// 设置空闲和借用的连接的最大总数量,同时可以激活。
        dataSource.setMaxTotal(60);
        // 设置初始大小
        dataSource.setInitialSize(5);
        // 最小空闲连接
        dataSource.setMinIdle(8);
        // 最大空闲连接
        dataSource.setMaxIdle(16);
        // 超时等待时间毫秒
        dataSource.setMaxWaitMillis(2 * 10000);
        // 只会发现当前连接失效,再创建一个连接供当前查询使用
        dataSource.setTestOnBorrow(true);
        // removeAbandonedTimeout :超过时间限制,回收没有用(废弃)的连接(默认为 300秒,调整为180)
        dataSource.setRemoveAbandonedTimeout(180);
        // removeAbandoned :超过removeAbandonedTimeout时间后,是否进
        // 行没用连接(废弃)的回收(默认为false,调整为true)
        // DATA_SOURCE.setRemoveAbandonedOnMaintenance(removeAbandonedOnMaintenance);
        dataSource.setRemoveAbandonedOnBorrow(true);
        // testWhileIdle
        dataSource.setTestOnReturn(true);
        // testOnReturn
        dataSource.setTestOnReturn(true);
        // setRemoveAbandonedOnMaintenance
        dataSource.setRemoveAbandonedOnMaintenance(true);
        // 记录日志
        dataSource.setLogAbandoned(true);
        // 设置自动提交
        dataSource.setDefaultAutoCommit(true);

    }

    /**
     * 获取数据库连接
     */
    public static Connection getConnection() {
        Connection conn = connHolder.get();
        if (conn == null) {
            try {
                conn = dataSource.getConnection();
                System.out.println("get connection success");
            } catch (SQLException e) {
                System.out.println("get connection failure:" + e);
            } finally {
                connHolder.set(conn);
            }
        }
        return conn;
    }

    /**
     * 关闭数据库连接
     */
    public static void closeConnection() {
        Connection conn = connHolder.get();
        if (conn != null) {
            try {
                conn.close();
                System.out.println("close connection success");
            } catch (SQLException e) {
                System.out.println("close connection failure:" + e);
                throw new RuntimeException(e);
            } finally {
                connHolder.remove();
            }
        }
    }

}
复制代码

标签:c3p0,Connection,static,dataSource,dbcp2,线程,true,public,conn
From: https://www.cnblogs.com/kn-zheng/p/17024873.html

相关文章

  • dbcp2连接池获取数据库连接Connection
    一、先来看看手工创建的方式publicstaticConnectiongetConnection(){Connectionconn=null;try{Class.forName("com.mysql.......
  • Java线程生命周期
    java.lang.Thread类包含一个静态枚举,它定义了它的潜在状态。在任何给定的时间点内,线程只能处于以下状态之一:NEW–一个新创建的线程,尚未开始执行RUNNABLE–正在运行......
  • SSH框架学习(十、Junit+GroboUtils进行多线程测试)
    Junit4不能模拟多线程的情况,需要其他支持,我用的是GroboUtils,最新版本5,下载地址:​​http://groboutils.sourceforge.net/downloads.html​​GroboUtils测试的代码是用网上抄......
  • python中进程与线程
    frommultiprocessingimportProcessfromosimportgetpidfromrandomimportrandintfromtimeimporttime,sleep#实现数据共享方式管道.套接字共享内存区#......
  • SimpleDateFormat的线程安全问题和ThreadLocal的使用
     在项目中遇到了用的同一个静态sdf 两个接口同时触发 用的同一个sdf 会出现线程安全的问题。   文章资料转载SimpleDateFormat的线程安全问题和ThreadLocal......
  • 线程方法
    1,线程调度lb.setPriority(Thread.MAX_PRIORITY);lb.getPriority();设置线程优先级(属于线程控制的方法,但是基本不会使用,了解一下,面试的时候有可能会问到)注意......
  • JAVA线程池-工具类
    packagecom.javacode2022.util;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.collections4.CollectionUtils;importjava.util.List;importjava.......
  • 线程池测试类-TEST
    packagecom.javacode2022.threadpool;importlombok.extern.slf4j.Slf4j;importjava.util.ArrayList;importjava.util.List;importjava.util.concurrent.CountD......
  • jstack和线程dump分析
          jstack命令的语法格式:jstack <pid>。可以用jps查看java进程id。这里要注意的是:      1.不同的JAVA虚机的线程DUMP的创建方法和文件格式是不一样的,......
  • 进程和线程关系
     进程是系统进行资源分配的基本单位,有独立的内存地址空间;线程是CPU调度的基本单位,没有单独地址空间,有独立的栈,局部变量,寄存器,程序计数器等只有进程有自己的addressspace......