How to solve the problem when the thread pool runtime finds that the transaction of the job method is not valid?
This article mainly explains the "thread pool runtime found that the job method transactions do not take effect how to solve", the article explained the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "thread pool runtime found job method transactions do not take effect how to solve" it!
Problem description:
The thread pool is used to run the instance method of the same class in the project. The code is roughly as follows. The runtime finds that the transaction of the job method does not take effect.
@ Transactional public void doJob () {EXECOTOR.execute (()-> job ());} @ Transactional public void job () {/ / db operation}
Cause analysis:
When debug enters the job method, it is found that the instance running the job method is a normal instance rather than a proxy object, so aop enhancement cannot be implemented.
Solution:
Call AopContext.currentProxy () in doJob to get the current proxy object, and call the job method on the proxy object
Transactional public void doJob () {AAAService proxy = (AAAService) AopContext.currentProxy (); EXECOTOR.execute (()-> proxy.job ());} @ Transactional public void job () {/ / db operation}
Thank you for your reading, the above is the "thread pool runtime found that the transaction of the job method does not take effect how to solve the content", after the study of this article, I believe you on the thread pool runtime found that the transaction of the job method does not take effect how to solve this problem has a more profound experience, the specific use of the need for everyone to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!