cheng
2024-01-28 31016f01ec27432295e77d1720b19cd5fd37ce72
product-server-datasource/src/main/java/com/product/datasource/connection/ConnectionManager.java
@@ -1,5 +1,9 @@
package com.product.datasource.connection;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Maps;
import com.product.common.lang.StringUtils;
import com.product.core.config.Global;
import com.product.core.exception.BaseException;
@@ -9,7 +13,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @Author cheng
@@ -19,6 +26,8 @@
public class ConnectionManager {
    static Logger log = LoggerFactory.getLogger(ConnectionManager.class);
    private static final Map<String, DruidDataSource> DB_DRUID_DATA_SOURCE_MAP = new ConcurrentHashMap<>();
    public static void main(String[] args) {
        DataBaseEntity DataBaseEntity = new DataBaseEntity(DataBaseType.MYSQL.getValue());
@@ -179,16 +188,72 @@
     */
    private static Connection getOracleConnection(DataBaseEntity dbe) {
        String url;
        String[] params = {dbe.getIp(), dbe.getPort(), null};
        if (!StringUtils.isEmpty(dbe.getSid())) {
            url = String.format("jdbc:oracle:thin:@%s:%s:%s", dbe.getIp(), dbe.getPort(), dbe.getSid());
            params[2] = dbe.getSid();
        } else if (!StringUtils.isEmpty(dbe.getServerName())) {
            url = String.format("jdbc:oracle:thin:@//%s:%s/%s", dbe.getIp(), dbe.getPort(), dbe.getServerName());
            params[2] = dbe.getServerName();
        } else {
            throw new BaseException(ErrorCode.GET_ORACLE_SID_SERVERNAME_EMPTY);
        }
        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())) {
                        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 getConnection(url, dbe);
    }
    private static int getProperty(String key, int defaultValue) {
        String systemConfig = Global.getSystemConfig(key, defaultValue + "");
        if (!NumberUtil.isNumber(systemConfig)) {
            return defaultValue;
        }
        return NumberUtil.parseInt(systemConfig);
    }
    /**
     * 获取Informix 连接
     *