tags:

views:

2350

answers:

4

How do I totally disable caching in nHibernate?

+5  A: 

Use the IStatelessSession to bypass the first level cache: http://darioquintana.com.ar/blogging/?p=4

In order to use the second level cache you must explicitly configure it. You will not use it if you don't.

You can also turn off lazy loading in your mappings. lazy=false.

Matt Hinze
+3  A: 

Note IStatelessSession is I think new in Nhibernate 2.0

second level cache configuration details : Chapter 21. NHibernate.Caches

Richard
+2  A: 

Totally disabling caching is something that should not be undertaken lightly. The first level cache acts as a persistence context and manages things like object identity during a Session. This is what ensures that asking NHibernate for the same object twice in a Session actually returns the same object instance. In general usage, the first level cache shouldn't be bypassed.

Sean Carpenter
A: 

@Sean - How is this useful when the alternative is to refresh the instance(ISession.Refresh) effectively creating a Select N + 1 problem for each GetAll query ... I think caching should always be the consideration of the consumer code and not be built into the API. I have the same problem with LinqToSql. Secondly, you dont need to cache an entire object to track it's identity for an equals operation. Once again, the consumer code should decide when two objects are equal, after all what are we overriding bool Equals(object instance) for ... ???

Gavin