Skip to content

Commit c998b3f

Browse files
nakeivenguodong.zhang
and
guodong.zhang
authored
Feature/adapter postgresql 20250124 (alibaba#5390)
* adapter tcp模式mysql数据全量同步至postgresql时 报错 fix issue 2146 * fix issue 1980 * feat 增加 当dbType 为 postgresql时,返回双引号(避免表名为postgresql的关键字时,sql查询报错) --------- Co-authored-by: guodong.zhang <zgd8988@126.com>
1 parent c15129b commit c998b3f

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

client-adapter/common/src/main/java/com/alibaba/otter/canal/client/adapter/support/Util.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import javax.sql.DataSource;
1818

19+
import com.alibaba.druid.pool.DruidDataSource;
1920
import org.apache.commons.lang.StringUtils;
2021
import org.joda.time.DateTime;
2122
import org.joda.time.DateTimeZone;
@@ -38,7 +39,15 @@ public class Util {
3839
public static Object sqlRS(DataSource ds, String sql, Function<ResultSet, Object> fun) {
3940
try (Connection conn = ds.getConnection();
4041
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
41-
stmt.setFetchSize(Integer.MIN_VALUE);
42+
43+
DruidDataSource druidDataSource = (DruidDataSource) ds;
44+
if ("postgresql".equals(druidDataSource.getDbType())) {
45+
conn.setAutoCommit(false);
46+
stmt.setFetchSize(1000);
47+
} else {
48+
stmt.setFetchSize(Integer.MIN_VALUE);
49+
}
50+
4251
try (ResultSet rs = stmt.executeQuery(sql)) {
4352
return fun.apply(rs);
4453
}
@@ -52,7 +61,13 @@ public static Object sqlRS(DataSource ds, String sql, List<Object> values, Funct
5261
try (Connection conn = ds.getConnection()) {
5362
try (PreparedStatement pstmt = conn
5463
.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
55-
pstmt.setFetchSize(Integer.MIN_VALUE);
64+
DruidDataSource druidDataSource = (DruidDataSource) ds;
65+
if ("postgresql".equals(druidDataSource.getDbType())) {
66+
conn.setAutoCommit(false);
67+
pstmt.setFetchSize(1000);
68+
} else {
69+
pstmt.setFetchSize(Integer.MIN_VALUE);
70+
}
5671
if (values != null) {
5772
for (int i = 0; i < values.size(); i++) {
5873
pstmt.setObject(i + 1, values.get(i));

client-adapter/rdb/src/main/java/com/alibaba/otter/canal/client/adapter/rdb/service/RdbSyncService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private void insert(BatchExecutor batchExecutor, MappingConfig config, SingleDml
294294
batchExecutor.execute(insertSql.toString(), values);
295295
} catch (SQLException e) {
296296
if (skipDupException
297-
&& (e.getMessage().contains("Duplicate entry") || e.getMessage().startsWith("ORA-00001:"))) {
297+
&& (e.getMessage().contains("Duplicate entry") || e.getMessage().contains("duplicate key") || e.getMessage().startsWith("ORA-00001:"))) {
298298
// ignore
299299
// TODO 增加更多关系数据库的主键冲突的错误码
300300
} else {

client-adapter/rdb/src/main/java/com/alibaba/otter/canal/client/adapter/rdb/support/SyncUtil.java

+3
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ public static String getBacktickByDbType(String dbTypeName) {
313313
case mariadb:
314314
case oceanbase:
315315
return "`";
316+
// 当dbType 为 postgresql时,返回双引号(避免表名为postgresql的关键字时,sql查询报错)
317+
case postgresql:
318+
return "\"";
316319
default:
317320
return "";
318321
}

0 commit comments

Comments
 (0)