Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Session.Flush & Evict vs Clear

In a test where I want to persist an object and then prove it was persisted by fetching it from the db (and not the session), I notice no difference between the following:

// save it
session.Clear()
// fetch it

or

// save it
session.Flush()
session.Evict(_instance)
// fetch it

The lazy programmer in me leans towards one line over two. Is there some reason I am missing to favor the two lines more?

like image 664
Berryl Avatar asked May 17 '10 21:05

Berryl


People also ask

What does NHibernate flush do?

Flushing synchronizes the persistent store with in-memory changes but not vice-versa. Note that for all NHibernate ADO.NET connections/transactions, the transaction isolation level for that connection applies to all operations executed by NHibernate!

How do I clear my NHibernate session?

NHibernate will evict associated entities automatically if the association is mapped with cascade="all" or cascade="all-delete-orphan". The ISession also provides a Contains() method to determine if an instance belongs to the session cache. To completely evict all objects from the session cache, call ISession. Clear().

What is NHibernate session?

NHibernate is an open source object-relational mapper, or simply put, a way to rapidly retrieve data from your database into standard . NET objects. This article teaches you how to create NHibernate sessions, which use database sessions to retrieve and store data into the database.


1 Answers

session.Clear actually cancels all pending saves/updates/etc.

If it doesn't, it's because you're using identity so the entity is persisted without flushing.

like image 169
Diego Mijelshon Avatar answered Oct 11 '22 12:10

Diego Mijelshon