Search code examples
javaspringjakarta-eespring-transactionstransactional

Spring Transactional annotation, isolation not working for READ_UNCOMMITTED


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?


Solution

  • 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