2

I have configured second level cache on the session factory. However for the POCO entity I have not enabled caching.

I am using Fluent NHibernate for configuring the SessionFactory and the POCO entities.

Here is the configuration of the session factory:

            var cfg = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)
                          .Provider("NHibernate.Connection.DriverConnectionProvider, NHibernate")
                          .Dialect<CustomMsSql2008Dialect>()
                          .Driver<SqlAzureClientDriver>()
                          .ShowSql())
            .Cache(x =>
                {
                    x.UseQueryCache();
                    x.UseSecondLevelCache().ProviderClass<HashtableCacheProvider>().UseMinimalPuts();
                })

The POCO entity configuration is as follows:

        public CustomerConfiguration()
    {
        Table("Sys_Customer");
        DynamicUpdate();

        Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);

        Map(x => x.Code);
        Map(x => x.Name);
        Map(x => x.IsActive);
        Component(x => x.Contact).ColumnPrefix("Contact_");
        Component(x => x.Address).ColumnPrefix("Address_");
        Map(x => x.IsDeleted);
        Map(x => x.CreatedOn).Column("CreatedOn");
        Map(x => x.CreatedBy).Column("CreatedBy").Length(100);
        Map(x => x.LastModifiedOn).Column("LastModifiedOn");
        Map(x => x.LastModifiedBy).Column("LastModifiedBy").Length(100);

        Version(x => x.RowVersion).UnsavedValue("0");
    }

So clearly for the Sys_Customer entity i have not configured any Caching, yet when i run the following code: the database is hit only once:

        using (ISession s = RepositorySession)
        {
            var f = s.Get<Customer>(new Guid("75EDC0C2-4E58-43FF-B0D8-8C52FBB2D502"));
            var d = f.Code;
        }

        using (ISession s = RepositorySession)
        {
            var f = s.Get<Customer>(new Guid("75EDC0C2-4E58-43FF-B0D8-8C52FBB2D502"));
            var d = f.Code;
        }

when I remove the Cache Configuration from the SessionFactory then the database is hit twice. so clearly the entity is getting cached in the second level cache.

Can someone tell me how to avoid caching the entity into the second level cache of NHibernate????

Rohit Gupta
  • 604
  • 2
  • 6
  • 15
  • 1
    Configuring the cache and it only hits the db once, then hits twice when removing the cache config, I would expect to be normal behavior. – mxmissile Jul 30 '13 at 16:45
  • but i have not enabled cache for the entity and yet the entity is getting cached. i have not asked NHibernate to cache the Sys_Customer entity yet its getting cached. – Rohit Gupta Jul 31 '13 at 04:25
  • what i meant by removing the cache configuration was to remove the cache configuration from the NHibernate Session Factory itself. i.e. from here: .Cache(x => { x.UseQueryCache(); x.UseSecondLevelCache().ProviderClass().UseMinimalPuts(); }) – Rohit Gupta Jul 31 '13 at 04:30

1 Answers1

2

I had ClassConvention that was configured with second level cache. it looked as follows:

    public class ClassConvention : IClassConvention
{
    /// <summary>
    /// Applies the specified instance.
    /// </summary>
    /// <param name="instance">The instance.</param>
    public void Apply(IClassInstance instance)
    {
        instance.Table(instance.EntityType.Name);
        instance.LazyLoad();
        instance.Cache.NonStrictReadWrite();
    }
}

this was causing the entity to be cached into the second level cache. i removed the configuration from IClassConvention and now it is working as expected

Rohit Gupta
  • 604
  • 2
  • 6
  • 15