记录一个问题今天在开发中遇到了这样一个问题
由于数据量比较大,采用批量插入手动提交事务,在使用stream流进行处理时,想着并行流多线程进行数据处理耗时会少一点,通过parallel方法开启并行流,处理完成后在用打算进行forEach遍历入库,这是代码简单贴一下
List<Map<Integer, String>> dataList = new ArrayList<>();
//获取sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
try {
//获取mapper
MapperRepository mapper = sqlSession.getMapper(MapperRepository.class);
//开启并行流
dataList.stream().parallel()
//构建对象对数据进行处理赋值
.map(map -> ComplaintAppeal.builder()
........
.build())
//插入
.forEach(mapper::insert);
sqlSession.commit();
sqlSession.clearCache();
} catch (Exception e) {
log.info("批量插入失败:" + e);
//未提交的数据可以回滚
sqlSession.rollback();
} finally {