• 线程池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();
                }
            }
        }
    
    }
  • 相关阅读:
    Kettle 实现mysql数据库不同表之间数据同步——实验过程
    Kettle ETL 来进行mysql 数据同步——试验环境搭建(表中无索引,无约束,无外键连接的情况)
    并查集知识总结
    c# 线程同步问题(about volatile)
    c# 线程的等待(堵塞)
    net中多线程返回值
    c# 中的 lock monitor mutex Semaphore 的比较
    c#两种同步结构
    links-some-blog
    T-SQL中的APPLY用法
  • 原文地址:https://www.cnblogs.com/yoyotl/p/6593671.html
Copyright © 2020-2023  润新知