Spring3 's @Transactional @Scheduled not committed to DB?

北慕城南 提交于 2019-11-30 13:41:24

You probably have figured this out or moved on (I hope so), but for the benefit of others:

The @Transactional annotation tells Spring to wrap your original ServiceImpl bean with a dynamic proxy that also implements 'Service' (by default Spring proxies the interface, not the implementation). This proxy will transparently handle the creation and commit/rollback of the transaction when you call hourly() on the proxy. However, if you call hourly() directly on your implementation (which is what is happening above), the proxy is bypassed, so there is no transaction.

http://blog.springsource.org/2012/05/23/understanding-proxy-usage-in-spring/

The solution is to either

  1. Demarcate the transaction programmatically as you are doing in your 'dirty' solution (you don't need the annotations is this case).
  2. Make sure that your @Scheduled method makes its call to dao.update(xxx); via the Service interface, not directly on your implementation (thereby going through the proxy). Basically you need to move the @Scheduled method to another bean.

I hope that is clear enough!

When you use annotation-driven support, it only works on classes created within that context. My bet is that ServiceImpl is not created in the same context as your transaction manager (either directly or by annotation scanning).

I had the same problem and after spending time on it, I realized that I got an exception after the dao.update() call in some unrelated code that didn't check null value - so it simply broke the transaction. There was no stackTrace printing because it has been treated well by spring (some catch block). I spent a while on that. So - just verify that your transaction method completes till its end. Hope it will help someone.

Yosi Lev

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