[英]Communication to caller of method annotated @Transactional
I have a method on which I am using the Spring @Transactional annotation: 我有一个使用Spring @Transactional批注的方法:
@Transactional
public void persistAmendments(int _cutsheetId) {
...
}
If a condition occurs which forces a rollback of this transaction, how will a caller of persistAmendments() know about this? 如果发生迫使该事务回滚的条件,那么persistAmendments()的调用者将如何知道这一点? I would like my calling code to handle the situation appropriately.
我希望我的调用代码能够适当地处理这种情况。 Is there a special exception that is thrown up the stack?
是否有抛出堆栈的特殊异常?
The method calling your persistence layer will catch any RuntimeExceptions, and that will let it know that you had an error while persisting the data. 调用持久层的方法将捕获所有RuntimeExceptions,这将使其知道持久化数据时发生了错误。 Spring by default rolls back any transaction that throws a RuntimeException.
默认情况下,Spring回滚任何引发RuntimeException的事务。 Here is an abbreviated example of what I mean.
这是我的意思的简短示例。
AmendementService AmendementService
@Service
public class AmendmentService {
@Autowired
private AmendmentRepository amendmentRepository;
public boolean persistAmendments(int _cutsheetId) {
boolean persistSuccessful = true;
try {
amendmentRepository.persistAmendments(_cutsheetId);
} catch (RuntimeException e) {
persistSuccessful = false;
}
return persistSuccessful;
}
}
AmendmentRepository AmendmentRepository
@Repository
public class AmendmentRepository {
@Transactional
public void persistAmendments(int _cutsheetId) {
//attempt to persist
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.