NHibernate doesnt refresh database value

3,199 views
Skip to first unread message

Atilla İlhan KARTAL

unread,
May 26, 2013, 4:28:49 AM5/26/13
to nhu...@googlegroups.com
Dear All;

We are working with nhibernate on a winform project. We are using a grid to list objects. When we try to run application on two different computers, the grid doesn't refresh values. When a user changes something on the list, even if the other user refreshes the grid, it doesn't show the updated values. The database is updating, though. 

We tried Session.Clear(), Session.Evict(), Session.SessionFactory.Evict(typeof(object)), Session.QueryOver().CacheMode(Refresh / Ignore)... But none of them seems to be working. We are getting lazy load exceptions. What can we do about it?

Thank you for your help in advance.

Best Regards,

--
Atilla İlhan KARTAL
Web Application & Software Architect
(Java & PHP & Registered Android Developer)
Kuşadası / Aydın / Turkey

Oskar Berggren

unread,
May 27, 2013, 5:11:54 AM5/27/13
to nhu...@googlegroups.com
Are you also actually reloading the objects? NHibernate will typically not modify your existing objects once they are loaded.

/Oskar


2013/5/26 Atilla İlhan KARTAL <atillail...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Atilla İlhan KARTAL

unread,
May 27, 2013, 5:15:38 AM5/27/13
to nhu...@googlegroups.com
We are using the code snippet below:

Session.QueryOver<M>().Where(expression).List<M>();

Is this reloading objects or do we need to use something else?

Thank you for your help in advance.

Best Regards,


2013/5/27 Oskar Berggren <oskar.b...@gmail.com>

Ricardo Peres

unread,
May 27, 2013, 9:34:12 AM5/27/13
to nhu...@googlegroups.com
Like Oskar said, NHibernate does not repopulate entities from the database unless a Refresh is called. The problem can be described as:

1 - In a session, you load some entity(ies) from the database, using Get, QueryOver, Query, or whatever;
2 - Some of the records that are mapped to the entity(ies) change in the database;
3 - In the same session, if you load the entity(ies) again, using whatever API you want, the changed columns are not mapped to the already loaded entity(ies).

You have at least 3 choices:

1 - Evict the entities from the session, by calling ISession.Evict() or ISession.Clear();
2 - Use a stateless session, which doesn't have a first level cache, so does not keep track of loaded entities;
3 - Manually call ISession.Refresh() on each loaded entity to get updated values from the database.

RP

Atilla İlhan KARTAL

unread,
May 28, 2013, 4:05:13 AM5/28/13
to nhu...@googlegroups.com
Dear Recardo;

1 - Evict the entities from the session, by calling ISession.Evict() or ISession.Clear();
-- We tried Session.SessionFactory.Evict(typeof(M)); Session.Evict(obj); Session.SessionFactory.EvictEntity(), Session.SessionFactory.EvictCollection(); but dont refresh data..
-- We tried Session.Clear(), yes object refreshed from database but we got exceptions when lazy loading.  
2 - Use a stateless session, which doesn't have a first level cache, so does not keep track of loaded entities;
-- We need lazy loading but Stateless session does not support this feature. 
3 - Manually call ISession.Refresh() on each loaded entity to get updated values from the database.
-- Did you mean ISession.Refresh() for each list / collection object to run Refresh() method. Isn't this performance issue?




2013/5/27 Ricardo Peres <rjp...@gmail.com>

Ricardo Peres

unread,
May 28, 2013, 9:14:55 AM5/28/13
to nhu...@googlegroups.com
Hello again,

Yes, calling Refresh is bad in performance terms, only to be used as last resort (IMHO). But if you evict some entity from a session, and you reissue a query, you will definitely reload from the database and get "fresh" entities.
The problem you mention about calling Clear is very strange. Can you reproduce these problems in a unit test, that is, without the binding-to-a-grid part?
Where are you keeping the session? How many sessions are there? Are you disposing of them?

RP 
Reply all
Reply to author
Forward
0 new messages