Skip to content

Commit

Permalink
fixed apache#201 Batch execution event sending misses pre-execution e…
Browse files Browse the repository at this point in the history
…vent send
  • Loading branch information
gaohongtao authored and xydonne committed Dec 2, 2016
1 parent 95a61b2 commit d1b5abd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ class EventPostman {

void postExecutionEvents() {
for (AbstractExecutorWrapper each : statementExecutorWrappers) {
if (each.getDMLExecutionEvent().isPresent()) {
if (each instanceof BatchPreparedStatementExecutorWrapper) {
postBatchExecutionEvent((BatchPreparedStatementExecutorWrapper) each);
} else if (each.getDMLExecutionEvent().isPresent()) {
DMLExecutionEventBus.post(each.getDMLExecutionEvent().get());
} else if (each.getDQLExecutionEvent().isPresent()) {
DQLExecutionEventBus.post(each.getDQLExecutionEvent().get());
}
}
}

private void postBatchExecutionEvent(final BatchPreparedStatementExecutorWrapper batchPreparedStatementExecutorWrapper) {
for (DMLExecutionEvent each : batchPreparedStatementExecutorWrapper.getDmlExecutionEvents()) {
DMLExecutionEventBus.post(each);
}
}

void postExecutionEventsAfterExecution(final AbstractExecutorWrapper statementExecutorWrapper) {
postExecutionEventsAfterExecution(statementExecutorWrapper, EventExecutionType.EXECUTE_SUCCESS, Optional.<SQLException>absent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
package com.dangdang.ddframe.rdb.sharding.jdbc;

import com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEvent;
import com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEventBus;
import com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEventListener;
import com.dangdang.ddframe.rdb.sharding.executor.event.EventExecutionType;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -26,6 +32,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -221,6 +228,24 @@ public void assertExecuteQueryWithColumnNames() throws SQLException {

@Test
public void assertAddBatch() throws SQLException {
final AtomicInteger beforeEventSum = new AtomicInteger();
final AtomicInteger successEventSum = new AtomicInteger();
DMLExecutionEventBus.register(new DMLExecutionEventListener() {
@Override
public String getName() {
return "test";
}

@Subscribe
@AllowConcurrentEvents
public void subscribe(final DMLExecutionEvent event) {
if (event.getEventExecutionType().equals(EventExecutionType.BEFORE_EXECUTE)) {
beforeEventSum.incrementAndGet();
} else if (event.getEventExecutionType().equals(EventExecutionType.EXECUTE_SUCCESS)) {
successEventSum.incrementAndGet();
}
}
});
String sql = "INSERT INTO `t_order`(`order_id`, `user_id`, `status`) VALUES (?,?,?)";
try (
Connection connection = shardingDataSource.getConnection();
Expand All @@ -246,6 +271,9 @@ public void assertAddBatch() throws SQLException {
assertThat(each, is(1));
}
}
assertThat(beforeEventSum.get(), is(4));
assertThat(successEventSum.get(), is(4));
DMLExecutionEventBus.clearListener();
}

@Test
Expand Down
1 change: 1 addition & 0 deletions sharding-jdbc-doc/content/post/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ weight = 1

1. [ISSUE #194](https://github.com/dangdangdotcom/sharding-jdbc/issues/194) jdbc接口中资源释放错误
1. [ISSUE #199](https://github.com/dangdangdotcom/sharding-jdbc/issues/199) 分表且复用PreparedStatement对象造成数据路由错误
1. [ISSUE #201](https://github.com/dangdangdotcom/sharding-jdbc/issues/201) 批量操作执行前事件发送缺失

## 1.4.0

Expand Down

0 comments on commit d1b5abd

Please sign in to comment.