问题描述:由于项目需要需要定时从其他服务接口查询数据,把符合条件的数据插入预警表,结果发现插入的数据总时插入数据不全或者没插入数据。
原始代码:
public class TestServiceImpl {
@Autowired
private TestServiceImpl testServiceService;
@Autowired
private TestDao testDao;
/**
*数据保存测试
*
* @param testEntity
*/
@Transactional(rollbackFor = Exception.class)
public void saveEntity(TestEntity testEntity) {
String vin = testEntity.getVin();
Date createDate = testEntity.getCreateDate();
if (null != createDate) {
String createDateStr = DateUtil.format(createDate);
TestEntity originDrivingWarning = testDao.findByVinCAndCreateDate(vin, createDateStr);
if (null != originDrivingWarning) {
testEntity.setSid(originDrivingWarning.getSid());
testEntity.setVersion(originDrivingWarning.getVersion()+ Constants.DEFAULT_ONE);
}
}
testDao.save(testEntity);
}
/**
* 校验预警,保存记录
*/
public void checkWarning() {
PageDto<TestWarning> pageDto = testDao.findWarningList();
// 处理数据
List<TestWarning> rows = pageDto.getRows();
rows.parallelStream().forEach(warningVO->{
TestEntity testWarningEntity = new TestEntity();
testWarningEntity.setVin(warningVO.getVin());
testWarningEntity.setAddress("上海");
//保存(问题出现在这)
testServiceService.saveEntity(testWarningEntity);
});
}
}
更改后的代码:
public class TestServiceImpl {
@Autowired
private TestServiceImpl testServiceService;
@Autowired
private TestDao testDao;
/**
*数据保存测试
*
* @param testEntity
*/
@Transactional(rollbackFor = Exception.class)
public void saveEntity(TestEntity testEntity) {
String vin = testEntity.getVin();
Date createDate = testEntity.getCreateDate();
if (null != createDate) {
String createDateStr = DateUtil.format(createDate);
TestEntity originDrivingWarning = testDao.findByVinCAndCreateDate(vin, createDateStr);
if (null != originDrivingWarning) {
testEntity.setSid(originDrivingWarning.getSid());
testEntity.setVersion(originDrivingWarning.getVersion()+ Constants.DEFAULT_ONE);
}
}
testDao.save(testEntity);
}
/**
* 批量保存数据
* @param testEntityList
*/
@Transactional(rollbackFor = Exception.class)
public void batchSaveEntity(List<TestEntity>
testEntityList) {
testEntityList.parallelStream().forEach(vo->{
String vin = vo.getVin();
Date createDate = vo.getCreateDate();
if (null != createDate) {
String createDateStr = DateUtil.format(createDate);
TestEntity originDrivingWarning = testDao.findByVinCAndCreateDate(vin, createDateStr);
if (null != originDrivingWarning) {
vo.setSid(originDrivingWarning.getSid());
vo.setVersion(originDrivingWarning.getVersion()+ Constants.DEFAULT_ONE);
}
}
}
);
testDao.save(testEntityList);
}
/**
* 校验预警,保存记录
*/
public void checkWarning() {
PageDto<TestWarning> pageDto = testDao.findWarningList();
// 处理数据
List<TestWarning> rows = pageDto.getRows();
List<TestEntity> saveEntityList = new ArrayList<>();
rows.parallelStream().forEach(warningVO->{
TestEntity testWarningEntity = new TestEntity();
testWarningEntity.setVin(warningVO.getVin());
testWarningEntity.setAddress("上海");
//step1:先放在待插入列表
saveEntityList.add(testWarningEntity);
});
if(saveEntityList.size()>0){
//step2:有预警需要保存,则批量保存效率更快。
testServiceService.batchSaveEntity(saveEntityList);
}
}
}
单个插入改成批量插入以同一个事务提交数据。