From 82b036fc8e5bb067f4cfee933a747b27878a34d6 Mon Sep 17 00:00:00 2001 From: justabug Date: Tue, 16 Jan 2024 19:53:52 +0800 Subject: [PATCH] bugfix: convert to utf8mb4 if mysql column is json type (#6232) --- changes/en-us/2.x.md | 3 +- changes/zh-cn/2.x.md | 3 +- .../undo/mysql/MySQLJsonHelper.java | 37 +++++++++++++++++++ .../undo/mysql/MySQLUndoDeleteExecutor.java | 2 +- .../undo/mysql/MySQLUndoUpdateExecutor.java | 7 +++- 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLJsonHelper.java diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 53759c4f3bb..b1b114e418e 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -18,7 +18,8 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6143](https://github.com/apache/incubator-seata/pull/6143)] gracefully shut down the server - [[#6204](https://github.com/apache/incubator-seata/pull/6204)] fix the problem that The incorrect configuration needs to be fixed - [[#6248](https://github.com/apache/incubator-seata/pull/6248)] fix JDBC resultSet, statement, connection closing order -- [[#6248](https://github.com/apache/incubator-seata/pull/6256)] fix raft-discovery cannot read registry configuration for seata-all sdk +- [[#6256](https://github.com/apache/incubator-seata/pull/6256)] fix raft-discovery cannot read registry configuration for seata-all sdk +- [[#6232](https://github.com/apache/incubator-seata/pull/6232)] convert to utf8mb4 if mysql column is json type ### optimize: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 502f8afbe9a..ebeec80036a 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -18,7 +18,8 @@ - [[#6143](https://github.com/apache/incubator-seata/pull/6143)] 修复优雅停机 - [[#6204](https://github.com/apache/incubator-seata/pull/6204)] 修复错误配置问题 - [[#6248](https://github.com/apache/incubator-seata/pull/6248)] 修复JDBC resultSet, statement, connection关闭顺序 -- [[#6248](https://github.com/apache/incubator-seata/pull/6256)] 修复在seata-all sdk下,raft-discovery模块不能读取registry.conf的配置的问题 +- [[#6256](https://github.com/apache/incubator-seata/pull/6256)] 修复在seata-all sdk下,raft-discovery模块不能读取registry.conf的配置的问题 +- [[#6232](https://github.com/apache/incubator-seata/pull/6232)] 修复在mysql的json类型下出现Cannot create a JSON value from a string with CHARACTER SET 'binary'问题 ### optimize: - [[#6031](https://github.com/apache/incubator-seata/pull/6031)] 添加undo_log表的存在性校验 diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLJsonHelper.java b/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLJsonHelper.java new file mode 100644 index 00000000000..90a083b756d --- /dev/null +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLJsonHelper.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.rm.datasource.undo.mysql; + +import io.seata.rm.datasource.sql.struct.Field; +import io.seata.sqlparser.struct.ColumnMeta; +import io.seata.sqlparser.struct.TableMeta; + +import java.sql.Types; + +/** + * the type MySQLJsonHelper + **/ +public class MySQLJsonHelper { + public static String convertIfJson(Field field, TableMeta tableMeta) { + ColumnMeta columnMeta = tableMeta.getColumnMeta(field.getName()); + if (columnMeta != null && columnMeta.getDataType() == Types.OTHER + && "JSON".equals(columnMeta.getDataTypeName())) { + return "CONVERT(? USING utf8mb4)"; + } + return "?"; + } +} diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoDeleteExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoDeleteExecutor.java index 9ebf829b4f9..29638bbdb38 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoDeleteExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoDeleteExecutor.java @@ -74,7 +74,7 @@ protected String buildUndoSQL() { String insertColumns = fields.stream() .map(field -> ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL)) .collect(Collectors.joining(", ")); - String insertValues = fields.stream().map(field -> "?") + String insertValues = fields.stream().map(field -> MySQLJsonHelper.convertIfJson(field, beforeImage.getTableMeta())) .collect(Collectors.joining(", ")); return String.format(INSERT_SQL_TEMPLATE, sqlUndoLog.getTableName(), insertColumns, insertValues); diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoUpdateExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoUpdateExecutor.java index e58cb5e22b5..5ccfee9aa96 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoUpdateExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/undo/mysql/MySQLUndoUpdateExecutor.java @@ -59,8 +59,11 @@ protected String buildUndoSQL() { // update sql undo log before image all field come from table meta. need add escape. // see BaseTransactionalExecutor#buildTableRecords String updateColumns = nonPkFields.stream().map( - field -> ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL) + " = ?").collect( - Collectors.joining(", ")); + field -> { + String addEscape = ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL); + return addEscape + " = " + MySQLJsonHelper.convertIfJson(field, beforeImage.getTableMeta()); + }) + .collect(Collectors.joining(", ")); List pkNameList = getOrderedPkList(beforeImage, row, JdbcConstants.MYSQL).stream().map(e -> e.getName()) .collect(Collectors.toList());