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

一、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();
            }
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值