Kaynağa Gözat

!199 merge
Merge pull request !199 from AE86/wuji-dev

AE86 1 yıl önce
ebeveyn
işleme
532d64e6be

+ 8 - 0
dbsyncer-biz/src/main/java/org/dbsyncer/biz/MappingService.java

@@ -1,6 +1,7 @@
 package org.dbsyncer.biz;
 
 import org.dbsyncer.biz.vo.MappingVo;
+import org.dbsyncer.parser.model.Connector;
 
 import java.util.List;
 import java.util.Map;
@@ -69,4 +70,11 @@ public interface MappingService {
      */
     String stop(String id);
 
+    /**
+     * 刷新表
+     *
+     * @param connector
+     */
+    void refreshTables(Connector connector);
+
 }

+ 10 - 0
dbsyncer-biz/src/main/java/org/dbsyncer/biz/impl/MappingServiceImpl.java

@@ -9,6 +9,7 @@ import org.dbsyncer.biz.vo.ConnectorVo;
 import org.dbsyncer.biz.vo.MappingVo;
 import org.dbsyncer.biz.vo.MetaVo;
 import org.dbsyncer.common.snowflake.SnowflakeIdWorker;
+import org.dbsyncer.common.spi.ConnectorMapper;
 import org.dbsyncer.common.util.CollectionUtils;
 import org.dbsyncer.common.util.JsonUtil;
 import org.dbsyncer.common.util.StringUtil;
@@ -272,4 +273,13 @@ public class MappingServiceImpl extends BaseServiceImpl implements MappingServic
         }
     }
 
+    @Override
+    public void refreshTables(Connector connector) {
+        // 刷新数据表
+        ConnectorMapper connectorMapper = manager.connect(connector.getConfig());
+        List<Table> table = manager.getTable(connectorMapper);
+        connector.setTable(table);
+        manager.editConfigModel(connector);
+    }
+
 }

+ 59 - 15
dbsyncer-connector/src/main/java/org/dbsyncer/connector/database/ds/SimpleDataSource.java

@@ -12,11 +12,31 @@ import java.sql.SQLFeatureNotSupportedException;
 import java.time.Instant;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantLock;
 import java.util.logging.Logger;
 
 public class SimpleDataSource implements DataSource, AutoCloseable {
 
-    private final BlockingQueue<SimpleConnection> pool = new LinkedBlockingQueue<>(300);
+    /**
+     * 默认最大连接
+     */
+    private final int MAX_IDLE = 300;
+
+    /**
+     * 连接上限后最大等待时间(秒)
+     */
+    private final int MAX_WAIT_SECONDS = 3;
+
+    /**
+     * 活跃连接数
+     */
+    private AtomicInteger activeNum = new AtomicInteger(0);
+
+    private final ReentrantLock lock = new ReentrantLock();
+
+    private final BlockingQueue<SimpleConnection> pool = new LinkedBlockingQueue<>(MAX_IDLE);
     /**
      * 有效期(毫秒),默认60s
      */
@@ -39,21 +59,35 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
 
     @Override
     public Connection getConnection() throws SQLException {
-        SimpleConnection poll = pool.poll();
-        if (null == poll) {
-            return createConnection();
-        }
-
-        // 连接无效
-        if (!poll.isValid(VALID_TIMEOUT_SECONDS)) {
-            return createConnection();
-        }
+        try {
+            lock.lock();
+            //如果当前连接数大于或等于最大连接数
+            if (activeNum.get() >= MAX_IDLE) {
+                //等待3秒
+                TimeUnit.SECONDS.sleep(MAX_WAIT_SECONDS);
+                if (activeNum.get() >= MAX_IDLE) {
+                    throw new ConnectorException(String.format("数据库连接数超过上限%d,url=%s", MAX_IDLE, url));
+                }
+            }
+            SimpleConnection poll = pool.poll();
+            if (null == poll) {
+                return createConnection();
+            }
+            // 连接无效
+            if (!poll.isValid(VALID_TIMEOUT_SECONDS)) {
+                return createConnection();
+            }
 
-        // 连接过期
-        if (isExpired(poll)) {
-            return createConnection();
+            // 连接过期
+            if (isExpired(poll)) {
+                return createConnection();
+            }
+            return poll;
+        } catch (InterruptedException e) {
+            throw new ConnectorException(e);
+        } finally {
+            lock.unlock();
         }
-        return poll;
     }
 
     @Override
@@ -103,6 +137,9 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
 
     public void close(Connection connection) {
         if (connection != null && connection instanceof SimpleConnection) {
+
+            activeNum.decrementAndGet();
+
             SimpleConnection simpleConnection = (SimpleConnection) connection;
             // 连接过期
             if (isExpired(simpleConnection)) {
@@ -132,7 +169,14 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
      * @throws SQLException
      */
     private SimpleConnection createConnection() throws SQLException {
-        return new SimpleConnection(DatabaseUtil.getConnection(driverClassName, url, username, password), StringUtil.equals(driverClassName, "oracle.jdbc.OracleDriver"));
+        SimpleConnection simpleConnection = null;
+        try {
+            simpleConnection = new SimpleConnection(DatabaseUtil.getConnection(driverClassName, url, username, password), StringUtil.equals(driverClassName, "oracle.jdbc.OracleDriver"));
+            activeNum.incrementAndGet();
+        } catch (SQLException e) {
+            throw new ConnectorException(e);
+        }
+        return simpleConnection;
     }
 
 }

+ 15 - 0
dbsyncer-web/src/main/java/org/dbsyncer/web/controller/index/MappingController.java

@@ -3,6 +3,7 @@ package org.dbsyncer.web.controller.index;
 import org.dbsyncer.biz.ConnectorService;
 import org.dbsyncer.biz.MappingService;
 import org.dbsyncer.biz.TableGroupService;
+import org.dbsyncer.biz.vo.MappingVo;
 import org.dbsyncer.biz.vo.RestResult;
 import org.dbsyncer.web.controller.BaseController;
 import org.slf4j.Logger;
@@ -134,4 +135,18 @@ public class MappingController extends BaseController {
         }
     }
 
+    @PostMapping(value = "/refreshTables")
+    @ResponseBody
+    public RestResult refreshTables(@RequestParam(value = "id") String id) {
+        try {
+            MappingVo mapping = mappingService.getMapping(id);
+            mappingService.refreshTables(mapping.getSourceConnector());
+            mappingService.refreshTables(mapping.getTargetConnector());
+            return RestResult.restSuccess(id);
+        } catch (Exception e) {
+            logger.error(e.getLocalizedMessage(), e);
+            return RestResult.restFail(e.getMessage());
+        }
+    }
+
 }

+ 6 - 1
dbsyncer-web/src/main/resources/public/mapping/editTable.html

@@ -56,7 +56,12 @@
 
     <div class="form-group">
         <div class="row">
-            <div class="col-md-10"></div>
+            <div class="col-md-2">&nbsp;
+                <button id="refreshTableBtn" type="button" class="btn btn-default" th:title="刷新数据表" th:tableGroupId="${mapping?.id}">
+                    <span class="fa fa-refresh"></span>刷新数据表
+                </button>
+            </div>
+            <div class="col-md-8"></div>
             <div class="col-md-2 text-right">
                 <button id="tableGroupDelBtn" th:mappingId="${mapping?.id}" type="button" class="btn btn-default" disabled="disabled">
                     <span class="fa fa-remove"></span>删除

+ 18 - 0
dbsyncer-web/src/main/resources/static/js/mapping/edit.js

@@ -201,6 +201,22 @@ function mappingModifyName(){
     });
 }
 
+// 绑定刷新表字段事件
+function bindRefreshTablesClick() {
+    let $refreshBtn = $("#refreshTableBtn");
+    $refreshBtn.bind('click', function(){
+        let id = $(this).attr("tableGroupId");
+        doPoster("/mapping/refreshTables", {'id': id}, function (data) {
+            if (data.success == true) {
+                bootGrowl("刷新数据表成功!", "success");
+                doLoader('/mapping/page/edit?id=' + id);
+            } else {
+                bootGrowl(data.resultValue, "danger");
+            }
+        });
+    });
+}
+
 $(function () {
     // 绑定同步方式切换事件
     bindMappingModelChange();
@@ -215,6 +231,8 @@ $(function () {
     initMultipleInputTags();
     // 绑定删除表关系点击事件
     bindMappingTableGroupDelClick();
+    //绑定刷新数据表按钮点击事件
+    bindRefreshTablesClick();
 
     // 初始化select插件
     initSelectIndex($(".select-control-table"), -1);