diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1730/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1730/Fixture.cs new file mode 100644 index 00000000000..3b9b66878df --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1730/Fixture.cs @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using NHibernate.Cfg; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1730 +{ + using System.Threading.Tasks; + [TestFixture] + public class FixtureAsync : BugTestCase + { + protected override void Configure(Configuration configuration) + { + cfg.SetProperty(Environment.GenerateStatistics, "true"); + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.CreateQuery("delete from Entity").ExecuteUpdate(); + + transaction.Commit(); + } + } + + [Test] + public async Task HitCacheInSameSessionAsync() + { + await (Sfi.EvictQueriesAsync()); + Sfi.Statistics.Clear(); + var entities = new System.Collections.Generic.List(); + + using (var session = OpenSession()) + { + using (var transaction = session.BeginTransaction()) + { + for (int i = 0; i < 3; i++) + { + var e = new Entity { Name = "Name" + i }; + entities.Add(e); + await (session.SaveAsync(e)); + } + await (transaction.CommitAsync()); + } + + var queryString = "from Entity"; + + using (var tx = session.BeginTransaction()) + { + // this query will hit the database and create the cache + await (session.CreateQuery(queryString).SetCacheable(true).ListAsync()); + await (tx.CommitAsync()); + } + + using (var transaction = session.BeginTransaction()) + { + //and this one SHOULD served by the cache + await (session.CreateQuery(queryString).SetCacheable(true).ListAsync()); + await (transaction.CommitAsync()); + } + + var qs = Sfi.Statistics.GetQueryStatistics(queryString); + Assert.AreEqual(1, qs.CacheHitCount); + Assert.AreEqual(1, qs.CachePutCount); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1730/Entity.cs b/src/NHibernate.Test/NHSpecificTest/GH1730/Entity.cs new file mode 100644 index 00000000000..2ba2a997abd --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1730/Entity.cs @@ -0,0 +1,10 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.GH1730 +{ + class Entity + { + public virtual Guid Id { get; set; } + public virtual string Name { get; set; } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1730/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/GH1730/Fixture.cs new file mode 100644 index 00000000000..7d69c1c48bb --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1730/Fixture.cs @@ -0,0 +1,67 @@ +using NHibernate.Cfg; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1730 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void Configure(Configuration configuration) + { + cfg.SetProperty(Environment.GenerateStatistics, "true"); + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.CreateQuery("delete from Entity").ExecuteUpdate(); + + transaction.Commit(); + } + } + + [Test] + public void HitCacheInSameSession() + { + Sfi.EvictQueries(); + Sfi.Statistics.Clear(); + var entities = new System.Collections.Generic.List(); + + using (var session = OpenSession()) + { + using (var transaction = session.BeginTransaction()) + { + for (int i = 0; i < 3; i++) + { + var e = new Entity { Name = "Name" + i }; + entities.Add(e); + session.Save(e); + } + transaction.Commit(); + } + + var queryString = "from Entity"; + + using (var tx = session.BeginTransaction()) + { + // this query will hit the database and create the cache + session.CreateQuery(queryString).SetCacheable(true).List(); + tx.Commit(); + } + + using (var transaction = session.BeginTransaction()) + { + //and this one SHOULD served by the cache + session.CreateQuery(queryString).SetCacheable(true).List(); + transaction.Commit(); + } + + var qs = Sfi.Statistics.GetQueryStatistics(queryString); + Assert.AreEqual(1, qs.CacheHitCount); + Assert.AreEqual(1, qs.CachePutCount); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1730/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/GH1730/Mappings.hbm.xml new file mode 100644 index 00000000000..e18236aa712 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1730/Mappings.hbm.xml @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/src/NHibernate/Async/Cache/StandardQueryCache.cs b/src/NHibernate/Async/Cache/StandardQueryCache.cs index 180064d1ad2..a85a0916311 100644 --- a/src/NHibernate/Async/Cache/StandardQueryCache.cs +++ b/src/NHibernate/Async/Cache/StandardQueryCache.cs @@ -41,7 +41,7 @@ public async Task PutAsync(QueryKey key, ICacheAssembler[] returnTypes, IL if (isNaturalKeyLookup && result.Count == 0) return false; - long ts = session.Timestamp; + long ts = session.Factory.Settings.CacheProvider.NextTimestamp(); if (Log.IsDebugEnabled()) Log.Debug("caching query results in region: '{0}'; {1}", _regionName, key); diff --git a/src/NHibernate/Cache/StandardQueryCache.cs b/src/NHibernate/Cache/StandardQueryCache.cs index b8c612dd520..a1643d0f998 100644 --- a/src/NHibernate/Cache/StandardQueryCache.cs +++ b/src/NHibernate/Cache/StandardQueryCache.cs @@ -60,7 +60,7 @@ public bool Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, bool if (isNaturalKeyLookup && result.Count == 0) return false; - long ts = session.Timestamp; + long ts = session.Factory.Settings.CacheProvider.NextTimestamp(); if (Log.IsDebugEnabled()) Log.Debug("caching query results in region: '{0}'; {1}", _regionName, key);