Try fast search NHibernate

17 July 2009

NHibernate Configuration : Cache

Part I : NHibernate Configuration

Part II : NHibernate Fluent Configuration

Part III : NHibernate Configuration through lambdas

As mentioned in the first post of the series NHibernate allow you to configure the cache of entities classes and collections outside the class mapping (outside *.hbm.xml file). More than a “option” this ability is what you should use because, in general, we are working over second-level-cache, to tune performance, after write all mappings, after have our application working and over all after write our tests. Even if in NH2.1.0 turning off the cache (cache.use_second_level_cache=false) the cache configuration, inside a class-mapping, is ignored ,the right place to configure the cache is the Configuration (or session-factory-configuration). Using the Configuration you have the entirely picture of the Cache configuration in one place, you don’t need to touch working mappings and you can have different configurations for different environments where you are using the same set of *.hbm.xml same entities-domain.

Cache configuration in NH2

You can configure the cache through the session-factory-configuration with:

<class-cache class="NameSpace.Entity"
usage="read-write" region="ARegion"/>
<
collection-cache collection="NameSpace.Entity.CollectionProperty"
usage="read-write" region="ARegion"/>

In this case you must even add all your mappings through the session-factory-configuration.

If you need to add your mappings by code you must use the cache configuration by code using methods of Configuration class.

SetCacheConcurrencyStrategy(string clazz, string concurrencyStrategy)
SetCacheConcurrencyStrategy(string clazz, string concurrencyStrategy, string region)
SetCollectionCacheConcurrencyStrategy(string collectionRole, string concurrencyStrategy)

The cache’s configuration by code, in NH2, is not strongly typed and is not so strange because, in NHibernate, entities are not strongly typed (you can have a persistent entity’s mapping even without a class nor interface).

Cache configuration in NH3.0.0

As you saw in previous posts NHibernate 3.0.0 has some new strongly-typed ways to configure the session-factory… the cache configuration is the first step involving entities mappings.

Perhaps some examples will be enough to explain how use it.

Configuration of the second-level-cache of an entity:

configure.EntityCache<Entity>(ce =>
{
ce.Strategy = EntityCacheUsage.NonStrictReadWrite;
ce.RegionName = "MyRegion";
});

Configuration of the second-level-cache of an entity and one of its collections:

configure.EntityCache<Entity>(ce =>
{
ce.Strategy = EntityCacheUsage.NonStrictReadWrite;
ce.RegionName = "MyRegion";
ce.Collection(e => e.Elements, cc =>
{
cc.RegionName = "MyCollectionRegion";
cc.Strategy = EntityCacheUsage.NonStrictReadWrite;
});
});

Configuration of the second-level-cache of a collection avoiding the cache of its owner:

configure.EntityCache<Entity>(ce =>
ce.Collection(e => e.Elements, cc =>
{
cc.RegionName = "MyCollectionRegion";
cc.Strategy = EntityCacheUsage.NonStrictReadWrite;
}));

Strongly typed Cache configuration now available.

3 comments:

  1. Ok, using NH 3.0 it is possible to think about using it that way. The suggestion of the XML as logical as it could be will be a nightmare for name refactoring i avoid taking decisions which will cause me to think twice before renaming something.

    I can't wait until NH3.0 is stable to start working with it...

    ReplyDelete
  2. NHibernate is very famous among the developer’s community as it has a lot of advantages for them. But it cannot handle ASP.NET worker process recycling. So the use of cache providers like NCache or MS Velocity can be a good option. NCache acts like a second level distributed cache which can enhance the performance of the app.

    ReplyDelete
  3. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

    ReplyDelete