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