Search in sources :

Example 1 with DMLExecutionEventListener

use of com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEventListener 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();
    }
}
Also used : DMLExecutionEvent(com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEvent) AllowConcurrentEvents(com.google.common.eventbus.AllowConcurrentEvents) DMLExecutionEventListener(com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEventListener) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Subscribe(com.google.common.eventbus.Subscribe) Test(org.junit.Test) AbstractShardingDataBasesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest)

Aggregations

AbstractShardingDataBasesOnlyDBUnitTest (com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest)1 DMLExecutionEvent (com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEvent)1 DMLExecutionEventListener (com.dangdang.ddframe.rdb.sharding.executor.event.DMLExecutionEventListener)1 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)1 Subscribe (com.google.common.eventbus.Subscribe)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1