From 6c83d2ad1a5e48f4af3e50e0415907ad8fe6651c Mon Sep 17 00:00:00 2001 From: H-Jason <953986800@qq.com> Date: Thu, 6 Jun 2024 00:31:45 +0800 Subject: [PATCH] fix dead lock in multi DB when using batchUpdate(#31038) --- .../sql/prepare/AbstractExecutionPrepareEngine.java | 4 ++-- .../test/natived/jdbc/commons/TestShardingService.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java index eea1fec1043b59..7519f521863b1b 100644 --- a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java +++ b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/AbstractExecutionPrepareEngine.java @@ -30,11 +30,11 @@ import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeMap; /** * Abstract execution prepare engine. @@ -80,7 +80,7 @@ private List> group(final List sqlUnits) { protected abstract List> group(String dataSourceName, int connectionOffset, List> executionUnitGroups, ConnectionMode connectionMode) throws SQLException; private Map> aggregateExecutionUnitGroups(final Collection executionUnits) { - Map> result = new LinkedHashMap<>(executionUnits.size(), 1F); + Map> result = new TreeMap<>(); for (ExecutionUnit each : executionUnits) { result.computeIfAbsent(each.getDataSourceName(), unused -> new LinkedList<>()).add(each); } diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java index 8e577b480b3f18..b1251a7cba8016 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java @@ -63,16 +63,16 @@ public void processSuccess() throws SQLException { final Collection orderIds = insertData(); Collection orders = orderRepository.selectAll(); assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()), - equalTo(Arrays.asList(1, 1, 1, 1, 1, 0, 0, 0, 0, 0))); + equalTo(Arrays.asList(0, 0, 0, 0, 0, 1, 1, 1, 1, 1))); assertThat(orders.stream().map(Order::getUserId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)))); + equalTo(new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)))); assertThat(orders.stream().map(Order::getAddressId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1L, 3L, 5L, 7L, 9L, 2L, 4L, 6L, 8L, 10L)))); + equalTo(new ArrayList<>(Arrays.asList(2L, 4L, 6L, 8L, 10L, 1L, 3L, 5L, 7L, 9L)))); assertThat(orders.stream().map(Order::getStatus).collect(Collectors.toList()), equalTo(IntStream.range(1, 11).mapToObj(i -> "INSERT_TEST").collect(Collectors.toList()))); Collection orderItems = orderItemRepository.selectAll(); assertThat(orderItems.stream().map(OrderItem::getUserId).collect(Collectors.toList()), - equalTo(new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)))); + equalTo(new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)))); assertThat(orderItems.stream().map(OrderItem::getPhone).collect(Collectors.toList()), equalTo(IntStream.range(1, 11).mapToObj(i -> "13800000001").collect(Collectors.toList()))); assertThat(orderItems.stream().map(OrderItem::getStatus).collect(Collectors.toList()),