diff --git a/changes/1.5.0.md b/changes/1.5.0.md index 7fddd99756c..c6576a73e02 100644 --- a/changes/1.5.0.md +++ b/changes/1.5.0.md @@ -32,6 +32,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单 - [[#3654](https://github.com/seata/seata/pull/3654)] 修正拼写,applicationContex -> applicationContext - [[#3687](https://github.com/seata/seata/pull/3687)] 修复某些场景下无法重试全局锁的问题 - [[#3689](https://github.com/seata/seata/pull/3689)] 修正script/server/config/file.properties中属性编写错误 +- [[#3700](https://github.com/seata/seata/pull/3700)] 优化buildLockKey方法的效率 ### test: diff --git a/changes/en-us/1.5.0.md b/changes/en-us/1.5.0.md index ed081f96cdf..726e47c4e9a 100644 --- a/changes/en-us/1.5.0.md +++ b/changes/en-us/1.5.0.md @@ -33,6 +33,7 @@ - [[#3654](https://github.com/seata/seata/pull/3654)] fix typo,applicationContex -> applicationContext - [[#3687](https://github.com/seata/seata/pull/3687)] fix the case that could not retry acquire global lock - [[#3689](https://github.com/seata/seata/pull/3689)] modify the attribute prefix in the file file.properties +- [[#3700](https://github.com/seata/seata/pull/3700)] optimize the speed of buildLockKey ### test: diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java index 1109bb92f67..35e7bde2bc2 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java @@ -302,9 +302,10 @@ protected String buildLockKey(TableRecords rowsIncludingPK) { sb.append(":"); int filedSequence = 0; List> pksRows = rowsIncludingPK.pkRows(); + List primaryKeysOnlyName = getTableMeta().getPrimaryKeyOnlyName(); for (Map rowMap : pksRows) { int pkSplitIndex = 0; - for (String pkName : getTableMeta().getPrimaryKeyOnlyName()) { + for (String pkName : primaryKeysOnlyName) { if (pkSplitIndex > 0) { sb.append("_"); } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java index 710ff546089..63db3728a53 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java @@ -84,7 +84,7 @@ public Map> getPkValues() throws SQLException { List pkColumnNameList = getTableMeta().getPrimaryKeyOnlyName(); Boolean isContainsPk = containsPK(); //when there is only one pk in the table - if (getTableMeta().getPrimaryKeyOnlyName().size() == 1) { + if (pkColumnNameList.size() == 1) { if (isContainsPk) { pkValuesMap = getPkValuesByColumn(); } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/oracle/OracleInsertExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/oracle/OracleInsertExecutor.java index 00e3ac1b29b..2ae7f3f1c65 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/oracle/OracleInsertExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/oracle/OracleInsertExecutor.java @@ -64,12 +64,10 @@ public Map> getPkValues() throws SQLException { //when there is only one pk in the table if (isContainsPk) { pkValuesMap = getPkValuesByColumn(); - } - else if (containsColumns()) { + } else if (containsColumns()) { String columnName = getTableMeta().getPrimaryKeyOnlyName().get(0); pkValuesMap = Collections.singletonMap(columnName, getGeneratedKeys()); - } - else { + } else { pkValuesMap = getPkValuesByColumn(); } return pkValuesMap; @@ -82,9 +80,9 @@ public Map> getPkValuesByColumn() throws SQLException { List pkValues = pkValuesMap.get(pkKey); if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlSequenceExpr) { - pkValuesMap.put(pkKey,getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0))); + pkValuesMap.put(pkKey, getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0))); } else if (pkValues.size() == 1 && pkValues.get(0) instanceof SqlMethodExpr) { - pkValuesMap.put(pkKey,getGeneratedKeys()); + pkValuesMap.put(pkKey, getGeneratedKeys()); } else if (pkValues.size() == 1 && pkValues.get(0) instanceof Null) { throw new NotSupportYetException("oracle not support null"); } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/postgresql/PostgresqlInsertExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/postgresql/PostgresqlInsertExecutor.java index 0db959e0199..a51e63e4ce7 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/postgresql/PostgresqlInsertExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/postgresql/PostgresqlInsertExecutor.java @@ -67,12 +67,10 @@ public Map> getPkValues() throws SQLException { //when there is only one pk in the table if (isContainsPk) { pkValuesMap = getPkValuesByColumn(); - } - else if (containsColumns()) { + } else if (containsColumns()) { String columnName = getTableMeta().getPrimaryKeyOnlyName().get(0); pkValuesMap = Collections.singletonMap(columnName, getGeneratedKeys()); - } - else { + } else { pkValuesMap = getPkValuesByColumn(); } return pkValuesMap; @@ -84,11 +82,11 @@ public Map> getPkValuesByColumn() throws SQLException { String pkKey = pkValuesMap.keySet().iterator().next(); List pkValues = pkValuesMap.get(pkKey); if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlSequenceExpr) { - pkValuesMap.put(pkKey,getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0))); + pkValuesMap.put(pkKey, getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0))); } else if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlMethodExpr) { - pkValuesMap.put(pkKey,getGeneratedKeys()); + pkValuesMap.put(pkKey, getGeneratedKeys()); } else if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlDefaultExpr) { - pkValuesMap.put(pkKey,getPkValuesByDefault()); + pkValuesMap.put(pkKey, getPkValuesByDefault()); } return pkValuesMap; diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableRecords.java b/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableRecords.java index 4e7c6e786d0..f04b889553c 100755 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableRecords.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableRecords.java @@ -185,6 +185,7 @@ public static TableRecords empty(TableMeta tableMeta) { public static TableRecords buildRecords(TableMeta tmeta, ResultSet resultSet) throws SQLException { TableRecords records = new TableRecords(tmeta); ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); + Map primaryKeyMap = tmeta.getPrimaryKeyMap(); int columnCount = resultSetMetaData.getColumnCount(); while (resultSet.next()) { @@ -195,7 +196,7 @@ public static TableRecords buildRecords(TableMeta tmeta, ResultSet resultSet) th int dataType = col.getDataType(); Field field = new Field(); field.setName(col.getColumnName()); - if (tmeta.getPrimaryKeyMap().containsKey(colName)) { + if (primaryKeyMap.containsKey(colName)) { field.setKeyType(KeyType.PRIMARY_KEY); } field.setType(dataType);