I am using hibernate and Spring to control the transactions.
I currently have a Object Foo that I want to read from database before I update it. Something like that
@Transactional(isolation = Isolation.READ_COMMITTED)
public void update(Foo beingUpdated) {
Foo beforeUpdate = fooDao.read(beingUpdated.getId());
checkDifferences(beingUpdated, beforeUpdate);
fooDao.update(beingUpdated);
}
but my problem is that when I read Foo, the returned object is the one being updated, not the commited. Am I misunderstanding the use of Isolation? How can I read the object committed at database, not the one being updated?
Appreciate any help.
Here are some options for you:
@Transactional(propgation=REQUIRES_NEW)which will give you a new session for that method. This adds DB overhead though: you will need to re-load the entity.None of these options are very palatable, but I have used 1 and 3 successfully in a few places.