问题
FlushMode in both Sessions is set to AUTO.
Session A: Session begins
Session B: Session begins
Session A: Session creates new objects, Session#flush() is called, Session ends.
Session B: Session reads objects from database and Session#flush() is automatically performed before this operation. Will the newly created objects of Session A also be visible to Session B?
回答1:
It depends on your isolation level and the underlying database. Hibernate defaults the isolation level to the underlying database. MySQL's default is REPEATABLE_READ. This means that session B will see the update if A commits before B does its first read (regardless of whether this is a read of the updated entity in question, although I believe this is a MySQL specific behavior. Some databases may allow reads of unrelated entities).
If the isolation level is READ_COMITTED then session B will see the update from session A as long as session A commits before session B does the read you are interested in.
It's worth pointing out that flushes have absolutely no effect on this question unless you have absolutely no isolation. The only thing that matters is commits.
来源:https://stackoverflow.com/questions/13842357/visibility-of-objects-in-different-hibernate-sessions