use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutorTest method assertExecuteForMultiplePreparedStatementsSuccessWithDML.
@Test
public void assertExecuteForMultiplePreparedStatementsSuccessWithDML() throws SQLException {
PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDML(preparedStatement1, "ds_0");
PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDML(preparedStatement2, "ds_1");
when(preparedStatement1.execute()).thenReturn(false);
when(preparedStatement2.execute()).thenReturn(false);
when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
assertFalse(actual.execute());
verify(preparedStatement1).execute();
verify(preparedStatement2).execute();
verify(preparedStatement1).getConnection();
verify(preparedStatement2).getConnection();
verify(eventCaller, times(2)).verifyDataSource("ds_0");
verify(eventCaller, times(2)).verifyDataSource("ds_1");
verify(eventCaller, times(4)).verifySQL("DELETE FROM dual");
verify(eventCaller, times(4)).verifyParameters(Collections.emptyList());
verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
verify(eventCaller, times(0)).verifyException(null);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutorTest method assertExecuteQueryForSinglePreparedStatementFailure.
@Test
public void assertExecuteQueryForSinglePreparedStatementFailure() throws SQLException {
PreparedStatement preparedStatement = mock(PreparedStatement.class);
PreparedStatementExecutorWrapper wrapper = createPreparedStatementExecutorWrapperForDQL(preparedStatement, "ds_0");
SQLException exp = new SQLException();
when(preparedStatement.executeQuery()).thenThrow(exp);
PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Collections.singleton(wrapper));
assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
verify(preparedStatement).executeQuery();
verify(eventCaller, times(2)).verifyDataSource("ds_0");
verify(eventCaller, times(2)).verifySQL("SELECT * FROM dual");
verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
verify(eventCaller).verifyException(exp);
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class ShardingPreparedStatement method routeSQL.
private List<PreparedStatementExecutorWrapper> routeSQL() throws SQLException {
List<PreparedStatementExecutorWrapper> result = new ArrayList<>();
SQLRouteResult sqlRouteResult = preparedSQLRouter.route(getParameters());
MergeContext mergeContext = sqlRouteResult.getMergeContext();
setMergeContext(mergeContext);
setGeneratedKeyContext(sqlRouteResult.getGeneratedKeyContext());
for (SQLExecutionUnit each : sqlRouteResult.getExecutionUnits()) {
PreparedStatement preparedStatement = (PreparedStatement) getStatement(getShardingConnection().getConnection(each.getDataSource(), sqlRouteResult.getSqlStatementType()), each.getSql());
replayMethodsInvocation(preparedStatement);
getParameters().replayMethodsInvocation(preparedStatement);
result.add(wrap(preparedStatement, each));
}
return result;
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutor method execute.
/**
* 执行SQL请求.
*
* @return true表示执行DQL, false表示执行的DML
*/
public boolean execute() {
Context context = MetricsContext.start("ShardingPreparedStatement-execute");
eventPostman.postExecutionEvents();
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
try {
if (1 == preparedStatementExecutorWrappers.size()) {
PreparedStatementExecutorWrapper preparedStatementExecutorWrapper = preparedStatementExecutorWrappers.iterator().next();
return executeInternal(preparedStatementExecutorWrapper, isExceptionThrown, dataMap);
}
List<Boolean> result = executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, Boolean>() {
@Override
public Boolean execute(final PreparedStatementExecutorWrapper input) throws Exception {
synchronized (input.getPreparedStatement().getConnection()) {
return executeInternal(input, isExceptionThrown, dataMap);
}
}
});
return (null == result || result.isEmpty()) ? false : result.get(0);
} finally {
MetricsContext.stop(context);
}
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class ShardingPreparedStatement method addBatch.
@Override
public void addBatch() throws SQLException {
try {
for (PreparedStatementExecutorWrapper each : routeSQL()) {
each.getPreparedStatement().addBatch();
each.mapBatchIndex(batchIndex);
}
batchIndex++;
getGeneratedKeyContext().addRow();
} finally {
resetBatch();
}
}
Aggregations