session.refresh is supposed to wipe out any local changes to an entity and replace them with whatever is in the database, essentially bringing the local session up-to-date with concurrent changes. When calling session.refresh(entity), this works as intended. session.refresh(entity, LockMode.PESSIMISTIC_WRITE) is supposed to behave similarly, the only difference being that it additionally acquires a lock on the entity after refreshing it. In practice though, this doesn’t seem to work: for some reason Hibernate ORM will perform an optimistic locking check when calling session.refresh(entity, LockMode.PESSIMISTIC_WRITE) and fail with a StaleObjectStateException if there was a concurrent change. Even though that concurrent change is expected and is the very reason we’re calling refresh in the first place… The stack trace:
|