I sorted it out.
The issue was that I was using the same session after the insert, to 
generate the cached query, and clearly, it was not working as expected.
This code works perfectly:

            using (var session = factory.OpenSession())
            {
                using (var tx = 
session.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    // I am creating non-cachable products
                    var products = MockFactory.MakeCachableProducts();
                    products.ForEach(p => session.Save(p));
                    tx.Commit();
                }
            }
            using (var session = factory.OpenSession())
            {
                using (var tx = 
session.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    Console.WriteLine("*** FIRST SESSION ***");
                    var result = session
                        .CreateQuery("from CachableProduct p where p.Name = 
:name")
                        .SetString("name", "PC")
                        .SetCacheable(true)
                        .List<CachableProduct>();
                    Console.WriteLine("Cached queries {0}", 
factory.Statistics.QueryCacheHitCount);
                    Console.WriteLine("Second level hits {0}", 
factory.Statistics.SecondLevelCacheHitCount);
                    tx.Commit();
                }
            }
            using (var session = factory.OpenSession())
            {
                using (var tx = 
session.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    Console.WriteLine("*** SECOND SESSION ***");
                    var result = session
                        .CreateQuery("from CachableProduct p where p.Name = 
:name")
                        .SetString("name", "PC")
                        .SetCacheable(true)
                        .List<CachableProduct>();
                    Console.WriteLine("Cached queries {0}", 
factory.Statistics.QueryCacheHitCount);
                    Console.WriteLine("Second level hits {0}", 
factory.Statistics.SecondLevelCacheHitCount);
                    tx.Commit();
                }
            }


-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/Ex84fb6VGNkJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to