views:

24

answers:

0

I'm using NHibernate with ASP.NET and I initialized a session factory at Application_Start that is used to produce sessions for requests. I configured NHibernate to use second level cahe and I use an entity called Post configured to be cached.

I have two scenarios:

  • The first scenario

    var session1 = sessionFactory.OpenSession();
    var session2 = sessionFactory.OpenSession();
    var session3 = sessionFactory.OpenSession();
    var postID = Guid.NewGuid();
    
    // This will insert a new post in the database
    using(var tx1 = session1.BeginTransaction()) {
        try {
            var post = new Post() { ID = postID, Name = "My post" };
            session1.Save(post);
            tx1.Commit();
        } catch(Exception) {
            tx1.Rollback();
        }
    }
    
    // This will get the post inserted by session1
    var sess1Post = session2.Get(postID);
    
    // This will update the post inserted by session1
    using(var tx2 = session2.BeginTransaction()) {
        try {
            var sess1Post.Name = "My updated post";
            session2.Update(sess1Post);
            tx2.Commit();
        } catch(Exception) {
            tx2.Rollback();
        }
    }
    
    // This will get the post updated by session2
    var sess2Post = session3.Get(postID);
    

In this scenario the database will get hit for every operation although I was expecting session2.Get and session3.Get to pull out the post from the second level cache. It seems like the data written in second level cache after the sessions were created is not available for the other sessions.

  • The second scenario

    var session1 = sessionFactory.OpenSession();
    var postID = Guid.NewGuid();
    
    // This will insert a new post in the database
    using(var tx1 = session1.BeginTransaction()) {
        try {
            var post = new Post() { ID = postID, Name = "My post" };
            session1.Save(post);
            tx1.Commit();
        } catch(Exception) {
            tx1.Rollback();
        }
    }
    
    var session2 = sessionFactory.OpenSession();
    //This will get the post inserted by session1
    var sess1Post = session2.Get(postID);
    
    // This will update the post inserted by session1
    using(var tx2 = session2.BeginTransaction()) {
        try {
            var sess1Post.Name = "My updated post";
            session2.Update(sess1Post);
            tx2.Commit();
        } catch(Exception) {
            tx2.Rollback();
        }
    }
    
    var session3 = sessionFactory.OpenSession();
    // This will get the post updated by session2
    var sess2Post = session3.Get(postID);
    

In this scenario the database will get hit only two times for the insert and for the update operations which is what I'm expecting from the first scenario too.

My problem is that I want to use NHibernate with Memcache provider for the second level cache and setup multiple web applications each using one sessionFactory and each sessionFactory will use the same cache server. Now I'm expecting that parallel sessions use the same second level cache data.

Is the first scenario a bug or it is the expected behaviour of NHibernate?