org.apache.ibatis.executor.BatchResult Java Examples

The following examples show how to use org.apache.ibatis.executor.BatchResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BatchDbSqlSession.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected FlushResult postProcessBatchFailure(List<DbOperation> operations, RuntimeException e) {
  BatchExecutorException batchExecutorException = ExceptionUtil.findBatchExecutorException(e);

  if (batchExecutorException == null) {
    // Unexpected exception
    throw e;
  }

  List<BatchResult> successfulBatches = batchExecutorException.getSuccessfulBatchResults();
  BatchUpdateException cause = batchExecutorException.getBatchUpdateException();

  Iterator<DbOperation> operationsIt = operations.iterator();
  List<DbOperation> failedOperations = new ArrayList<>();

  for (BatchResult successfulBatch : successfulBatches) {
    postProcessJdbcBatchResult(operationsIt, successfulBatch.getUpdateCounts(), null, failedOperations);
  }

  int[] failedBatchUpdateCounts = cause.getUpdateCounts();
  postProcessJdbcBatchResult(operationsIt, failedBatchUpdateCounts, e, failedOperations);

  List<DbOperation> remainingOperations = CollectionUtil.collectInList(operationsIt);
  return FlushResult.withFailuresAndRemaining(failedOperations, remainingOperations);
}
 
Example #2
Source File: BatchDbSqlSession.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected FlushResult postProcessBatchSuccess(List<DbOperation> operations, List<BatchResult> batchResults) {
  Iterator<DbOperation> operationsIt = operations.iterator();
  List<DbOperation> failedOperations = new ArrayList<>();
  for (BatchResult successfulBatch : batchResults) {
    // even if all batches are successful, there can be concurrent modification failures
    // (e.g. 0 rows updated)
    postProcessJdbcBatchResult(operationsIt, successfulBatch.getUpdateCounts(), null, failedOperations);
  }

  // there should be no more operations remaining
  if (operationsIt.hasNext()) {
    throw LOG.wrongBatchResultsSizeException(operations);
  }

  return FlushResult.withFailures(failedOperations);
}
 
Example #3
Source File: BatchDbSqlSession.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public FlushResult executeDbOperations(List<DbOperation> operations) {
  for (int i = 0; i < operations.size(); i++) {

    DbOperation operation = operations.get(i);

    // stages all operations
    executeDbOperation(operation);
  }

  List<BatchResult> batchResults;
  try {
    // applies all operations
    batchResults = flushBatchOperations();
  } catch (RuntimeException e) {
    return postProcessBatchFailure(operations, e);
  }

  return postProcessBatchSuccess(operations, batchResults);
}
 
Example #4
Source File: InsertTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Ignore // Not supported yet in PostgreSQL
@Test
public void testInsertMappedBatch() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
    Name name = new Name();
    name.setFirstName("Fred");
    name.setLastName("Flintstone");
    mapper.insertNameMapped(name);
    Name name2 = new Name();
    name2.setFirstName("Wilma");
    name2.setLastName("Flintstone");
    mapper.insertNameMapped(name2);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    assertNotNull(name.getId());
    assertNotNull(name2.getId());
    assertEquals(1, batchResults.size());
  } finally {
    sqlSession.close();
  }
}
 
Example #5
Source File: NoParamTypeTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example #6
Source File: InsertTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Ignore // Not supported yet in PostgreSQL
@Test
public void testInsertMappedBatch() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
    Name name = new Name();
    name.setFirstName("Fred");
    name.setLastName("Flintstone");
    mapper.insertNameMapped(name);
    Name name2 = new Name();
    name2.setFirstName("Wilma");
    name2.setLastName("Flintstone");
    mapper.insertNameMapped(name2);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    assertNotNull(name.getId());
    assertNotNull(name2.getId());
    assertEquals(1, batchResults.size());
  } finally {
    sqlSession.close();
  }
}
 
Example #7
Source File: NoParamTypeTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example #8
Source File: DefaultSqlSession.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public List<BatchResult> flushStatements() {
  try {
    //转而用执行器来flushStatements
    return executor.flushStatements();
  } catch (Exception e) {
    throw ExceptionFactory.wrapException("Error flushing statements.  Cause: " + e, e);
  } finally {
    ErrorContext.instance().reset();
  }
}
 
Example #9
Source File: EnginePersistenceLogger.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void printBatchResults(List<BatchResult> results) {
  if (results.size() > 0) {
    StringBuilder sb = new StringBuilder();
    sb.append("Batch summary:\n");
    for (int i = 0; i < results.size(); i++) {
      BatchResult result = results.get(i);
      sb.append("Result ").append(i).append(":\t");
      sb.append(result.getSql().replaceAll("\n", "").replaceAll("\\s+", " ")).append("\t");
      sb.append("Update counts: ").append(Arrays.toString(result.getUpdateCounts())).append("\n");
    }
    logDebug("082", sb.toString());
  }
}
 
Example #10
Source File: SqlSessionManager.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public List<BatchResult> flushStatements() {
  final SqlSession sqlSession = localSqlSession.get();
  if (sqlSession == null) {
    throw new SqlSessionException("Error:  Cannot rollback.  No managed session is started.");
  }
  return sqlSession.flushStatements();
}
 
Example #11
Source File: DefaultSqlSession.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public List<BatchResult> flushStatements() {
  try {
    //转而用执行器来flushStatements
    return executor.flushStatements();
  } catch (Exception e) {
    throw ExceptionFactory.wrapException("Error flushing statements.  Cause: " + e, e);
  } finally {
    ErrorContext.instance().reset();
  }
}
 
Example #12
Source File: SqlSessionManager.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public List<BatchResult> flushStatements() {
  final SqlSession sqlSession = localSqlSession.get();
  if (sqlSession == null) {
    throw new SqlSessionException("Error:  Cannot rollback.  No managed session is started.");
  }
  return sqlSession.flushStatements();
}
 
Example #13
Source File: SqlSessionMetricsWrapper.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<BatchResult> flushStatements()
{
    return this.sqlSession.flushStatements();
}
 
Example #14
Source File: DelegatingSqlSession.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public List<BatchResult> flushStatements() {
  return wrappedSession.flushStatements();
}
 
Example #15
Source File: DbSqlSession.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public List<BatchResult> flushBatchOperations() {
  return sqlSession.flushStatements();
}
 
Example #16
Source File: ResultLoaderMap.java    From mybatis with Apache License 2.0 4 votes vote down vote up
@Override
protected List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
  throw new UnsupportedOperationException("Not supported.");
}
 
Example #17
Source File: SqlSessionWrapper.java    From live-chat-engine with Apache License 2.0 4 votes vote down vote up
@Override
public List<BatchResult> flushStatements() {
	return real.flushStatements();
}
 
Example #18
Source File: ResultLoaderMap.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Override
protected List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
  throw new UnsupportedOperationException("Not supported.");
}
 
Example #19
Source File: EntityExecutor.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public List<BatchResult> flushStatements() throws SQLException {
  return delegate.flushStatements();
}
 
Example #20
Source File: ProxySession.java    From sumk with Apache License 2.0 4 votes vote down vote up
@Override
public List<BatchResult> flushStatements() {
	return this.writeSession().flushStatements();
}
 
Example #21
Source File: SqlSession.java    From mybatis with Apache License 2.0 2 votes vote down vote up
/**
 * Flushes batch statements.
 * 刷新批处理语句,返回批处理结果
 * @return BatchResult list of updated records
 * @since 3.0.6
 */
List<BatchResult> flushStatements();
 
Example #22
Source File: JxSqlSession.java    From sinavi-jfw with Apache License 2.0 2 votes vote down vote up
/**
 * このメソッドは {@link org.apache.ibatis.session.SqlSession#flushStatements()} への単純な委譲です。
 * {@inheritDoc}
 */
public List<BatchResult> flushStatements() {
    return delegate.flushStatements();
}
 
Example #23
Source File: SqlSession.java    From mybaties with Apache License 2.0 2 votes vote down vote up
/**
 * Flushes batch statements.
 * 刷新批处理语句,返回批处理结果
 * @return BatchResult list of updated records
 * @since 3.0.6
 */
List<BatchResult> flushStatements();
 
Example #24
Source File: CustomSqlSessionTemplate.java    From galaxy with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 * @since 1.0.2
 */
public List<BatchResult> flushStatements() {
    return this.sqlSessionProxy.flushStatements();
}
 
Example #25
Source File: CustomSqlSessionTemplate.java    From tcc-transaction with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 * @since 1.0.2
 */
public List<BatchResult> flushStatements() {
    return this.sqlSessionProxy.flushStatements();
}
 
Example #26
Source File: CustomSqlSessionTemplate.java    From ApplicationPower with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.0.2
 */
public List<BatchResult> flushStatements() {
    return this.sqlSessionProxy.flushStatements();
}