Sfoglia il codice sorgente

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

AE86 1 anno fa
parent
commit
862cc61225

+ 28 - 14
dbsyncer-connector/src/main/java/org/dbsyncer/connector/database/ds/SimpleDataSource.java

@@ -29,6 +29,11 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
      */
     private final int MAX_WAIT_SECONDS = 3;
 
+    /**
+     * 从缓存队列获取连接次数
+     */
+    private final int MAX_PULL_TIME = 20;
+
     /**
      * 活跃连接数
      */
@@ -69,20 +74,23 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
                     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();
+            int time = MAX_PULL_TIME;
+            while (time-- > 0){
+                SimpleConnection poll = pool.poll();
+                if (null == poll) {
+                    return createConnection();
+                }
+                // 连接无效或过期, 直接关闭连接
+                if (!poll.isValid(VALID_TIMEOUT_SECONDS) || isExpired(poll)) {
+                    closeQuietly(poll);
+                    continue;
+                }
+                // 返回缓存连接
+                return poll;
             }
 
-            // 连接过期
-            if (isExpired(poll)) {
-                return createConnection();
-            }
-            return poll;
+            // 兜底方案,保证一定能获取连接
+            return createConnection();
         } catch (InterruptedException e) {
             throw new ConnectorException(e);
         } finally {
@@ -140,8 +148,7 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
             SimpleConnection simpleConnection = (SimpleConnection) connection;
             // 连接过期
             if (isExpired(simpleConnection)) {
-                simpleConnection.close();
-                activeNum.decrementAndGet();
+                closeQuietly(simpleConnection);
                 return;
             }
 
@@ -150,6 +157,13 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
         }
     }
 
+    private void closeQuietly(SimpleConnection connection) {
+        if (connection != null) {
+            connection.close();
+            activeNum.decrementAndGet();
+        }
+    }
+
     /**
      * 连接是否过期
      *