From c8b5f9083b10e1b8e31801b2ddc2646878f88ab2 Mon Sep 17 00:00:00 2001 From: caohao Date: Wed, 19 Sep 2018 15:27:01 +0800 Subject: [PATCH 01/15] for #1216, move the UpdateStatementParserTest relative test cases to IntegrateParsingTest for oracle special syntax 12th. --- .../integrate/cases/dml/dml-integrate-test-cases.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sharding-jdbc/src/test/resources/integrate/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/integrate/cases/dml/dml-integrate-test-cases.xml index 5ca3081fcde8c..f03cacf9c497d 100644 --- a/sharding-jdbc/src/test/resources/integrate/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/integrate/cases/dml/dml-integrate-test-cases.xml @@ -113,6 +113,10 @@ + + + + From 54cf023d36b49d512499b3859353ddca09af9d93 Mon Sep 17 00:00:00 2001 From: caohao Date: Wed, 19 Sep 2018 15:27:13 +0800 Subject: [PATCH 02/15] for #1216, move the UpdateStatementParserTest relative test cases to IntegrateParsingTest for oracle special syntax 13th. --- .../src/test/resources/parser/update.xml | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sharding-core/src/test/resources/parser/update.xml b/sharding-core/src/test/resources/parser/update.xml index 041fce44f2e17..2d354f7c0ca05 100644 --- a/sharding-core/src/test/resources/parser/update.xml +++ b/sharding-core/src/test/resources/parser/update.xml @@ -142,4 +142,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + From f4bcc3309e8f6a3b29f6ab084b30270f2b6014f2 Mon Sep 17 00:00:00 2001 From: caohao Date: Wed, 19 Sep 2018 15:27:37 +0800 Subject: [PATCH 03/15] for #1216, move the UpdateStatementParserTest relative test cases to IntegrateParsingTest for oracle special syntax 14th. --- sharding-sql-test/src/main/resources/sql/dml/update.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-sql-test/src/main/resources/sql/dml/update.xml b/sharding-sql-test/src/main/resources/sql/dml/update.xml index 749a66c5684c8..082e8db451963 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/update.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/update.xml @@ -8,4 +8,5 @@ + From 04f300b4f77c4b3a2e27888bc59e29495c60db0c Mon Sep 17 00:00:00 2001 From: caohao Date: Wed, 19 Sep 2018 15:28:18 +0800 Subject: [PATCH 04/15] for #1216, move the UpdateStatementParserTest relative test cases to IntegrateParsingTest for oracle special syntax 15th. --- .../core/parsing/parser/sql/AllStatementParserTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/AllStatementParserTests.java b/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/AllStatementParserTests.java index ab67965215486..34c1e8a7db62d 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/AllStatementParserTests.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/AllStatementParserTests.java @@ -23,8 +23,7 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ SelectStatementTest.class, - InsertStatementParserTest.class, - UpdateStatementParserTest.class + InsertStatementParserTest.class }) public final class AllStatementParserTests { } From ba509e5670da7bef0d034e8f6367485a658e22e8 Mon Sep 17 00:00:00 2001 From: caohao Date: Wed, 19 Sep 2018 15:30:42 +0800 Subject: [PATCH 05/15] for #1216, move the UpdateStatementParserTest relative test cases to IntegrateParsingTest and removed UpdateStatementParserTest. --- .../parser/sql/UpdateStatementParserTest.java | 47 ------------------- 1 file changed, 47 deletions(-) delete mode 100644 sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/UpdateStatementParserTest.java diff --git a/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/UpdateStatementParserTest.java b/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/UpdateStatementParserTest.java deleted file mode 100644 index 1ae55b7aee87c..0000000000000 --- a/sharding-core/src/test/java/io/shardingsphere/core/parsing/parser/sql/UpdateStatementParserTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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.shardingsphere.core.parsing.parser.sql; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.parsing.SQLParsingEngine; -import io.shardingsphere.core.parsing.parser.sql.dml.DMLStatement; -import io.shardingsphere.core.rule.ShardingRule; -import org.junit.Test; - - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -public final class UpdateStatementParserTest extends AbstractStatementParserTest { - - @Test - public void parseWithOr() { - ShardingRule shardingRule = createShardingRule(); - DMLStatement updateStatement = (DMLStatement) new SQLParsingEngine( - DatabaseType.Oracle, "UPDATE TABLE_XXX AS xxx SET field1=1 WHERE field1<1 AND (field1 >2 OR xxx.field2 =1)", shardingRule, null).parse(false); - assertUpdateStatementWitOr(updateStatement); - } - - private void assertUpdateStatementWitOr(final DMLStatement updateStatement) { - assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX")); - assertThat(updateStatement.getTables().find("TABLE_XXX").get().getAlias().get(), is("xxx")); - assertTrue(updateStatement.getConditions().getOrCondition().getAndConditions().isEmpty()); - - } -} From 89d9049b2b8d0e04e35056fec2a89567dca202d0 Mon Sep 17 00:00:00 2001 From: wangzhenfei Date: Wed, 19 Sep 2018 15:30:58 +0800 Subject: [PATCH 06/15] Prevent generation negative numbers to produce null pointer exception --- .../masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/api/algorithm/masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java b/sharding-core/src/main/java/io/shardingsphere/core/api/algorithm/masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java index 955c8c71b0316..fe7effb48b009 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/api/algorithm/masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/api/algorithm/masterslave/RoundRobinMasterSlaveLoadBalanceAlgorithm.java @@ -35,6 +35,6 @@ public String getDataSource(final String name, final String masterDataSourceName AtomicInteger count = COUNT_MAP.containsKey(name) ? COUNT_MAP.get(name) : new AtomicInteger(0); COUNT_MAP.putIfAbsent(name, count); count.compareAndSet(slaveDataSourceNames.size(), 0); - return slaveDataSourceNames.get(count.getAndIncrement() % slaveDataSourceNames.size()); + return slaveDataSourceNames.get(Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size()); } } From 874e9abff1d2a9e0e17e66ee60a7481417640519 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 19 Sep 2018 16:59:08 +0800 Subject: [PATCH 07/15] for #1205, sync data source map for Get connection sync to prevent dead lock --- .../core/jdbc/adapter/AbstractConnectionAdapter.java | 5 ++--- .../backend/jdbc/datasource/JDBCBackendDataSource.java | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java index c7a41a1f0dce2..93109dfd8abd7 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java @@ -117,10 +117,9 @@ public final List getConnections(final String dataSourceName, final return result; } - @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") - private synchronized List createConnections(final DataSource dataSource, final int connectionSize) throws SQLException { + private List createConnections(final DataSource dataSource, final int connectionSize) throws SQLException { List result = new ArrayList<>(connectionSize); - synchronized (dataSource) { + synchronized (getDataSourceMap()) { for (int i = 0; i < connectionSize; i++) { Connection connection = dataSource.getConnection(); replayMethodsInvocation(connection); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java index 90713029a915b..a20fcdb85e785 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java @@ -104,13 +104,11 @@ public Connection getConnection(final String dataSourceName) throws SQLException * @return connections * @throws SQLException SQL exception */ - @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") public List getConnections(final String dataSourceName, final int connectionSize) throws SQLException { List result = new ArrayList<>(connectionSize); - DataSource dataSource = getDataSourceMap().get(dataSourceName); - synchronized (dataSource) { + synchronized (getDataSourceMap()) { for (int i = 0; i < connectionSize; i++) { - result.add(dataSource.getConnection()); + result.add(getDataSourceMap().get(dataSourceName).getConnection()); } } return result; From 0559861c58dfa684b3e66b075dc0ec2eb648ae19 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 19 Sep 2018 17:04:49 +0800 Subject: [PATCH 08/15] update release notes --- RELEASE-NOTES.md | 1 + RELEASE-NOTES_ZH.md | 1 + 2 files changed, 2 insertions(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 405244af13b10..c05a3bc8fb1c8 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -56,6 +56,7 @@ 1. [ISSUE #1015](https://github.com/sharding-sphere/sharding-sphere/issues/1015) Support SQL like `SELECT id, COUNT(*) FROM table GROUP BY 1,2` 1. [ISSUE #1120](https://github.com/sharding-sphere/sharding-sphere/issues/1120) Derived columns of `GROUP BY / ORDER BY` appear in query result 1. [ISSUE #1186](https://github.com/sharding-sphere/sharding-sphere/issues/1186) Dead lock may occur on MEMORY_STRICTLY mode when get connection on concurrency environment +1. [ISSUE #1265](https://github.com/sharding-sphere/sharding-sphere/issues/1265) RoundRobinMasterSlaveLoadBalanceAlgorithm throw an ArrayIndexOutOfBoundsException when AtomicInteger overflow #### Sharding-JDBC diff --git a/RELEASE-NOTES_ZH.md b/RELEASE-NOTES_ZH.md index b5e6653799a07..e5757b80ded1c 100644 --- a/RELEASE-NOTES_ZH.md +++ b/RELEASE-NOTES_ZH.md @@ -58,6 +58,7 @@ 1. [ISSUE #1015](https://github.com/sharding-sphere/sharding-sphere/issues/1015) 支持SQL `SELECT id, COUNT(*) FROM table GROUP BY 1,2` 1. [ISSUE #1120](https://github.com/sharding-sphere/sharding-sphere/issues/1120) `GROUP BY / ORDER BY`产生的补列不应展现在查询结果中 1. [ISSUE #1186](https://github.com/sharding-sphere/sharding-sphere/issues/1186) 在MEMORY_STRICTLY模式中,并发环境下可能产生死锁 +1. [ISSUE #1265](https://github.com/sharding-sphere/sharding-sphere/issues/1265) 当AtomicInteger溢出后,RoundRobinMasterSlaveLoadBalanceAlgorithm抛出ArrayIndexOutOfBoundsException异常 #### Sharding-JDBC From 58f684d2c19f2544137edc0e598f89b0686853d0 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 19 Sep 2018 18:13:25 +0800 Subject: [PATCH 09/15] for #1205, sync SQLExecutePrepareTemplate for Get connection sync to prevent dead lock --- .../sql/prepare/SQLExecutePrepareTemplate.java | 6 ++++-- .../core/jdbc/adapter/AbstractConnectionAdapter.java | 10 ++++------ .../backend/jdbc/datasource/JDBCBackendDataSource.java | 6 ++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java index 48a10529b3e51..6aef71131ada9 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java @@ -57,8 +57,10 @@ public final class SQLExecutePrepareTemplate { public Collection> getExecuteUnitGroups(final Collection routeUnits, final SQLExecutePrepareCallback callback) throws SQLException { Map> sqlUnitGroups = getSQLUnitGroups(routeUnits); Collection> result = new LinkedList<>(); - for (Entry> entry : sqlUnitGroups.entrySet()) { - result.addAll(getSQLExecuteGroups(entry.getKey(), entry.getValue(), callback)); + synchronized (SQLExecutePrepareTemplate.class) { + for (Entry> entry : sqlUnitGroups.entrySet()) { + result.addAll(getSQLExecuteGroups(entry.getKey(), entry.getValue(), callback)); + } } return result; } diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java index 93109dfd8abd7..101e68e904db8 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java @@ -119,12 +119,10 @@ public final List getConnections(final String dataSourceName, final private List createConnections(final DataSource dataSource, final int connectionSize) throws SQLException { List result = new ArrayList<>(connectionSize); - synchronized (getDataSourceMap()) { - for (int i = 0; i < connectionSize; i++) { - Connection connection = dataSource.getConnection(); - replayMethodsInvocation(connection); - result.add(connection); - } + for (int i = 0; i < connectionSize; i++) { + Connection connection = dataSource.getConnection(); + replayMethodsInvocation(connection); + result.add(connection); } return result; } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java index a20fcdb85e785..ddb64052fe481 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java @@ -106,10 +106,8 @@ public Connection getConnection(final String dataSourceName) throws SQLException */ public List getConnections(final String dataSourceName, final int connectionSize) throws SQLException { List result = new ArrayList<>(connectionSize); - synchronized (getDataSourceMap()) { - for (int i = 0; i < connectionSize; i++) { - result.add(getDataSourceMap().get(dataSourceName).getConnection()); - } + for (int i = 0; i < connectionSize; i++) { + result.add(getDataSourceMap().get(dataSourceName).getConnection()); } return result; } From c459b722933a441f25434c0b8e9f6baf48cb53cc Mon Sep 17 00:00:00 2001 From: tristaZero Date: Wed, 19 Sep 2018 18:50:43 +0800 Subject: [PATCH 10/15] delete execute() --- .../core/executor/ShardingExecuteEngine.java | 67 ------------------- 1 file changed, 67 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/ShardingExecuteEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/ShardingExecuteEngine.java index b229acac25464..deea9210a6fb5 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/ShardingExecuteEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/ShardingExecuteEngine.java @@ -24,7 +24,6 @@ import io.shardingsphere.core.exception.ShardingException; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -53,72 +52,6 @@ public ShardingExecuteEngine(final int executorSize) { MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS); } - /** - * Execute. - * - * @param inputs input values - * @param callback sharding execute callback - * @param type of input value - * @param type of return value - * @return execute result - * @throws SQLException throw if execute failure - */ - public List execute(final Collection inputs, final ShardingExecuteCallback callback) throws SQLException { - return execute(inputs, null, callback); - } - - /** - * Execute. - * - * @param inputs input values - * @param firstCallback first sharding execute callback - * @param callback sharding execute callback - * @param type of input value - * @param type of return value - * @return execute result - * @throws SQLException throw if execute failure - */ - public List execute(final Collection inputs, final ShardingExecuteCallback firstCallback, final ShardingExecuteCallback callback) throws SQLException { - if (inputs.isEmpty()) { - return Collections.emptyList(); - } - Iterator inputIterator = inputs.iterator(); - I firstInput = inputIterator.next(); - Collection> restFutures = asyncExecute(Lists.newArrayList(inputIterator), callback); - return getResults(syncExecute(firstInput, null == firstCallback ? callback : firstCallback), restFutures); - } - - private Collection> asyncExecute(final Collection inputs, final ShardingExecuteCallback callback) { - Collection> result = new ArrayList<>(inputs.size()); - for (final I each : inputs) { - result.add(executorService.submit(new Callable() { - - @Override - public O call() throws SQLException { - return callback.execute(each); - } - })); - } - return result; - } - - private O syncExecute(final I input, final ShardingExecuteCallback callback) throws SQLException { - return callback.execute(input); - } - - private List getResults(final O firstResult, final Collection> restFutures) throws SQLException { - List result = new LinkedList<>(); - result.add(firstResult); - for (ListenableFuture each : restFutures) { - try { - result.add(each.get()); - } catch (final InterruptedException | ExecutionException ex) { - return throwException(ex); - } - } - return result; - } - /** * Execute for group. * From 9c60f56531caa0ae3d7b225229d4f2ab546c5447 Mon Sep 17 00:00:00 2001 From: tristaZero Date: Wed, 19 Sep 2018 18:50:58 +0800 Subject: [PATCH 11/15] delete execute() --- .../sql/execute/SQLExecuteTemplate.java | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java index 8c930ea6a6a78..f1c7d17062496 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java @@ -41,42 +41,6 @@ public final class SQLExecuteTemplate { private final ShardingExecuteEngine executeEngine; - /** - * Execute. - * - * @param statementExecuteUnits SQL execute units - * @param callback SQL execute callback - * @param class type of return value - * @return execute result - * @throws SQLException SQL exception - */ - public List execute(final Collection statementExecuteUnits, final SQLExecuteCallback callback) throws SQLException { - return execute(statementExecuteUnits, null, callback); - } - - /** - * Execute. - * - * @param statementExecuteUnits SQL execute units - * @param firstExecuteCallback first SQL execute callback - * @param callback SQL execute callback - * @param class type of return value - * @return execute result - * @throws SQLException SQL exception - */ - @SuppressWarnings("unchecked") - public List execute( - final Collection statementExecuteUnits, final SQLExecuteCallback firstExecuteCallback, final SQLExecuteCallback callback) throws SQLException { - try { - return executeEngine.execute((Collection) statementExecuteUnits, firstExecuteCallback, callback); - } catch (final SQLException ex) { - ExecutorExceptionHandler.handleException(ex); - return Collections.emptyList(); - } - } - - - /** * Execute group. * From ac61314bcba9de4039ab66e5ecbf10869d3bade2 Mon Sep 17 00:00:00 2001 From: tristaZero Date: Wed, 19 Sep 2018 18:51:07 +0800 Subject: [PATCH 12/15] modify cases --- .../listener/ExecuteEventListenerTest.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/listener/ExecuteEventListenerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/listener/ExecuteEventListenerTest.java index c4964bbe12e2a..606d92817e290 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/listener/ExecuteEventListenerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/listener/ExecuteEventListenerTest.java @@ -21,6 +21,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.executor.ShardingExecuteEngine; +import io.shardingsphere.core.executor.ShardingExecuteGroup; import io.shardingsphere.core.executor.StatementExecuteUnit; import io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback; import io.shardingsphere.core.executor.sql.execute.SQLExecuteTemplate; @@ -36,6 +37,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -73,8 +75,9 @@ protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) { return 0; } }; - sqlExecuteTemplate.execute(Collections.singleton(new StatementExecuteUnit(new RouteUnit("ds_0", - new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY)), executeCallback); + ShardingExecuteGroup shardingExecuteGroup = new ShardingExecuteGroup<>(Collections.singletonList(new StatementExecuteUnit(new RouteUnit("ds_0", + new SQLUnit("insert into ...", Collections.singletonList(Collections.singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY))); + sqlExecuteTemplate.executeGroup((Collection) Collections.singletonList(shardingExecuteGroup), executeCallback); assertThat(getTracer().finishedSpans().size(), is(1)); } @@ -102,7 +105,8 @@ protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) { return 0; } }; - sqlExecuteTemplate.execute(statementExecuteUnits, executeCallback); + ShardingExecuteGroup shardingExecuteGroup = new ShardingExecuteGroup<>(statementExecuteUnits); + sqlExecuteTemplate.executeGroup((Collection) Collections.singletonList(shardingExecuteGroup), executeCallback); assertThat(getTracer().finishedSpans().size(), is(2)); } @@ -121,7 +125,8 @@ protected Integer executeSQL(final StatementExecuteUnit statementExecuteUnit) th throw new SQLException(); } }; - sqlExecuteTemplate.execute(Collections.singleton(new StatementExecuteUnit(new RouteUnit("ds_0", - new SQLUnit("select ...", Collections.singletonList(Collections.singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY)), executeCallback); + ShardingExecuteGroup shardingExecuteGroup = new ShardingExecuteGroup<>(Collections.singletonList(new StatementExecuteUnit(new RouteUnit("ds_0", + new SQLUnit("select ...", Collections.singletonList(Collections.singletonList(1)))), statement, ConnectionMode.MEMORY_STRICTLY))); + sqlExecuteTemplate.executeGroup((Collection) Collections.singletonList(shardingExecuteGroup), executeCallback); } } From 3daf91422811e0a2701e83ad89a55b106b2b5449 Mon Sep 17 00:00:00 2001 From: tristaZero Date: Wed, 19 Sep 2018 18:51:24 +0800 Subject: [PATCH 13/15] delete blank line --- .../core/executor/sql/execute/SQLExecuteTemplate.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java index f1c7d17062496..fe7fe4282fe8a 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteTemplate.java @@ -75,4 +75,3 @@ public List executeGroup(final Collection Date: Wed, 19 Sep 2018 19:05:42 +0800 Subject: [PATCH 14/15] add AccessLevel.PROTECTED --- .../shardingsphere/core/executor/AbstractStatementExecutor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java index 5cdbc6170331f..f8dfba0bf6592 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java @@ -74,7 +74,7 @@ public class AbstractStatementExecutor { private final Collection> executeGroups = new LinkedList<>(); - @Getter + @Getter(AccessLevel.PROTECTED) @Setter private SQLType sqlType; From 1f7c0be8779ab746db1cec51f2f0ea186b3ab2f5 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 20 Sep 2018 10:57:10 +0800 Subject: [PATCH 15/15] for #1205, remove sync for SQLExecutePrepareTemplate and recover sync for get connections on same data source, because data source map always use same hash sequence to iterator entries --- .../sql/prepare/SQLExecutePrepareTemplate.java | 6 ++---- .../core/jdbc/adapter/AbstractConnectionAdapter.java | 11 +++++++---- .../jdbc/datasource/JDBCBackendDataSource.java | 8 ++++++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java index 6aef71131ada9..48a10529b3e51 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java @@ -57,10 +57,8 @@ public final class SQLExecutePrepareTemplate { public Collection> getExecuteUnitGroups(final Collection routeUnits, final SQLExecutePrepareCallback callback) throws SQLException { Map> sqlUnitGroups = getSQLUnitGroups(routeUnits); Collection> result = new LinkedList<>(); - synchronized (SQLExecutePrepareTemplate.class) { - for (Entry> entry : sqlUnitGroups.entrySet()) { - result.addAll(getSQLExecuteGroups(entry.getKey(), entry.getValue(), callback)); - } + for (Entry> entry : sqlUnitGroups.entrySet()) { + result.addAll(getSQLExecuteGroups(entry.getKey(), entry.getValue(), callback)); } return result; } diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java index 101e68e904db8..338cb3df43cfa 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java @@ -117,12 +117,15 @@ public final List getConnections(final String dataSourceName, final return result; } + @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") private List createConnections(final DataSource dataSource, final int connectionSize) throws SQLException { List result = new ArrayList<>(connectionSize); - for (int i = 0; i < connectionSize; i++) { - Connection connection = dataSource.getConnection(); - replayMethodsInvocation(connection); - result.add(connection); + synchronized (dataSource) { + for (int i = 0; i < connectionSize; i++) { + Connection connection = dataSource.getConnection(); + replayMethodsInvocation(connection); + result.add(connection); + } } return result; } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java index ddb64052fe481..90713029a915b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/datasource/JDBCBackendDataSource.java @@ -104,10 +104,14 @@ public Connection getConnection(final String dataSourceName) throws SQLException * @return connections * @throws SQLException SQL exception */ + @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") public List getConnections(final String dataSourceName, final int connectionSize) throws SQLException { List result = new ArrayList<>(connectionSize); - for (int i = 0; i < connectionSize; i++) { - result.add(getDataSourceMap().get(dataSourceName).getConnection()); + DataSource dataSource = getDataSourceMap().get(dataSourceName); + synchronized (dataSource) { + for (int i = 0; i < connectionSize; i++) { + result.add(dataSource.getConnection()); + } } return result; }