tags:

views:

786

answers:

6

nHibernate is not able to retrieve manually changed data from repository table? I have disabled second level cache also but looks like it(nhibernate) is retrieving sometimes from cache and sometimes from repository table.

A: 

I think you're going to have to be a bit more descriptive of your exact problem to get an answer; I have some minimal experience with nhibernate, but I can't really go about replicating what's going on and try to fix it without, say, some code.

mmr
A: 

Srry for being too specific

Nhibernate is retrieving data without any problem. But When I manually change data in a repository table. the Icriterea(nhibernate) is sometimes picking up from cache or from the table. i am using Icriteria funcnality:

ICriteria criteria = session.CreateCriteria(typeof(xyzclass)); criteria.Add(Expression.Eq("xyzclass", somestringto retreivedata)); criteria.SetCacheable(false); return criteria.UniqueResult();

alice7
A: 

I've never actually used the caching features of NHibernate first hand, but I believe the intent of it is to remove the need to go to the database at all, meaning it wouldn't pick up manual changes because it doesn't know about them.

I'd question exactly what it is you're trying to achieve here, are you just doing some testing by manually editing the database or will this be a regular activity in a live application? Really, the only thing that should be modifying your database is the application, doing so through your NHibernate data layer, therefor updating and/or dirtying the cache in the process.

roryf
A: 

THis is just part of testing.

But in the future we could edit the datatable manually. I have also set lazy to false for that table and commented out all the second level cache properties used by hibernate. But even then it is returning me different values instead of newly edited value. Sometimes it is giving me old value and sometimes the new, so it is not constant.

alice7
+1  A: 

I don't know whether it will solve the problem you're having, but the documentation states:

To completely evict all objects from the session cache, call ISession.Clear()

For the second-level cache, there are methods defined on ISessionFactory for evicting the cached state of an instance, entire class, collection instance or entire collection role.

If you did this every time you did something that executes a SELECT on data likely to change out of band, it should do what you want.

+3  A: 

There are two types of caches in nhibernate: session caches and second-level caches. The session cache is always caching objects seen by that session - it's how nhibernate knows which objects have changed and need to be persisted. The second-level cache, which you disabled, is below that. The information you're seeing cached is coming from the session cache.

If your application needs to see changes persisted by other sources (say, manual database changes), the answer is probably to create sessions at a finer granularity. While a SessionFactory lives for the life of your application, a Session object should be created much more often. For example, in a web application, every request generates its own session.

If that's not an option, session.Clear() will evict all objects from the session.

Isaac Cambron