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

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

Introduction

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

Prototype

public BatchExecutorException(String message, BatchUpdateException cause,
            List<BatchResult> successfulBatchResults, BatchResult batchResult) 

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 w  w.  j  av  a 2  s . co  m
            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;
        }
    }
}