use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class ShardingPreparedStatement method wrap.
private PreparedStatementExecutorWrapper wrap(final PreparedStatement preparedStatement, final SQLExecutionUnit sqlExecutionUnit) {
Optional<PreparedStatementExecutorWrapper> wrapperOptional = Iterators.tryFind(cachedPreparedStatementWrappers.iterator(), new Predicate<PreparedStatementExecutorWrapper>() {
@Override
public boolean apply(final PreparedStatementExecutorWrapper input) {
return Objects.equals(input.getPreparedStatement(), preparedStatement);
}
});
if (wrapperOptional.isPresent()) {
wrapperOptional.get().addBatchParameters(getParameters());
return wrapperOptional.get();
}
PreparedStatementExecutorWrapper result = new PreparedStatementExecutorWrapper(preparedStatement, getParameters(), sqlExecutionUnit);
cachedPreparedStatementWrappers.add(result);
return result;
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutor method executeQuery.
/**
* 执行SQL查询.
*
* @return 结果集列表
*/
public List<ResultSet> executeQuery() {
Context context = MetricsContext.start("ShardingPreparedStatement-executeQuery");
eventPostman.postExecutionEvents();
List<ResultSet> result;
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
try {
if (1 == preparedStatementExecutorWrappers.size()) {
return Collections.singletonList(executeQueryInternal(preparedStatementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap));
}
result = executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, ResultSet>() {
@Override
public ResultSet execute(final PreparedStatementExecutorWrapper input) throws Exception {
synchronized (input.getPreparedStatement().getConnection()) {
return executeQueryInternal(input, isExceptionThrown, dataMap);
}
}
});
} finally {
MetricsContext.stop(context);
}
return result;
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutor method executeUpdate.
/**
* 执行SQL更新.
*
* @return 更新数量
*/
public int executeUpdate() {
Context context = MetricsContext.start("ShardingPreparedStatement-executeUpdate");
eventPostman.postExecutionEvents();
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
try {
if (1 == preparedStatementExecutorWrappers.size()) {
return executeUpdateInternal(preparedStatementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
}
return executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, Integer>() {
@Override
public Integer execute(final PreparedStatementExecutorWrapper input) throws Exception {
synchronized (input.getPreparedStatement().getConnection()) {
return executeUpdateInternal(input, isExceptionThrown, dataMap);
}
}
}, new MergeUnit<Integer, Integer>() {
@Override
public Integer merge(final List<Integer> results) {
if (null == results) {
return 0;
}
int result = 0;
for (Integer each : results) {
result += each;
}
return result;
}
});
} finally {
MetricsContext.stop(context);
}
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutor method executeBatch.
/**
* 执行批量接口.
*
* @return 每个
* @param batchSize 批量执行语句总数
*/
public int[] executeBatch(final int batchSize) {
Context context = MetricsContext.start("ShardingPreparedStatement-executeUpdate");
eventPostman.postExecutionEvents();
final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
try {
if (1 == preparedStatementExecutorWrappers.size()) {
return executeBatchInternal(preparedStatementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
}
return executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, int[]>() {
@Override
public int[] execute(final PreparedStatementExecutorWrapper input) throws Exception {
synchronized (input.getPreparedStatement().getConnection()) {
return executeBatchInternal(input, isExceptionThrown, dataMap);
}
}
}, new MergeUnit<int[], int[]>() {
@Override
public int[] merge(final List<int[]> results) {
if (null == results) {
return new int[] { 0 };
}
int[] result = new int[batchSize];
int i = 0;
for (PreparedStatementExecutorWrapper each : preparedStatementExecutorWrappers) {
for (Integer[] indices : each.getBatchIndices()) {
result[indices[0]] += results.get(i)[indices[1]];
}
i++;
}
return result;
}
});
} finally {
MetricsContext.stop(context);
}
}
use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.
the class PreparedStatementExecutorTest method assertExecuteQueryForMultiplePreparedStatementsFailure.
@Test
public void assertExecuteQueryForMultiplePreparedStatementsFailure() throws SQLException {
PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDQL(preparedStatement1, "ds_0");
PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDQL(preparedStatement2, "ds_1");
SQLException exp = new SQLException();
when(preparedStatement1.executeQuery()).thenThrow(exp);
when(preparedStatement2.executeQuery()).thenThrow(exp);
when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
List<ResultSet> actualResultSets = actual.executeQuery();
assertThat(actualResultSets, is(Arrays.asList((ResultSet) null, null)));
verify(preparedStatement1).executeQuery();
verify(preparedStatement2).executeQuery();
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("SELECT * FROM dual");
verify(eventCaller, times(4)).verifyParameters(Collections.emptyList());
verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
verify(eventCaller, times(2)).verifyException(exp);
}
Aggregations