From ec7ba187927278f51132f2b0cd57973900250b12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E5=A4=84=E4=B8=8D=E5=9C=A8?= <42382506+wuchubuzai2018@users.noreply.github.com> Date: Thu, 10 Nov 2022 09:42:38 +0800 Subject: [PATCH] [impr]Update some method from RETURN_GENERATED_KEYS replace to String[]{"id"},Avoid database compatibility issues (#9515) --- .../service/capacity/GroupCapacityPersistService.java | 8 ++++---- .../service/capacity/TenantCapacityPersistService.java | 4 ++-- .../extrnal/ExternalStoragePersistServiceImpl.java | 5 ++--- .../nacos/plugin/datasource/mapper/AbstractMapper.java | 5 +++++ .../alibaba/nacos/plugin/datasource/mapper/Mapper.java | 8 ++++++++ 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java index 6592e136a..3074f19a5 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java @@ -37,7 +37,6 @@ import javax.annotation.PostConstruct; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.sql.Timestamp; import java.util.Arrays; import java.util.List; @@ -122,14 +121,15 @@ public class GroupCapacityPersistService { // Note: add "tenant_id = ''" condition. sql = groupCapacityMapper.insertIntoSelectByWhere(); } - return insertGroupCapacity(sql, capacity); + String[] primaryKeyGeneratedKeys = groupCapacityMapper.getPrimaryKeyGeneratedKeys(); + return insertGroupCapacity(sql, capacity, primaryKeyGeneratedKeys); } - private boolean insertGroupCapacity(final String sql, final GroupCapacity capacity) { + private boolean insertGroupCapacity(final String sql, final GroupCapacity capacity, String[] primaryKeyGeneratedKeys) { try { GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder(); PreparedStatementCreator preparedStatementCreator = connection -> { - PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + PreparedStatement ps = connection.prepareStatement(sql, primaryKeyGeneratedKeys); String group = capacity.getGroup(); ps.setString(1, group); ps.setInt(2, capacity.getQuota()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java index 43e155c12..5e027f5c8 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java @@ -35,7 +35,6 @@ import javax.annotation.PostConstruct; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; @@ -110,10 +109,11 @@ public class TenantCapacityPersistService { TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.TENANT_CAPACITY); final String sql = tenantCapacityMapper.insertTenantCapacity(); + String[] primaryKeyGeneratedKeys = tenantCapacityMapper.getPrimaryKeyGeneratedKeys(); try { GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder(); PreparedStatementCreator preparedStatementCreator = connection -> { - PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + PreparedStatement ps = connection.prepareStatement(sql, primaryKeyGeneratedKeys); String tenant = tenantCapacity.getTenant(); ps.setString(1, tenant); ps.setInt(2, tenantCapacity.getQuota()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java index bb8278a19..7471aa745 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java @@ -76,7 +76,6 @@ import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; -import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; @@ -1675,12 +1674,12 @@ public class ExternalStoragePersistServiceImpl implements PersistService { Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", "src_user", "gmt_create", "gmt_modified", "c_desc", "c_use", "effect", "type", "c_schema", "encrypted_data_key")); - + String[] returnGeneratedKeys = configInfoMapper.getPrimaryKeyGeneratedKeys(); try { jt.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { - PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + PreparedStatement ps = connection.prepareStatement(sql, returnGeneratedKeys); ps.setString(1, configInfo.getDataId()); ps.setString(2, configInfo.getGroup()); ps.setString(3, tenantTmp); diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java index 62b079eb6..09481d743 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java @@ -152,4 +152,9 @@ public abstract class AbstractMapper implements Mapper { } return sql.toString(); } + + @Override + public String[] getPrimaryKeyGeneratedKeys() { + return new String[]{"id"}; + } } diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java index 775a73f3b..20259194b 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java @@ -75,4 +75,12 @@ public interface Mapper { * @return The name of datasource. */ String getDataSource(); + + /** + * Get config_info table primary keys name. + * The old default value: Statement.RETURN_GENERATED_KEYS + * The new default value: new String[]{"id"} + * @return an array of column names indicating the columns + */ + String[] getPrimaryKeyGeneratedKeys(); } \ No newline at end of file