How to make NHibernate hit the database when querying an object?

I have a situation where I need NHibernate to ignore its caches and just go to the database because the data has been changed (another user on another computer changed the data). How is this possible? So far, I’m out of luck. Get, Load, Linq, doesn't matter. NHibernate does not receive the latest data.

+4
source share
1 answer

For a second-level cache, you must clear it using ISessionFactory.Evict(typeof(T)); . For the first level cache, you can simply call ISession.Clear(); .

And if you do not know when you need to clear the second level cache, you should send some information from another application to this application (via sockets or webservice ...). If this is not possible, you can create a table in the database that tells you when the data in the database was last modified, and then check the record in this table. If it has been changed, you clear the caches. Just make sure the record is updated every time some other application changes the database (you can do this using triggers or check system tables).

If you use triggers, remember to ignore the entry if you are updating nhibernate. You can do this with some variable that has the latest update time set for it, and compare it with this.

+7
source

Source: https://habr.com/ru/post/1305260/


All Articles