How to make the queries in a stored procedure aware of the Spring Transaction?

别等时光非礼了梦想. 提交于 2019-12-24 09:59:29

问题


After I execute a bunch of queries on the DB, I'm calling a stored procedure from a Spring Transaction (a Spring Service marked with @Transactional).

entityManager.createNativeQuery("call stored_procedure()");
query.executeUpdate();

In order to make the queries of the stored procedure rollback when an exception is thrown by the java code (or the modifications of the DB made in the javacode rolled back because of an exception thrown in the stored procedure), I've set the autocommit variable of the mysql server to false. This fixes part of my problem. I now have all the code to the DB transactional. My problem is that the queries of the stored procedure are not aware of the modifications (to the DB) made in the java code. I.e. the selects from the stored procedure reads the values that were in the DB before the Spring transaction started.

My question is (the one stated in the title) How to make the queries in a stored procedure aware of the modifications made to the DB in the Spring Transaction prior to the stored procedure being called (from that transaction)?

Unfortunately setting the DB to dirty read is not an option for me now. Would it be possivle to get the ID of the transaction that Spring opens on mysql, and run the queries of the stored procedure inside that transaction?

Thanks


回答1:


It turned out I need to call flush on the Entity Manager, in order to synchronise the persistence context to the underlying database. Also I removed any transaction management instruction from the SP, and it worked as expected; the queries inside the Stored Procedure are now part of the transaction managed by Spring.



来源:https://stackoverflow.com/questions/11626353/how-to-make-the-queries-in-a-stored-procedure-aware-of-the-spring-transaction

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