Example usage for org.apache.ibatis.executor BatchResult BatchResult

List of usage examples for org.apache.ibatis.executor BatchResult BatchResult

Introduction

In this page you can find the example usage for org.apache.ibatis.executor BatchResult BatchResult.

Prototype

public BatchResult(MappedStatement mappedStatement, String sql) 

Source Link

Usage

From source file:com.luxoft.mybatis.splitter.ReusingBatchExecutor.java

License:Apache License

private void executeUpTo(PreparedStatementKey lastToExecute, boolean moveToReuse) throws SQLException {
    for (Iterator<Map.Entry<PreparedStatementKey, StatementData>> iterator = statementsData.entrySet()
            .iterator(); iterator.hasNext();) {
        Map.Entry<PreparedStatementKey, StatementData> entry = iterator.next();
        StatementData statementData = entry.getValue();
        Statement stmt = statementData.getStatement();
        BatchResult batchResult = new BatchResult(entry.getKey().getMappedStatement(), entry.getKey().getSql());
        batchResult.getParameterObjects().addAll(entry.getValue().getParameterObjects());
        try {/*from  w ww  .j  ava 2s .com*/
            batchResult.setUpdateCounts(stmt.executeBatch());
            MappedStatement ms = entry.getKey().getMappedStatement();
            List<Object> parameterObjects = statementData.getParameterObjects();
            KeyGenerator keyGenerator = ms.getKeyGenerator();
            if (keyGenerator instanceof Jdbc3KeyGenerator) {
                Jdbc3KeyGenerator jdbc3KeyGenerator = (Jdbc3KeyGenerator) keyGenerator;
                jdbc3KeyGenerator.processBatch(ms, stmt, parameterObjects);
            } else {
                for (Object parameter : parameterObjects) {
                    keyGenerator.processAfter(this, ms, stmt, parameter);
                }
            }
        } catch (BatchUpdateException e) {
            List<BatchResult> batchResults = results;
            results = new ArrayList<BatchResult>();
            throw new BatchExecutorException(
                    entry.getKey().getMappedStatement().getId() + " (batch query " + entry.getKey().getSql()
                            + ")" + " failed. Prior " + batchResults.size()
                            + " queries completed successfully, but will be rolled back.",
                    e, batchResults, batchResult);
        }
        results.add(batchResult);
        if (moveToReuse) {
            iterator.remove();
            unusedStatementData.put(entry.getKey(), statementData);
        }
        if (entry.getKey().equals(lastToExecute)) {
            break;
        }
    }
}

From source file:org.mybatis.spring.batch.builder.MyBatisBatchItemWriterBuilderTest.java

License:Apache License

@BeforeEach
void setUp() {/*from w  ww.  j  a  v a  2  s  . co  m*/
    MockitoAnnotations.initMocks(this);
    {
        Configuration configuration = new Configuration();
        Environment environment = new Environment("unittest", new JdbcTransactionFactory(), dataSource);
        configuration.setEnvironment(environment);
        Mockito.when(this.sqlSessionFactory.getConfiguration()).thenReturn(configuration);
        Mockito.when(this.sqlSessionFactory.openSession(ExecutorType.BATCH)).thenReturn(this.sqlSession);
    }
    {
        BatchResult result = new BatchResult(null, null);
        result.setUpdateCounts(new int[] { 1 });
        Mockito.when(this.sqlSession.flushStatements()).thenReturn(Collections.singletonList(result));
    }
}

From source file:org.mybatis.spring.batch.MyBatisBatchItemWriterTest.java

License:Apache License

@Test
public void testZeroUpdateCountShouldThrowException() {
    List<Employee> employees = Arrays.asList(new Employee(), new Employee());

    BatchResult batchResult = new BatchResult(null, null);
    batchResult.setUpdateCounts(new int[] { 1, 0 });
    List<BatchResult> batchResults = Collections.singletonList(batchResult);

    given(mockSqlSessionTemplate.flushStatements()).willReturn(batchResults);

    assertThrows(EmptyResultDataAccessException.class, () -> writer.write(employees));
}