Skip to content

Commit d044a5c

Browse files
committed
GH1730 Query Cache effective only after closing the session that created the cache
1 parent 2345bb4 commit d044a5c

File tree

6 files changed

+167
-2
lines changed

6 files changed

+167
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Cfg;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.GH1730
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class FixtureAsync : BugTestCase
19+
{
20+
protected override void Configure(Configuration configuration)
21+
{
22+
cfg.SetProperty(Environment.GenerateStatistics, "true");
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
session.CreateQuery("delete from Entity").ExecuteUpdate();
31+
32+
transaction.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public async Task HitCacheInSameSessionAsync()
38+
{
39+
await (Sfi.EvictQueriesAsync());
40+
Sfi.Statistics.Clear();
41+
var entities = new System.Collections.Generic.List<Entity>();
42+
43+
using (var session = OpenSession())
44+
{
45+
using (var transaction = session.BeginTransaction())
46+
{
47+
for (int i = 0; i < 3; i++)
48+
{
49+
var e = new Entity { Name = "Name" + i };
50+
entities.Add(e);
51+
await (session.SaveAsync(e));
52+
}
53+
await (transaction.CommitAsync());
54+
}
55+
56+
var queryString = "from Entity";
57+
58+
using (var tx = session.BeginTransaction())
59+
{
60+
// this query will hit the database and create the cache
61+
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
62+
await (tx.CommitAsync());
63+
}
64+
65+
using (var transaction = session.BeginTransaction())
66+
{
67+
//and this one SHOULD served by the cache
68+
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
69+
await (transaction.CommitAsync());
70+
}
71+
72+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
73+
Assert.AreEqual(1, qs.CacheHitCount);
74+
Assert.AreEqual(1, qs.CachePutCount);
75+
}
76+
}
77+
}
78+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1730
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using NHibernate.Cfg;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1730
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
protected override void Configure(Configuration configuration)
10+
{
11+
cfg.SetProperty(Environment.GenerateStatistics, "true");
12+
}
13+
14+
protected override void OnTearDown()
15+
{
16+
using (var session = OpenSession())
17+
using (var transaction = session.BeginTransaction())
18+
{
19+
session.CreateQuery("delete from Entity").ExecuteUpdate();
20+
21+
transaction.Commit();
22+
}
23+
}
24+
25+
[Test]
26+
public void HitCacheInSameSession()
27+
{
28+
Sfi.EvictQueries();
29+
Sfi.Statistics.Clear();
30+
var entities = new System.Collections.Generic.List<Entity>();
31+
32+
using (var session = OpenSession())
33+
{
34+
using (var transaction = session.BeginTransaction())
35+
{
36+
for (int i = 0; i < 3; i++)
37+
{
38+
var e = new Entity { Name = "Name" + i };
39+
entities.Add(e);
40+
session.Save(e);
41+
}
42+
transaction.Commit();
43+
}
44+
45+
var queryString = "from Entity";
46+
47+
using (var tx = session.BeginTransaction())
48+
{
49+
// this query will hit the database and create the cache
50+
session.CreateQuery(queryString).SetCacheable(true).List();
51+
tx.Commit();
52+
}
53+
54+
using (var transaction = session.BeginTransaction())
55+
{
56+
//and this one SHOULD served by the cache
57+
session.CreateQuery(queryString).SetCacheable(true).List();
58+
transaction.Commit();
59+
}
60+
61+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
62+
Assert.AreEqual(1, qs.CacheHitCount);
63+
Assert.AreEqual(1, qs.CachePutCount);
64+
}
65+
}
66+
}
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH1730">
4+
5+
<class name="Entity">
6+
<id name="Id" generator="guid.comb"/>
7+
<property name="Name"/>
8+
</class>
9+
10+
</hibernate-mapping>

src/NHibernate/Async/Cache/StandardQueryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task<bool> PutAsync(QueryKey key, ICacheAssembler[] returnTypes, IL
4141
if (isNaturalKeyLookup && result.Count == 0)
4242
return false;
4343

44-
long ts = session.Timestamp;
44+
long ts = session.Factory.Settings.CacheProvider.NextTimestamp();
4545

4646
if (Log.IsDebugEnabled())
4747
Log.Debug("caching query results in region: '{0}'; {1}", _regionName, key);

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public bool Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, bool
6060
if (isNaturalKeyLookup && result.Count == 0)
6161
return false;
6262

63-
long ts = session.Timestamp;
63+
long ts = session.Factory.Settings.CacheProvider.NextTimestamp();
6464

6565
if (Log.IsDebugEnabled())
6666
Log.Debug("caching query results in region: '{0}'; {1}", _regionName, key);

0 commit comments

Comments
 (0)