简体   繁体   English

Spring Boot 使用@Transactional 注解方法,调用其他事务性方法并抛出异常

[英]Spring Boot With @Transactional annotated method, calls other transactional methods and throws an Exception

I am trying to process a record within a @Transactional annotated method.我正在尝试在@Transactional注释方法中处理记录。 There are some dependencies from other methods that must be taken care before processing some other secondary (but necessary) business logic.在处理其他一些次要(但必要)的业务逻辑之前,必须注意其他方法的一些依赖性。 The main record seems to be saved with no problem inside the transaction, but when I try to clean some data by using another service, it throws a JpaEntityNotFoundException .主记录似乎在事务内部没有问题地保存,但是当我尝试使用其他服务清理一些数据时,它会抛出一个JpaEntityNotFoundException

I used Propagation.REQUIRES_NEW in the method that causes the issue, but with no success.我在导致问题的方法中使用了Propagation.REQUIRES_NEW ,但没有成功。 Does anyone have an idea about what I am missing or doing wrong?有没有人知道我错过了什么或做错了什么? I must not commit the main record before doing the rest of the transactional operations.在执行其余的事务操作之前,我不能提交主记录。

The exception that I am getting is: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.myproject.repository.entity.Book with id 5851445; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.myproject.repository.entity.Book with id 5851445...............我得到的例外是: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.myproject.repository.entity.Book with id 5851445; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.myproject.repository.entity.Book with id 5851445............... org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.myproject.repository.entity.Book with id 5851445; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.myproject.repository.entity.Book with id 5851445...............

Here is an example that shows somehow my issue:这是一个以某种方式显示我的问题的示例:

ServiceA class服务A类

@Service
public class ServiceA {


    public void nonTransactionalMethodA(Book book) {

        //..... Any independent logic from ServiceB

        updateAuthor(book);

        nonTransactionalMethodB();
    }

    public void nonTransactionalMethodB() {
        //post process logic ......
    }

}

ServiceB Class服务B类

  @Service
  public class ServiceB {

        @Autowired
        private BookRepository bookRepository;

        @Autowired
        private OfferRepository offerRepository;

        @Transactional
        private void updateAuthor(Author author) {
            Book book = new Book(1);
            book.setAuthor(author);
            bookRepository.save(book);
            removeUnavailableOffers(book);        
        }

        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public void removeUnavailableOffers (Book book) {
            /*throwing org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.myproject.repository.entity.Book with id 5851445; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.myproject.repository.entity.Book with id 5851445............*/
            offerRepository.deleteByBookIdIsNullAndBookAuthorIdNot(book.authorId);            
        }
    }   

Any thought on this will be greatly appreciated.对此的任何想法将不胜感激。

Make sure that the method annotated with @Transactional is declared as public and called by a different class.确保使用@Transactional注释的方法声明为public并由不同的类调用。

A transactional method must be public so that Spring can override it and it must be called from outside the defining class so that the invocation can go through a proxy.事务方法必须是公共的,以便 Spring 可以覆盖它,并且必须从定义类的外部调用它,以便调用可以通过代理。

It's one of the common pitfalls when using @Transactional, for more information seehttps://codete.com/blog/5-common-spring-transactional-pitfalls/这是使用@Transactional 时常见的陷阱之一,有关更多信息,请参阅https://codete.com/blog/5-common-spring-transactional-pitfalls/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working 在单个事务中运行spring @Transactional注释方法 - Run spring @Transactional annotated methods in a single transaction 回滚 @Transactional 注释的方法 - Rollback a @Transactional annotated method Spring AOP:具有@Transactional注释方法的方法的注释切入点? - Spring AOP : Annotated pointcuts for a method with @Transactional annotated method? 弹簧靴。 @Transactional 方法调用 @Transactional(readonly=true) 方法 - Spring Boot. @Transactional method calling a @Transactional(readonly=true) method 使用 @Transactional(propagation = Propagation.REQUIRES_NEW) 注释的服务方法的 Spring Boot 集成测试 - Spring Boot Integration Test for Service method annotated with @Transactional(propagation = Propagation.REQUIRES_NEW) 在Spring的@Transactional注释的方法中引发并捕获的未经检查的异常是否还会导致事务回滚? - Will an unchecked exception thrown and caught inside a method annotated with spring's @Transactional still cause the transaction to rollback? Spring 引导:@Transactional rollbackFor 检查异常 - Spring Boot: @Transactional rollbackFor checked Exception @Transactional 注释方法是否等待 Spring 中的成功提交? - Do @Transactional annotated methods wait for a successful commit in Spring? Spring:@Transactional @Scheduled方法抛出TransactionException - Spring : @Transactional @Scheduled method throws TransactionException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM