Search in sources :

Example 1 with Jdbc3KeyGenerator

use of org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator in project mybatis-3 by mybatis.

the class SimpleStatementHandler method update.

@Override
public int update(Statement statement) throws SQLException {
    String sql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();
    KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
    int rows;
    if (keyGenerator instanceof Jdbc3KeyGenerator) {
        statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
        rows = statement.getUpdateCount();
        keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
    } else if (keyGenerator instanceof SelectKeyGenerator) {
        statement.execute(sql);
        rows = statement.getUpdateCount();
        keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
    } else {
        statement.execute(sql);
        rows = statement.getUpdateCount();
    }
    return rows;
}
Also used : Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator)

Example 2 with Jdbc3KeyGenerator

use of org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator in project mybatis-3 by mybatis.

the class BatchExecutor method doFlushStatements.

@Override
public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
    try {
        List<BatchResult> results = new ArrayList<BatchResult>();
        if (isRollback) {
            return Collections.emptyList();
        }
        for (int i = 0, n = statementList.size(); i < n; i++) {
            Statement stmt = statementList.get(i);
            applyTransactionTimeout(stmt);
            BatchResult batchResult = batchResultList.get(i);
            try {
                batchResult.setUpdateCounts(stmt.executeBatch());
                MappedStatement ms = batchResult.getMappedStatement();
                List<Object> parameterObjects = batchResult.getParameterObjects();
                KeyGenerator keyGenerator = ms.getKeyGenerator();
                if (Jdbc3KeyGenerator.class.equals(keyGenerator.getClass())) {
                    Jdbc3KeyGenerator jdbc3KeyGenerator = (Jdbc3KeyGenerator) keyGenerator;
                    jdbc3KeyGenerator.processBatch(ms, stmt, parameterObjects);
                } else if (!NoKeyGenerator.class.equals(keyGenerator.getClass())) {
                    //issue #141
                    for (Object parameter : parameterObjects) {
                        keyGenerator.processAfter(this, ms, stmt, parameter);
                    }
                }
            } catch (BatchUpdateException e) {
                StringBuilder message = new StringBuilder();
                message.append(batchResult.getMappedStatement().getId()).append(" (batch index #").append(i + 1).append(")").append(" failed.");
                if (i > 0) {
                    message.append(" ").append(i).append(" prior sub executor(s) completed successfully, but will be rolled back.");
                }
                throw new BatchExecutorException(message.toString(), e, results, batchResult);
            }
            results.add(batchResult);
        }
        return results;
    } finally {
        for (Statement stmt : statementList) {
            closeStatement(stmt);
        }
        currentSql = null;
        statementList.clear();
        batchResultList.clear();
    }
}
Also used : MappedStatement(org.apache.ibatis.mapping.MappedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) MappedStatement(org.apache.ibatis.mapping.MappedStatement) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) BatchUpdateException(java.sql.BatchUpdateException)

Aggregations

Jdbc3KeyGenerator (org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator)2 KeyGenerator (org.apache.ibatis.executor.keygen.KeyGenerator)2 BatchUpdateException (java.sql.BatchUpdateException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 NoKeyGenerator (org.apache.ibatis.executor.keygen.NoKeyGenerator)1 SelectKeyGenerator (org.apache.ibatis.executor.keygen.SelectKeyGenerator)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1