AE86 3 years ago
parent
commit
c230bf65e3

+ 4 - 8
dbsyncer-connector/src/main/java/org/dbsyncer/connector/database/AbstractDatabaseConnector.java

@@ -174,13 +174,9 @@ public abstract class AbstractDatabaseConnector extends AbstractConnector implem
         try {
             // 2、设置参数
             execute = connectorMapper.execute(databaseTemplate ->
-                    databaseTemplate.update(sql, (ps) -> {
-                        Field f = null;
-                        for (int i = 0; i < size; i++) {
-                            f = fields.get(i);
-                            SetterEnum.getSetter(f.getType()).set(databaseTemplate.getConnection(), ps, i + 1, f.getType(), data.get(f.getName()));
-                        }
-                    })
+                    databaseTemplate.update(sql, (ps) ->
+                            batchRowsSetter(databaseTemplate.getConnection(), ps, fields, size, data)
+                    )
             );
         } catch (Exception e) {
             // 记录错误数据
@@ -572,7 +568,7 @@ public abstract class AbstractDatabaseConnector extends AbstractConnector implem
                 }
             }
         }
-        if(!TableTypeEnum.isView(table.getType())){
+        if (!TableTypeEnum.isView(table.getType())) {
             throw new ConnectorException("Table primary key can not be empty.");
         }
         return "";

+ 18 - 3
dbsyncer-connector/src/main/java/org/dbsyncer/connector/database/ds/SimpleDataSource.java

@@ -28,12 +28,23 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
 
     @Override
     public Connection getConnection() throws SQLException {
-        synchronized (pool) {
+        SimpleConnection poll = null;
+        int i = 3;
+        do {
             if (pool.isEmpty()) {
-                pool.offer(new SimpleConnection(this, DatabaseUtil.getConnection(url, username, password)));
+                pool.offer(createConnection());
             }
-            return pool.poll();
+            poll = pool.poll();
+            if (null != poll) {
+                break;
+            }
+            i--;
+        } while (i > 1);
+
+        if (null == poll) {
+            poll = createConnection();
         }
+        return poll;
     }
 
     @Override
@@ -81,6 +92,10 @@ public class SimpleDataSource implements DataSource, AutoCloseable {
         pool.forEach(c -> c.closeQuietly());
     }
 
+    private SimpleConnection createConnection() throws SQLException {
+        return new SimpleConnection(this, DatabaseUtil.getConnection(url, username, password));
+    }
+
     public BlockingQueue<SimpleConnection> getPool() {
         return pool;
     }