views:

25

answers:

1

Normally, when you define a mapping for a certain property of a class, NHibernate saves the value in the property as soon as it has been fetched. Either it's fetched immediately when getting the object, or it's lazily loaded on the first invoke of the getter of the property, but once it has been loaded from the database it's fetched.

Please correct me if I'm wrong.

Is it possible to configure a mapping such that each single invocation of the same getter causes NHibernate to go to the database and read the value again?

Background: I have a table and a corresponding class ExcelFile that manages the path's and metadata of some Excel files. Each ExcelFile object represents one file.

ExcelFile contains properties IsLocked, LockedBy and DateLocked, with the purpose to lock the file while someone is working on it. As soon as he closes the file, the lock is released.

Caching these properties can introduce race conditions:

  • Alice loads the DB entry to display it. It also displays IsLocked. It's false. NHibernate caches the value.
  • Bob does the same, it's false.
  • 5 Minutes later, Alice opens the file: The application checks IsLocked again and then sets the lock.
  • Bob opens the file: The application checks IsLocked which has been cached and is still false. So he sets the lock and opens the application.

For setting the lock, I can live with writing methods that go to the database directly using a native SQL query. So far I did the same for reading the properties.

However I need to query entries with IsLocked being set using HQL. Also the own coding of methods that simply read and write a corresponding table field introduces a second place where mapping logic resists, which is not very nice.