Calling @Transactional methods from another thread (Runnable)

让人想犯罪 __ 提交于 2019-12-03 01:50:55
JB Nizet

The problem with your code is that you expect a transaction to be started when you call saveTaskResult(). This won't happen because Spring uses AOP to start and stop transactions.

If you get an instance of a transactional Spring bean from the bean factory, or through dependency injection, what you get is in fact a proxy around the bean. This proxy starts a transaction before calling the actual method, and commits or rollbacks the transaction once the method has completed.

In this case, you call a local method of the bean, without going through the transactional proxy. Put the saveTaskResult() method (annotated with @Transactional) in another Spring bean. Inject this other Spring bean into DemoService, and call the other Spring bean from the DemoService, and everything will be fine.

Yair Zaslavsky

Transactions are held at thread local storage.
If your other method is running a thread with @Transactional annotation.
The default is set to REQUIRED and this means that if you run a method annotated with @Transacitonal from a different thread, you will have a new transaction (as there is no transaction held in the thread local storage of this thread).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!