Spring4 + Hibernate4 + JTA Write Operations Fail

杀马特。学长 韩版系。学妹 提交于 2019-12-06 08:36:30
spadhi

So I finally got the write operations to work in the legacy code. Here are the steps I followed to get the code working:

  1. Ensure that you are using the Hibernate specific transaction manager org.springframework.orm.hibernate4.HibernateTransactionManager and NOT the generic org.springframework.transaction.jta.JtaTransactionManager

  2. Verify that the annotations are enabled.

  3. Add the @Transactional annotation (org.springframework.transaction.annotation.Transactional) to the methods where you perform the save/update/delete etc operations. The legacy code worked without needing this annotation but now, for the latest versions, it is required to enable write operations.

  4. In my case, I got a bunch of auto-wiring issues as soon as I added the annotation. The root cause turned out to be that some implementation classes, not interfaces, were being used to auto-wire properties at the @Service level classes. Changing the references to use the interfaces fixed that issue. You can read more about it on other threads such as this one.

I had to search and repeat the steps to fix all such instances in the legacy code. Note that setting the FlushMode to AUTO globally via OpenSessionInViewFilter is not a clean solution; there is a good reason why Spring sets the FlushMode to MANUAL by default. Spring makes the necessary runtime tweaks to support write operations when the @Transactional annotation is present. I debugged all the way to the Hibernate org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl class and the Jta synchronization works fine with the setup above. Hope this helps anyone stuck in trying to migrate legacy code to the latest versions.

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