use of com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEvent in project sharding-jdbc by dangdangdotcom.
the class EventPostman method postExecutionEventsAfterExecution.
void postExecutionEventsAfterExecution(final AbstractExecutorWrapper statementExecutorWrapper, final EventExecutionType eventExecutionType, final Optional<SQLException> exp) {
if (statementExecutorWrapper.getDMLExecutionEvent().isPresent()) {
DMLExecutionEvent event = statementExecutorWrapper.getDMLExecutionEvent().get();
event.setEventExecutionType(eventExecutionType);
event.setExp(exp);
DMLExecutionEventBus.post(event);
} else if (statementExecutorWrapper.getDQLExecutionEvent().isPresent()) {
DQLExecutionEvent event = statementExecutorWrapper.getDQLExecutionEvent().get();
event.setEventExecutionType(eventExecutionType);
event.setExp(exp);
DQLExecutionEventBus.post(event);
}
}
use of com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEvent in project sharding-jdbc by dangdangdotcom.
the class ShardingPreparedStatementTest method assertAddBatch.
@Test
public void assertAddBatch() throws SQLException {
DMLExecutionEventBus.register(new DMLExecutionEventListener() {
private List<DMLExecutionEvent> beforeEvents = new ArrayList<>();
@Override
public String getName() {
return "test";
}
@Subscribe
@AllowConcurrentEvents
public void subscribe(final DMLExecutionEvent event) {
assertTrue(event.isBatch());
assertThat(event.getBatchParameters().size(), is(2));
if (event.getEventExecutionType().equals(EventExecutionType.BEFORE_EXECUTE)) {
beforeEvents.add(event);
} else if (event.getEventExecutionType().equals(EventExecutionType.EXECUTE_SUCCESS)) {
assertThat(beforeEvents, hasItem(event));
}
}
});
String sql = "INSERT INTO `t_order`(`order_id`, `user_id`, `status`) VALUES (?,?,?)";
try (Connection connection = shardingDataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, 3101);
preparedStatement.setInt(2, 11);
preparedStatement.setString(3, "BATCH");
preparedStatement.addBatch();
preparedStatement.setInt(1, 3102);
preparedStatement.setInt(2, 12);
preparedStatement.setString(3, "BATCH");
preparedStatement.addBatch();
preparedStatement.setInt(1, 3111);
preparedStatement.setInt(2, 21);
preparedStatement.setString(3, "BATCH");
preparedStatement.addBatch();
preparedStatement.setInt(1, 3112);
preparedStatement.setInt(2, 22);
preparedStatement.setString(3, "BATCH");
preparedStatement.addBatch();
int[] result = preparedStatement.executeBatch();
for (int each : result) {
assertThat(each, is(1));
}
} finally {
DMLExecutionEventBus.clearListener();
}
}
Aggregations