'nHibernate QueryCache NHibernate.ObjectNotFoundException: No row with the given identifier exists

I encounter an exception during working with nHiberante hql query with query cache. I will try describe situation:

  • nHibernate, QueryCache enabled

  • HQL query with region, and cache set to lets say 2 hours

Records from table targeted by HQL query are read and stored in cache. After some time record in table are removed by plain SQL - not using application but using SQL IDE. nHibernate starts throwing exception (not immediately but probably after expiring 2h window):

NHibernate.ObjectNotFoundException: No row with the given identifier exists *Unique key of object that nHiberante is trying load from SQL db but is not present anymore*

I have couple of questions:

  • how long nHibernate will try load such record? The same time as query cache region is set - 2h? It looks like nHiberante store key of loaded object and is trying reloading using it but if row is not present I'm assuming it should return null not missing record.
  • Is only way to get out of impact by restarting application?
  • Is there a "good" way to change nHiberante behavior and say that if there is no record in database return null instead of throwing exception?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source