gpt4 book ai didi

java - 当嵌套方法中发生错误时,不会发生事务回滚

转载 作者:行者123 更新时间:2023-11-29 16:08:42 24 4
gpt4 key购买 nike

在我最近的项目中,我遇到了一个场景,当嵌套方法发生错误时,我需要回滚数据库操作。早些时候,我在单一方法上使用了@Transactional。那一次效果很好。但在当前情况下,我依赖于另一种方法。所以我需要在嵌套方法发生错误时回滚。我想用这个方法回滚

@Transactional
@Override
public String updateCustomOfficeHour(OfficeHour officeHour) {
if (officeHour.getId() == null || officeHourRepository.getFirstById(officeHour.getId()) == null)
throw new EntityNotFoundException("No Office-Hour found with this id");

OfficeHour temp = officeHourRepository.getFirstById(officeHour.getId());
List<OfficeHour> officeHours = officeHourRepository.findByFromDateAndToDate(temp.getFromDate(), temp.getToDate());

List<OfficeHour> tempList = new ArrayList<>();
for (OfficeHour officeHourObj : officeHours) {
officeHourObj.setDeleted(true);
tempList.add(officeHourObj);
}
officeHourRepository.saveAll(tempList);
this.createCustomOfficeHour(officeHour);
return "Updated Custom-Office-Hour";
}

这里我有一个数据库操作 officeHourRepository.saveAll(tempList);当方法 this.createCustomOfficeHour(officeHour); 发生错误时,我需要回滚。这个方法是

   @Transactional
@Override
public String createCustomOfficeHour(OfficeHour officeHour) {
if (officeHour.getFromDate() == null || officeHour.getToDate() == null
|| officeHour.getFirstOutTime() == null || officeHour.getLastInTime() == null
|| officeHour.getInTime() == null || officeHour.getOutTime() == null)
throw new EntityNotFoundException("Null value received for OfficeHour fields!");

if (officeHour.getToDate().compareTo(officeHour.getFromDate()) < 0)
throw new EntityNotFoundException("FromDate is Getter than ToDate");

if (officeHourRepository.isFromDateExist(officeHour.getFromDate()).longValue() > 0
|| officeHourRepository.isToDateExist(officeHour.getToDate()).longValue() > 0) {
throw new EntityNotFoundException("FromDate/ToDate is already assigned");
}

if (officeHourRepository.isDateExistsBetweenFromAndToDate(officeHour.getFromDate(), officeHour.getToDate()).longValue() > 0)
throw new EntityNotFoundException("Office Hour Already Assigned In This Range");


for (int i = 1; i <= count; i++) {
........
........
........
officeHourRepository.save(obj);

}
return "Custom Office-Hour Created";
}

这是我的Exception

public class EntityNotFoundException extends RuntimeException {

public EntityNotFoundException(String message) {
super(message);
}
}

我还在application.properties中添加了spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

任何帮助将不胜感激。

最佳答案

您的 EntityNotFoundException 是一个未经检查的异常,并且遵循 spring docs :

Spring default behavior for declarative transaction management follows EJB convention (roll back is automatic only on unchecked exceptions)

默认情况下事务应该回滚。

这是因为 updateCustomOfficeHourcreateCustomOfficeHour 在同一事务下处理(此处没有覆盖传播,因此使用默认的 REQUIRED)。

您可以在 updateCustomOfficeHour 方法上为异常添加显式回滚,但这应该是多余的:

@Transactional(rollbackFor = {EntityNotFoundException.class})

关于java - 当嵌套方法中发生错误时,不会发生事务回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55491660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com