Fluent NHibernate, Linq and Cache is not working...

Daniel Gerst's Avatar

Daniel Gerst

02 Nov, 2010 07:33 AM via web

Hi,

this combination seems not to work. Here is what I've done
Configuration in Database

Fluently.Configure()
  .Database(
    MsSqlConfiguration.MsSql2005
      .ConnectionString(c =>
        c.FromConnectionStringWithKey(GetConnectionStringName()))
          .ShowSql()
      .Cache(c => //c.ProviderClass<HashtableCacheProvider>()
        c.UseQueryCache()
          .UseMinimalPuts())
      .UseReflectionOptimizer()
      //.UseOuterJoin()
      .DefaultSchema("dbo")
    )

configure LinqQueries in Repository:

    private INHibernateQueryable<TEntity> CreateLinqQuery<TEntity>() 
    {
        var query = _session.Linq<TEntity>();

        if (ActivateCaching)
        {
            query.QueryOptions.SetCacheRegion(GetCacheRegion<TEntity>());
            query.QueryOptions.SetCachable(ActivateCaching);
            query.QueryOptions.SetCacheMode(CachingMode);
        }

        return query; 
    }

and in EntityMapping I use:

    public EntityMap()
    {
        Cache.ReadOnly().IncludeAll();

        Id(p => p.Id);
        Map(p => p.Name);
   }

Programm:

    static void Main(string[] args)
    {
        LoadData();
        LoadData();
        Console.ReadKey();
    }

    private static void LoadData()
    {
        var session = SessionFactory.Instance.OpenSession();
        session.FlushMode = FlushMode.Commit;

        var nHibSession = new NHibSession(session);
        nHibSession.ActivateCaching = true;

        var repository = new Repository(nHibSession);
        repository.List<Entity>();

        repository.Dispose();
    }

So when I wrote a small programm to test Caching of Entity nothing works, fluent fires to sql statements to retrieve Data from SqlServer... so I think something is wrong in my code, but I can't figure it out. May someone could help me?

Best regards,
Daniel

  1. Support Staff 2 Posted by James Gregory on 05 Nov, 2010 01:32 PM

    James Gregory's Avatar

    Your cache provider is commented out?

    Have you tried executing a query with HQL or the Criteria API?

  2. 3 Posted by Daniel Gerst on 06 Nov, 2010 07:28 PM

    Daniel Gerst's Avatar

    In the wiki of fluent a example shows no provider is required... :-)...

    No we do not try to use HQL or criteria, cause our repository pattern must still work on Linq. It's used in different projects and this time our customer do not want EntityFramework 4.0, so we have to use fluent :-)... We have now the Cache running for simple queries with linq. The query cache seems to work only for statements that fetches data without joins and where clases in the sql statement.

    But when a where clause is in the sql statement or joins created, no cache is working. So we try to preload all readonly data through our Presenters/Services in each view. For the first time this seems to work, hibernate do not fires any query to database and fetch all data from the cache, but if you open a view again, all data will be retieved from database... So caching seems to me a little bit strange...

  3. Support Staff 4 Posted by James Gregory on 06 Nov, 2010 09:37 PM

    James Gregory's Avatar

    The reason I suggested you try criteria or HQL was to isolate whether the
    issue was particularly with Linq, or your queries in general. I was not
    suggesting you use HQL instead of linq, except for debugging this issue.

    You should configure a provider, otherwise NHibernate will default to using
    a default caching provider. You do not want to be using the default provider
    for production. Do a search on NHibernate Second Level Cache and have a read
    on the info out there.

    The fact that caching isn't working for queries with where clauses tells me
    that something isn't working right, as that is fairly standard functionality
    for a cache.

    On Sat, Nov 6, 2010 at 7:30 PM, Daniel Gerst <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  4. 5 Posted by Daniel Gerst on 08 Nov, 2010 12:22 PM

    Daniel Gerst's Avatar

    Hi,

    now caching works :-)... but HasMany not worked for .Inverse() is this not supported?

    Best regards,
    Daniel

  5. Support Staff 6 Posted by James Gregory on 08 Nov, 2010 01:10 PM

    James Gregory's Avatar

    Inverse is definitely supported. What's not working about it?

  6. 7 Posted by Daniel Gerst on 08 Nov, 2010 01:15 PM

    Daniel Gerst's Avatar

    Hi,

    I've forgot to set Cache().ReadWrite() on HasMany() to tell hibernate to cache entities... sorry... everythigs works fine... so it is my first project tith hibernate ;-)... but it is much more flexible than EntityFramework... :-)

    Thanks a lot for supporting me...

    Best regards,
    Daniel

  7. Support Staff 8 Posted by James Gregory on 08 Nov, 2010 01:22 PM

    James Gregory's Avatar

    Glad you got it working! (mostly without my help too! :))

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    Five times two is what?