diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index dd1eea4876474..deea349cdfdda 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -4,7 +4,8 @@ 1. [ISSUE #475](https://github.com/shardingjdbc/sharding-jdbc/issues/475) 支持create index语句 ### 缺陷修正 -1. [ISSUE #521](https://github.com/shardingjdbc/sharding-jdbc/issues/521) YAML文件中ShardingProperties设置无效 +1. [ISSUE #520](https://github.com/shardingjdbc/sharding-jdbc/issues/520) 引入分表后,唯一键冲突时异常类型不再是DuplicateKeyException +1. [ISSUE #521](https://github.com/shardingjdbc/sharding-jdbc/issues/521) YAML文件中ShardingProperties设置无效 ## 2.0.1 diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/ExecutorEngine.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/ExecutorEngine.java index ca6576f59c8f1..2b67d04925863 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/ExecutorEngine.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/ExecutorEngine.java @@ -76,8 +76,9 @@ public ExecutorEngine(final int executorSize) { * @param executeCallback statement execute callback * @param class type of return value * @return execute result + * @throws SQLException SQL exception */ - public List executeStatement(final SQLType sqlType, final Collection statementUnits, final ExecuteCallback executeCallback) { + public List executeStatement(final SQLType sqlType, final Collection statementUnits, final ExecuteCallback executeCallback) throws SQLException { return execute(sqlType, statementUnits, Collections.>emptyList(), executeCallback); } @@ -90,9 +91,10 @@ public List executeStatement(final SQLType sqlType, final Collection class type of return value * @return execute result + * @throws SQLException SQL exception */ public List executePreparedStatement( - final SQLType sqlType, final Collection preparedStatementUnits, final List parameters, final ExecuteCallback executeCallback) { + final SQLType sqlType, final Collection preparedStatementUnits, final List parameters, final ExecuteCallback executeCallback) throws SQLException { return execute(sqlType, preparedStatementUnits, Collections.singletonList(parameters), executeCallback); } @@ -104,14 +106,17 @@ public List executePreparedStatement( * @param parameterSets parameters for SQL placeholder * @param executeCallback prepared statement execute callback * @return execute result + * @throws SQLException SQL exception */ public List executeBatch( - final SQLType sqlType, final Collection batchPreparedStatementUnits, final List> parameterSets, final ExecuteCallback executeCallback) { + final SQLType sqlType, final Collection batchPreparedStatementUnits, + final List> parameterSets, final ExecuteCallback executeCallback) throws SQLException { return execute(sqlType, batchPreparedStatementUnits, parameterSets, executeCallback); } private List execute( - final SQLType sqlType, final Collection baseStatementUnits, final List> parameterSets, final ExecuteCallback executeCallback) { + final SQLType sqlType, final Collection baseStatementUnits, + final List> parameterSets, final ExecuteCallback executeCallback) throws SQLException { if (baseStatementUnits.isEmpty()) { return Collections.emptyList(); } diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandler.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandler.java index 17b31e93ccf5a..68497febfb3b5 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandler.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandler.java @@ -22,6 +22,8 @@ import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; +import java.sql.SQLException; + /** * Executor runtime exception handler. * @@ -55,9 +57,13 @@ public static boolean isExceptionThrown() { * Handle exception. * * @param exception to be handled exception + * @throws SQLException SQL exception */ - public static void handleException(final Exception exception) { + public static void handleException(final Exception exception) throws SQLException { if (isExceptionThrown()) { + if (exception instanceof SQLException) { + throw (SQLException) exception; + } throw new ShardingJdbcException(exception); } log.error("exception occur: ", exception); diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/batch/BatchPreparedStatementExecutor.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/batch/BatchPreparedStatementExecutor.java index b31279e4c7ffa..848186378631c 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/batch/BatchPreparedStatementExecutor.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/batch/BatchPreparedStatementExecutor.java @@ -24,6 +24,7 @@ import io.shardingjdbc.core.executor.ExecutorEngine; import lombok.RequiredArgsConstructor; +import java.sql.SQLException; import java.util.Collection; import java.util.List; import java.util.Map; @@ -50,8 +51,9 @@ public final class BatchPreparedStatementExecutor { * Execute batch. * * @return execute results + * @throws SQLException SQL exception */ - public int[] executeBatch() { + public int[] executeBatch() throws SQLException { return accumulate(executorEngine.executeBatch(sqlType, batchPreparedStatementUnits, parameterSets, new ExecuteCallback() { @Override diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/prepared/PreparedStatementExecutor.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/prepared/PreparedStatementExecutor.java index 76f2f1eb32da7..4b9e89c836230 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/prepared/PreparedStatementExecutor.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/prepared/PreparedStatementExecutor.java @@ -25,6 +25,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -49,8 +50,9 @@ public final class PreparedStatementExecutor { * Execute query. * * @return result set list + * @throws SQLException SQL exception */ - public List executeQuery() { + public List executeQuery() throws SQLException { return executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback() { @Override @@ -64,8 +66,9 @@ public ResultSet execute(final BaseStatementUnit baseStatementUnit) throws Excep * Execute update. * * @return effected records count + * @throws SQLException SQL exception */ - public int executeUpdate() { + public int executeUpdate() throws SQLException { List results = executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback() { @Override @@ -88,8 +91,9 @@ private int accumulate(final List results) { * Execute SQL. * * @return return true if is DQL, false if is DML + * @throws SQLException SQL exception */ - public boolean execute() { + public boolean execute() throws SQLException { List result = executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback() { @Override diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/statement/StatementExecutor.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/statement/StatementExecutor.java index 8d5b514e13c5a..e3a8f12012a01 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/statement/StatementExecutor.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/executor/type/statement/StatementExecutor.java @@ -49,8 +49,9 @@ public final class StatementExecutor { * Execute query. * * @return result set list + * @throws SQLException SQL exception */ - public List executeQuery() { + public List executeQuery() throws SQLException { return executorEngine.executeStatement(sqlType, statementUnits, new ExecuteCallback() { @Override @@ -64,8 +65,9 @@ public ResultSet execute(final BaseStatementUnit baseStatementUnit) throws Excep * Execute update. * * @return effected records count + * @throws SQLException SQL exception */ - public int executeUpdate() { + public int executeUpdate() throws SQLException { return executeUpdate(new Updater() { @Override @@ -80,8 +82,9 @@ public int executeUpdate(final Statement statement, final String sql) throws SQL * * @param autoGeneratedKeys auto generated keys' flag * @return effected records count + * @throws SQLException SQL exception */ - public int executeUpdate(final int autoGeneratedKeys) { + public int executeUpdate(final int autoGeneratedKeys) throws SQLException { return executeUpdate(new Updater() { @Override @@ -96,8 +99,9 @@ public int executeUpdate(final Statement statement, final String sql) throws SQL * * @param columnIndexes column indexes * @return effected records count + * @throws SQLException SQL exception */ - public int executeUpdate(final int[] columnIndexes) { + public int executeUpdate(final int[] columnIndexes) throws SQLException { return executeUpdate(new Updater() { @Override @@ -112,8 +116,9 @@ public int executeUpdate(final Statement statement, final String sql) throws SQL * * @param columnNames column names * @return effected records count + * @throws SQLException SQL exception */ - public int executeUpdate(final String[] columnNames) { + public int executeUpdate(final String[] columnNames) throws SQLException { return executeUpdate(new Updater() { @Override @@ -123,7 +128,7 @@ public int executeUpdate(final Statement statement, final String sql) throws SQL }); } - private int executeUpdate(final Updater updater) { + private int executeUpdate(final Updater updater) throws SQLException { List results = executorEngine.executeStatement(sqlType, statementUnits, new ExecuteCallback() { @Override @@ -146,8 +151,9 @@ private int accumulate(final List results) { * Execute SQL. * * @return return true if is DQL, false if is DML + * @throws SQLException SQL exception */ - public boolean execute() { + public boolean execute() throws SQLException { return execute(new Executor() { @Override @@ -162,8 +168,9 @@ public boolean execute(final Statement statement, final String sql) throws SQLEx * * @param autoGeneratedKeys auto generated keys' flag * @return return true if is DQL, false if is DML + * @throws SQLException SQL exception */ - public boolean execute(final int autoGeneratedKeys) { + public boolean execute(final int autoGeneratedKeys) throws SQLException { return execute(new Executor() { @Override @@ -178,8 +185,9 @@ public boolean execute(final Statement statement, final String sql) throws SQLEx * * @param columnIndexes column indexes * @return return true if is DQL, false if is DML + * @throws SQLException SQL exception */ - public boolean execute(final int[] columnIndexes) { + public boolean execute(final int[] columnIndexes) throws SQLException { return execute(new Executor() { @Override @@ -194,8 +202,9 @@ public boolean execute(final Statement statement, final String sql) throws SQLEx * * @param columnNames column names * @return return true if is DQL, false if is DML + * @throws SQLException SQL exception */ - public boolean execute(final String[] columnNames) { + public boolean execute(final String[] columnNames) throws SQLException { return execute(new Executor() { @Override @@ -205,7 +214,7 @@ public boolean execute(final Statement statement, final String sql) throws SQLEx }); } - private boolean execute(final Executor executor) { + private boolean execute(final Executor executor) throws SQLException { List result = executorEngine.executeStatement(sqlType, statementUnits, new ExecuteCallback() { @Override diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandlerTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandlerTest.java index d2702db0cf2b6..306a988cf6a50 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandlerTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/threadlocal/ExecutorExceptionHandlerTest.java @@ -17,7 +17,6 @@ package io.shardingjdbc.core.executor.threadlocal; -import io.shardingjdbc.core.exception.ShardingJdbcException; import io.shardingjdbc.core.executor.fixture.ExecutorTestUtil; import org.junit.After; import org.junit.Test; @@ -34,21 +33,21 @@ public void tearDown() throws NoSuchFieldException, IllegalAccessException { ExecutorTestUtil.clear(); } - @Test(expected = ShardingJdbcException.class) - public void assertHandleExceptionWithoutSet() { + @Test(expected = SQLException.class) + public void assertHandleExceptionWithoutSet() throws SQLException { assertTrue(ExecutorExceptionHandler.isExceptionThrown()); ExecutorExceptionHandler.handleException(new SQLException("")); } - @Test(expected = ShardingJdbcException.class) - public void assertHandleExceptionWhenExceptionThrownIsTrue() { + @Test(expected = SQLException.class) + public void assertHandleExceptionWhenExceptionThrownIsTrue() throws SQLException { ExecutorExceptionHandler.setExceptionThrown(true); assertTrue(ExecutorExceptionHandler.isExceptionThrown()); ExecutorExceptionHandler.handleException(new SQLException("")); } @Test - public void assertHandleExceptionWhenExceptionThrownIsFalse() { + public void assertHandleExceptionWhenExceptionThrownIsFalse() throws SQLException { ExecutorExceptionHandler.setExceptionThrown(false); assertFalse(ExecutorExceptionHandler.isExceptionThrown()); ExecutorExceptionHandler.handleException(new SQLException("")); diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/type/StatementExecutorTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/type/StatementExecutorTest.java index 2daf65e5819b9..7bf85523e024b 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/type/StatementExecutorTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/executor/type/StatementExecutorTest.java @@ -18,7 +18,6 @@ package io.shardingjdbc.core.executor.type; import io.shardingjdbc.core.constant.SQLType; -import io.shardingjdbc.core.exception.ShardingJdbcException; import io.shardingjdbc.core.executor.event.EventExecutionType; import io.shardingjdbc.core.executor.threadlocal.ExecutorExceptionHandler; import io.shardingjdbc.core.executor.type.statement.StatementExecutor; @@ -472,7 +471,7 @@ public void assertOverallExceptionFailure() throws SQLException { StatementExecutor actual = new StatementExecutor(getExecutorEngine(), SQLType.DML, createStatementUnits(DML_SQL, statement, "ds_0")); try { assertFalse(actual.execute()); - } catch (final ShardingJdbcException ignored) { + } catch (final SQLException ignored) { } verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML); verify(getEventCaller()).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);