Skip to content

Commit a914425

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

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
configuration.SetProperty("show_sql", "true");
23+
configuration.SetProperty("generate_statistics", "true");
24+
}
25+
26+
protected override void OnTearDown()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
session.CreateQuery("delete from Entity").ExecuteUpdate();
32+
33+
transaction.Commit();
34+
}
35+
}
36+
37+
[Test]
38+
public async Task HitCacheInSameSessionAsync()
39+
{
40+
await (Sfi.EvictQueriesAsync());
41+
Sfi.Statistics.Clear();
42+
var entities = new System.Collections.Generic.List<Entity>();
43+
44+
using (var session = OpenSession())
45+
{
46+
using (var transaction = session.BeginTransaction())
47+
{
48+
for (int i = 0; i < 3; i++)
49+
{
50+
var e = new Entity { Name = "Name" + i };
51+
entities.Add(e);
52+
await (session.SaveAsync(e));
53+
}
54+
await (transaction.CommitAsync());
55+
}
56+
57+
var queryString = "from Entity";
58+
59+
using (var tx = session.BeginTransaction())
60+
{
61+
// this query will hit the database and create the cache
62+
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
63+
await (tx.CommitAsync());
64+
}
65+
66+
using (var transaction = session.BeginTransaction())
67+
{
68+
//and this one SHOULD served by the cache
69+
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
70+
await (transaction.CommitAsync());
71+
}
72+
73+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
74+
Assert.AreEqual(1, qs.CacheHitCount);
75+
Assert.AreEqual(1, qs.CachePutCount);
76+
}
77+
}
78+
}
79+
}
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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
configuration.SetProperty("show_sql", "true");
12+
configuration.SetProperty("generate_statistics", "true");
13+
}
14+
15+
protected override void OnTearDown()
16+
{
17+
using (var session = OpenSession())
18+
using (var transaction = session.BeginTransaction())
19+
{
20+
session.CreateQuery("delete from Entity").ExecuteUpdate();
21+
22+
transaction.Commit();
23+
}
24+
}
25+
26+
[Test]
27+
public void HitCacheInSameSession()
28+
{
29+
Sfi.EvictQueries();
30+
Sfi.Statistics.Clear();
31+
var entities = new System.Collections.Generic.List<Entity>();
32+
33+
using (var session = OpenSession())
34+
{
35+
using (var transaction = session.BeginTransaction())
36+
{
37+
for (int i = 0; i < 3; i++)
38+
{
39+
var e = new Entity { Name = "Name" + i };
40+
entities.Add(e);
41+
session.Save(e);
42+
}
43+
transaction.Commit();
44+
}
45+
46+
var queryString = "from Entity";
47+
48+
using (var tx = session.BeginTransaction())
49+
{
50+
// this query will hit the database and create the cache
51+
session.CreateQuery(queryString).SetCacheable(true).List();
52+
tx.Commit();
53+
}
54+
55+
using (var transaction = session.BeginTransaction())
56+
{
57+
//and this one SHOULD served by the cache
58+
session.CreateQuery(queryString).SetCacheable(true).List();
59+
transaction.Commit();
60+
}
61+
62+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
63+
Assert.AreEqual(1, qs.CacheHitCount);
64+
Assert.AreEqual(1, qs.CachePutCount);
65+
}
66+
}
67+
}
68+
}
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)