简体   繁体   English

与方法注解者@Transactional的通信

[英]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.

相关问题 回滚 @Transactional 注释的方法 - Rollback a @Transactional annotated method 从另一个@Transactional注释方法调用@Transactional注释方法 - Call @Transactional annotated method from another @Transactional annotated method Spring AOP:具有@Transactional注释方法的方法的注释切入点? - Spring AOP : Annotated pointcuts for a method with @Transactional annotated method? 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working 如何在@Transactional带注释的方法中将状态提交给DB? - How to commit status to DB within @Transactional annotated method? 当@Transactional注释方法被多个实例并行命中时会发生什么? - What happens when a @Transactional annotated method is hit in parallel by multiple instances? 在实体上没有合并调用的Spring事务注释方法 - spring transactional annotated method without merge call on entities 如果在内部@Spring注释了方法,则Spring @Transactional注释方法是否会覆盖@Transactional(readOnly = true)方法? - Does a Spring @Transactional annotated method overwrite an @Transactional(readOnly=true) method if called within it? Spring 没有为 @service 注释的 class 创建 bean,当它的方法之一用 @transactional 注释时 - Spring is not creating the bean for @service annotated class when one of its method is annotated with @transactional Spring Boot 使用@Transactional 注解方法,调用其他事务性方法并抛出异常 - Spring Boot With @Transactional annotated method, calls other transactional methods and throws an Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM