From 31016f01ec27432295e77d1720b19cd5fd37ce72 Mon Sep 17 00:00:00 2001
From: cheng <1821349743@qq.com>
Date: 星期日, 28 一月 2024 14:29:00 +0800
Subject: [PATCH] 归档限制每个表的队列数量,oracle 更改创建连接池

---
 product-server-datasource/src/main/java/com/product/datasource/connection/ConnectionManager.java |   65 ++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/product-server-datasource/src/main/java/com/product/datasource/connection/ConnectionManager.java b/product-server-datasource/src/main/java/com/product/datasource/connection/ConnectionManager.java
index 0718459..df184b5 100644
--- a/product-server-datasource/src/main/java/com/product/datasource/connection/ConnectionManager.java
+++ b/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 杩炴帴
      *

--
Gitblit v1.9.2