Skip to content

Commit 095d5b3

Browse files
authored
Fix caching SQL queries with enabled auto discovery type (#2943)
Fixes #2904
1 parent 1978468 commit 095d5b3

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,72 @@ public async Task SQLQueryInterfaceCacheableAsync()
216216
}
217217
}
218218

219+
[Test(Description = "GH-2904")]
220+
public async Task CacheableScalarSQLQueryAsync()
221+
{
222+
Organization ifa = new Organization("IFA");
223+
Organization jboss = new Organization("JBoss");
224+
Person gavin = new Person("Gavin");
225+
Employment emp = new Employment(gavin, jboss, "AU");
226+
227+
using (ISession s = OpenSession())
228+
using (ITransaction t = s.BeginTransaction())
229+
{
230+
await (s.SaveAsync(ifa));
231+
await (s.SaveAsync(jboss));
232+
await (s.SaveAsync(gavin));
233+
await (s.SaveAsync(emp));
234+
await (t.CommitAsync());
235+
}
236+
237+
using (ISession s = OpenSession())
238+
{
239+
Task<IList> GetCacheableSqlQueryResultsAsync()
240+
{
241+
try
242+
{
243+
return s.CreateSQLQuery(
244+
"select emp.REGIONCODE " +
245+
"from ORGANIZATION org " +
246+
"left outer join EMPLOYMENT emp on org.ORGID = emp.EMPLOYER ")
247+
.AddScalar("regionCode", NHibernateUtil.String)
248+
.SetCacheable(true)
249+
.ListAsync();
250+
}
251+
catch (System.Exception ex)
252+
{
253+
return Task.FromException<IList>(ex);
254+
}
255+
}
256+
257+
using (EnableStatisticsScope())
258+
{
259+
IList l = await (GetCacheableSqlQueryResultsAsync());
260+
Assert.AreEqual(2, l.Count);
261+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "results are expected from DB");
262+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "results are expected from DB");
263+
}
264+
265+
using (EnableStatisticsScope())
266+
{
267+
IList l2 = await (GetCacheableSqlQueryResultsAsync());
268+
Assert.AreEqual(2, l2.Count);
269+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "results are expected from cache");
270+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "results are expected from cache");
271+
}
272+
}
273+
274+
using (var s = OpenSession())
275+
using (var t = s.BeginTransaction())
276+
{
277+
await (s.DeleteAsync(emp));
278+
await (s.DeleteAsync(gavin));
279+
await (s.DeleteAsync(ifa));
280+
await (s.DeleteAsync(jboss));
281+
await (t.CommitAsync());
282+
}
283+
}
284+
219285
[Test]
220286
public async Task ResultSetMappingDefinitionAsync()
221287
{

src/NHibernate.Test/SqlTest/Query/NativeSQLQueriesFixture.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,65 @@ public void SQLQueryInterfaceCacheable()
205205
}
206206
}
207207

208+
[Test(Description = "GH-2904")]
209+
public void CacheableScalarSQLQuery()
210+
{
211+
Organization ifa = new Organization("IFA");
212+
Organization jboss = new Organization("JBoss");
213+
Person gavin = new Person("Gavin");
214+
Employment emp = new Employment(gavin, jboss, "AU");
215+
216+
using (ISession s = OpenSession())
217+
using (ITransaction t = s.BeginTransaction())
218+
{
219+
s.Save(ifa);
220+
s.Save(jboss);
221+
s.Save(gavin);
222+
s.Save(emp);
223+
t.Commit();
224+
}
225+
226+
using (ISession s = OpenSession())
227+
{
228+
IList GetCacheableSqlQueryResults()
229+
{
230+
return s.CreateSQLQuery(
231+
"select emp.REGIONCODE " +
232+
"from ORGANIZATION org " +
233+
"left outer join EMPLOYMENT emp on org.ORGID = emp.EMPLOYER ")
234+
.AddScalar("regionCode", NHibernateUtil.String)
235+
.SetCacheable(true)
236+
.List();
237+
}
238+
239+
using (EnableStatisticsScope())
240+
{
241+
IList l = GetCacheableSqlQueryResults();
242+
Assert.AreEqual(2, l.Count);
243+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "results are expected from DB");
244+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(0), "results are expected from DB");
245+
}
246+
247+
using (EnableStatisticsScope())
248+
{
249+
IList l2 = GetCacheableSqlQueryResults();
250+
Assert.AreEqual(2, l2.Count);
251+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "results are expected from cache");
252+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "results are expected from cache");
253+
}
254+
}
255+
256+
using (var s = OpenSession())
257+
using (var t = s.BeginTransaction())
258+
{
259+
s.Delete(emp);
260+
s.Delete(gavin);
261+
s.Delete(ifa);
262+
s.Delete(jboss);
263+
t.Commit();
264+
}
265+
}
266+
208267
[Test]
209268
public void ResultSetMappingDefinition()
210269
{

src/NHibernate.Test/TestCase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ protected virtual ISession OpenSession()
389389
return Sfi.OpenSession();
390390
}
391391

392+
protected virtual IDisposable EnableStatisticsScope()
393+
{
394+
return new StatisticsScope(Sfi);
395+
}
396+
392397
protected virtual ISession OpenSession(IInterceptor sessionLocalInterceptor)
393398
{
394399
return Sfi.WithOptions().Interceptor(sessionLocalInterceptor).OpenSession();
@@ -574,6 +579,24 @@ public void Dispose()
574579
}
575580
}
576581

582+
protected class StatisticsScope : IDisposable
583+
{
584+
private readonly ISessionFactoryImplementor _factory;
585+
586+
public StatisticsScope(ISessionFactoryImplementor factory)
587+
{
588+
_factory = factory;
589+
_factory.Statistics.IsStatisticsEnabled = true;
590+
_factory.Statistics.Clear();
591+
}
592+
593+
public void Dispose()
594+
{
595+
_factory.Statistics.IsStatisticsEnabled = false;
596+
_factory.Statistics.Clear();
597+
}
598+
}
599+
577600
#endregion
578601
}
579602
}

src/NHibernate/Transform/CacheableResultTransformer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public override bool Equals(object o)
387387

388388
// Auto-discovery does not allow distinguishing by transformer: just compare their queries
389389
if (AutoDiscoverTypes && that.AutoDiscoverTypes)
390-
return _autoDiscoveredQuery == that._autoDiscoveredQuery;
390+
return Equals(_autoDiscoveredQuery, that._autoDiscoveredQuery);
391391

392392
return HasSameParameters(that);
393393
}

0 commit comments

Comments
 (0)