Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off NHibernate's automatic (dirty checking) update behaviour?

Tags:

nhibernate

I've just discovered that if I get an object from an NHibernate session and change a property on object, NHibernate will automatically update the object on commit without me calling Session.Update(myObj)!

I can see how this could be helpful, but as default behaviour it seems crazy!

Update: I now understand persistence ignorance, so this behaviour is now clearly the preferred option. I'll leave this now embarrassing question here to hopefully help other profane users.

How can I stop this happening? Is this default NHibernate behaviour or something coming from Fluent NHibernate's AutoPersistenceModel?

If there's no way to stop this, what do I do? Unless I'm missing the point this behaviour seems to create a right mess.

I'm using NHibernate 2.0.1.4 and a Fluent NHibernate build from 18/3/2009

Is this guy right with his answer?

I've also read that overriding an Event Listener could be a solution to this. However, IDirtyCheckEventListener.OnDirtyCheck isn't called in this situation. Does anyone know which listener I need to override?

like image 426
Andrew Bullock Avatar asked Mar 23 '09 14:03

Andrew Bullock


1 Answers

You can set Session.FlushMode to FlushMode.Never. This will make your operations explicit

ie: on tx.Commit() or session.Flush(). Of course this will still update the database upon commit/flush. If you do not want this behavior, then call session.Evict(yourObj) and it will then become transient and NHibernate will not issue any db commands for it.

Response to your edit: Yes, that guy gives you more options on how to control it.

like image 85
Ben Scheirman Avatar answered Sep 30 '22 17:09

Ben Scheirman