Spring Transactional annotation, isolation not working for READ_UNCOMMITTED

人盡茶涼 提交于 2019-12-23 03:29:32

问题


I have a method in Java which is wrapped by a spring Transactional annotation.

I have 2 operations inside, one is delete, another one is insert. My insertion statement have to rely on the first operation (which is the delete), but now because of the first operation is not committed yet, my insertion fail (unique constraint). But this is funny that, usually within the same transaction, I should be able to read/see the uncommited operation in the same transaction (my old proprietary framework are able to do that), but this is not happening for my scenario, the second insertion still fail because it see the data is not yet deleted.

I try to use isolation READ_UNCOMMITTED, but it doesn't work.

I have to put these two operation in same transaction, because any of the failure should rollback both operations, I can't commit the first operation then proceed to the second one.

How can I do that in Spring framework?


回答1:


Generally in Hibernate , in the same transaction , while flushing(committing) it always follows a particular order.

Inserts are first executed and then deletes are executed while flushing.

So ideally in your case , since you are deleting before insert just call enitityManager.flush() explicitly after delete .

Alternatively also have a look at EntityManager.setFlushMode() method where you can set to flush mode type to either commit or auto



来源:https://stackoverflow.com/questions/37382872/spring-transactional-annotation-isolation-not-working-for-read-uncommitted

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