Monday 26 February 2007

NHibernate - Record re-read not showing server updates

Has an issue where re-reading a record the record did not include updates that had happened on the server:
1. Read USER record (e.g. Name=JOHN).
2. Update USER record on server (e.g. set Name=MIKE).
3. Read USER record again. Name is still JOHN.

This implied the record is being read from the cache.
The problem came down to the fact that the Data-access-object was being created as a Singleton which meant the Hibernate Session was not being opened and closed.
The record will only be re-read from the server outside of Session (i.e. Hibernate will read the record from the cache if inside the same session).

Solution was to change the Spring object mapping to not create the DAO as a singleton, this meant the session was being open/closed during page life cycle.