If FlushMode.AUTO is set, will Hibernate flush my updated persistent object when I call session.close()?
I know that session.close() does not normally flush the session but I’m not sure how FlushMode.AUTO affects this.
From the Docs:
FlushMode.AUTO
The Session is sometimes flushed before query execution in order to ensure that queries never return stale state. This is the default flush mode.
Does this mean I can rely on Hibernate to verify my changes are flushed sometimes before my session is closed?
Small code example:
Session session = HibernateSessionFactory.getSession();
PersistedObject p = session.get(PersistedObject.class,id);
p.setSomeProperty(newValue);
session.close();
UPDATE
According to the docs these are the places where the session will flush (when AUTO is used)
- before some query executions
- from org.hibernate.Transaction.commit()
- from Session.flush()
This does not say anything about Session.close()
No it won’t, and you should use a transaction with well defined boundaries. Quoting Non-transactional data access and the auto-commit mode:
Bottom line: use explicit transaction demarcation.