Skip to content

Commit

Permalink
Fixed #520.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Dec 28, 2017
1 parent af0596c commit fb66859
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 29 deletions.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ public ExecutorEngine(final int executorSize) {
* @param executeCallback statement execute callback
* @param <T> class type of return value
* @return execute result
* @throws SQLException SQL exception
*/
public <T> List<T> executeStatement(final SQLType sqlType, final Collection<StatementUnit> statementUnits, final ExecuteCallback<T> executeCallback) {
public <T> List<T> executeStatement(final SQLType sqlType, final Collection<StatementUnit> statementUnits, final ExecuteCallback<T> executeCallback) throws SQLException {
return execute(sqlType, statementUnits, Collections.<List<Object>>emptyList(), executeCallback);
}

Expand All @@ -90,9 +91,10 @@ public <T> List<T> executeStatement(final SQLType sqlType, final Collection<Stat
* @param executeCallback prepared statement execute callback
* @param <T> class type of return value
* @return execute result
* @throws SQLException SQL exception
*/
public <T> List<T> executePreparedStatement(
final SQLType sqlType, final Collection<PreparedStatementUnit> preparedStatementUnits, final List<Object> parameters, final ExecuteCallback<T> executeCallback) {
final SQLType sqlType, final Collection<PreparedStatementUnit> preparedStatementUnits, final List<Object> parameters, final ExecuteCallback<T> executeCallback) throws SQLException {
return execute(sqlType, preparedStatementUnits, Collections.singletonList(parameters), executeCallback);
}

Expand All @@ -104,14 +106,17 @@ public <T> List<T> executePreparedStatement(
* @param parameterSets parameters for SQL placeholder
* @param executeCallback prepared statement execute callback
* @return execute result
* @throws SQLException SQL exception
*/
public List<int[]> executeBatch(
final SQLType sqlType, final Collection<BatchPreparedStatementUnit> batchPreparedStatementUnits, final List<List<Object>> parameterSets, final ExecuteCallback<int[]> executeCallback) {
final SQLType sqlType, final Collection<BatchPreparedStatementUnit> batchPreparedStatementUnits,
final List<List<Object>> parameterSets, final ExecuteCallback<int[]> executeCallback) throws SQLException {
return execute(sqlType, batchPreparedStatementUnits, parameterSets, executeCallback);
}

private <T> List<T> execute(
final SQLType sqlType, final Collection<? extends BaseStatementUnit> baseStatementUnits, final List<List<Object>> parameterSets, final ExecuteCallback<T> executeCallback) {
final SQLType sqlType, final Collection<? extends BaseStatementUnit> baseStatementUnits,
final List<List<Object>> parameterSets, final ExecuteCallback<T> executeCallback) throws SQLException {
if (baseStatementUnits.isEmpty()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;

/**
* Executor runtime exception handler.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<int[]>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

Expand All @@ -49,8 +50,9 @@ public final class PreparedStatementExecutor {
* Execute query.
*
* @return result set list
* @throws SQLException SQL exception
*/
public List<ResultSet> executeQuery() {
public List<ResultSet> executeQuery() throws SQLException {
return executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback<ResultSet>() {

@Override
Expand All @@ -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<Integer> results = executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback<Integer>() {

@Override
Expand All @@ -88,8 +91,9 @@ private int accumulate(final List<Integer> 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<Boolean> result = executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback<Boolean>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public final class StatementExecutor {
* Execute query.
*
* @return result set list
* @throws SQLException SQL exception
*/
public List<ResultSet> executeQuery() {
public List<ResultSet> executeQuery() throws SQLException {
return executorEngine.executeStatement(sqlType, statementUnits, new ExecuteCallback<ResultSet>() {

@Override
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<Integer> results = executorEngine.executeStatement(sqlType, statementUnits, new ExecuteCallback<Integer>() {

@Override
Expand All @@ -146,8 +151,9 @@ private int accumulate(final List<Integer> 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<Boolean> result = executorEngine.executeStatement(sqlType, statementUnits, new ExecuteCallback<Boolean>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fb66859

Please sign in to comment.