jdbctemplate事务回滚
需求:在service里,需要做一个操作,该操作包括多个dao方法,一旦有一个dao方法有异常,所有已执行的dao回滚。可以通过@Transactional(rollbackFor=Exception.class)注解来实现,该注解表示只要该方法下的有异常出现(包括我们自己抛出的异常),就回滚。
比如EntityServiceImpl下的deleteEntityById方法,该方法删除一个实体,删除实体之前需要删除与该实体有关联的其他表中的一些记录,比如第一步先删除bind_entities表中的相关,第二步再删实体本身,第三步再删其他的,这三步只要有异常出现就回滚。
@Transactional(rollbackFor=Exception.class)//这里要用事务。。。
@Override
public boolean deleteEntityById(String id_of_entity) throws Exception {
boolean result1=reuse_propertyService.remove_one_bind_entity(id_of_entity);
if (!result1){
throw new Exception("删除bindentitiy时出错了");
}
boolean result2 = entity_constraintDao.deleteEntity(id_of_entity);
if (!result2) {
return false;//说明没有该实体记录了
}else{
//说明实体删除成功,接下来删除编号
bianhaoDao.deleteBianhaoById_of_entity(id_of_entity);
return true;
}
}