Skip to content

Commit fd0220a

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

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed
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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using NHibernate.Cfg;
2+
using NUnit.Framework;
3+
using System.Threading.Tasks;
4+
5+
namespace NHibernate.Test.NHSpecificTest.GH1730
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
protected override void Configure(Configuration configuration)
11+
{
12+
configuration.SetProperty("show_sql", "true");
13+
configuration.SetProperty("generate_statistics", "true");
14+
}
15+
16+
protected override void OnTearDown()
17+
{
18+
using (var session = OpenSession())
19+
using (var transaction = session.BeginTransaction())
20+
{
21+
session.CreateQuery("delete from Entity").ExecuteUpdate();
22+
23+
transaction.Commit();
24+
}
25+
}
26+
27+
[Test]
28+
public void HitCacheInSameSession()
29+
{
30+
Sfi.EvictQueries();
31+
Sfi.Statistics.Clear();
32+
var entities = new System.Collections.Generic.List<Entity>();
33+
34+
using (var session = OpenSession())
35+
{
36+
using (var transaction = session.BeginTransaction())
37+
{
38+
for (int i = 0; i < 3; i++)
39+
{
40+
var e = new Entity { Name = "Name" + i };
41+
entities.Add(e);
42+
session.Save(e);
43+
}
44+
transaction.Commit();
45+
}
46+
47+
var queryString = "from Entity";
48+
49+
using (var tx = session.BeginTransaction())
50+
{
51+
// this query will hit the database and create the cache
52+
session.CreateQuery(queryString).SetCacheable(true).List();
53+
tx.Commit();
54+
}
55+
56+
using (var transaction = session.BeginTransaction())
57+
{
58+
//and this one SHOULD served by the cache
59+
session.CreateQuery(queryString).SetCacheable(true).List();
60+
transaction.Commit();
61+
}
62+
63+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
64+
Assert.AreEqual(1, qs.CacheHitCount);
65+
Assert.AreEqual(1, qs.CachePutCount);
66+
}
67+
}
68+
69+
[Test]
70+
public async Task HitCacheInSameSessionAsync()
71+
{
72+
Sfi.EvictQueries();
73+
Sfi.Statistics.Clear();
74+
var entities = new System.Collections.Generic.List<Entity>();
75+
76+
using (var session = OpenSession())
77+
{
78+
using (var transaction = session.BeginTransaction())
79+
{
80+
for (int i = 0; i < 3; i++)
81+
{
82+
var e = new Entity { Name = "Name" + i };
83+
entities.Add(e);
84+
await session.SaveAsync(e);
85+
}
86+
await transaction.CommitAsync();
87+
}
88+
89+
var queryString = "from Entity";
90+
91+
using (var tx = session.BeginTransaction())
92+
{
93+
// this query will hit the database and create the cache
94+
await session.CreateQuery(queryString).SetCacheable(true).ListAsync();
95+
await tx.CommitAsync();
96+
}
97+
98+
using (var transaction = session.BeginTransaction())
99+
{
100+
//and this one SHOULD served by the cache
101+
await session.CreateQuery(queryString).SetCacheable(true).ListAsync();
102+
await transaction.CommitAsync();
103+
}
104+
105+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
106+
Assert.AreEqual(1, qs.CacheHitCount);
107+
Assert.AreEqual(1, qs.CachePutCount);
108+
}
109+
}
110+
}
111+
}
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)