xpc
2024-01-30 cc9ebffc57e6343745cb1eadc47360d6107936bc
product-server-datasource/src/main/java/com/product/datasource/connection/ConnectionManager.java
@@ -200,46 +200,48 @@
        }
        Boolean enabling = Global.getPropertyToBoolean("data.system.oracle.connection-pool.enabling", "false");
        if (enabling) {
            DruidDataSource druidDataSource = DB_DRUID_DATA_SOURCE_MAP.get(ArrayUtil.join(params, ","));
            if (druidDataSource == null || druidDataSource.isClosed()) {
                druidDataSource = new DruidDataSource();
                druidDataSource.setUrl(url);
                druidDataSource.setUsername(dbe.getUserName());
                druidDataSource.setPassword(dbe.getPassWord());
                // 初始化时建立物理连接的个数
                druidDataSource.setInitialSize(getProperty("data.system.oracle.connection-pool.initial-size", 10));
                // 最大活动连接数
                druidDataSource.setMaxActive(getProperty("data.system.oracle.connection-pool.max-active", 100));
                // 最小空闲连接数
                druidDataSource.setMinIdle(getProperty("data.system.oracle.connection-pool.min-idle", 20));
                // 校验查询语句
                druidDataSource.setValidationQuery(dbe.getDataBaseType().getValidationQuery());
                // 当连接空闲时是否测试连接有效性
                druidDataSource.setTestWhileIdle(true);
                // 两次空闲连接清除之间的时间间隔
                druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
                druidDataSource.setMaxWait(60 * 1000);
                druidDataSource.setPoolPreparedStatements(true);
                druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(100);
                druidDataSource.setRemoveAbandoned(true);
                //半小时强制归还连接
                druidDataSource.setRemoveAbandonedTimeout(60 * 30);
                DB_DRUID_DATA_SOURCE_MAP.put(ArrayUtil.join(params, ","), druidDataSource);
                try {
                    //获获取连接
                    Connection connection = druidDataSource.getConnection();
                    //测试连接
                    if (!connectionValidity(connection, dbe.getDataBaseType())) {
            synchronized (url.intern()){
                DruidDataSource druidDataSource = DB_DRUID_DATA_SOURCE_MAP.get(ArrayUtil.join(params, ","));
                if (druidDataSource == null || druidDataSource.isClosed()) {
                    druidDataSource = new DruidDataSource();
                    druidDataSource.setUrl(url);
                    druidDataSource.setUsername(dbe.getUserName());
                    druidDataSource.setPassword(dbe.getPassWord());
                    // 初始化时建立物理连接的个数
                    druidDataSource.setInitialSize(getProperty("data.system.oracle.connection-pool.initial-size", 10));
                    // 最大活动连接数
                    druidDataSource.setMaxActive(getProperty("data.system.oracle.connection-pool.max-active", 100));
                    // 最小空闲连接数
                    druidDataSource.setMinIdle(getProperty("data.system.oracle.connection-pool.min-idle", 20));
                    // 校验查询语句
                    druidDataSource.setValidationQuery(dbe.getDataBaseType().getValidationQuery());
                    // 当连接空闲时是否测试连接有效性
                    druidDataSource.setTestWhileIdle(true);
                    // 两次空闲连接清除之间的时间间隔
                    druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
                    druidDataSource.setMaxWait(60 * 1000);
                    druidDataSource.setPoolPreparedStatements(true);
                    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(100);
                    druidDataSource.setRemoveAbandoned(true);
                    //半小时强制归还连接
                    druidDataSource.setRemoveAbandonedTimeout(60 * 30);
                    DB_DRUID_DATA_SOURCE_MAP.put(ArrayUtil.join(params, ","), druidDataSource);
                    try {
                        //获获取连接
                        Connection connection = druidDataSource.getConnection();
                        //测试连接
                        if (!connectionValidity(connection, dbe.getDataBaseType())) {
                            throw new BaseException(ErrorCode.GET_CONNECTION_FAIL);
                        }
                        return connection;
                    } catch (BaseException e) {
                        throw e;
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("获取链接失败", e);
                        log.error(url);
                        throw new BaseException(ErrorCode.GET_CONNECTION_FAIL);
                    }
                    return connection;
                } catch (BaseException e) {
                    throw e;
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("获取链接失败", e);
                    log.error(url);
                    throw new BaseException(ErrorCode.GET_CONNECTION_FAIL);
                }
            }
        }