Skip to content

Commit 3c4d3b7

Browse files
authored
Skip caching for auto discovery type queries with result transformer (#3172)
Fixes #3169
1 parent ba574e9 commit 3c4d3b7

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,55 @@ Task<IList> GetCacheableSqlQueryResultsAsync()
282282
}
283283
}
284284

285+
class ResultDto
286+
{
287+
public string regionCode { get; set; }
288+
}
289+
290+
[Test(Description = "GH-3169")]
291+
public async Task CacheableScalarSQLQueryWithTransformerAsync()
292+
{
293+
Organization ifa = new Organization("IFA");
294+
295+
using (ISession s = OpenSession())
296+
using (ITransaction t = s.BeginTransaction())
297+
{
298+
await (s.SaveAsync(ifa));
299+
await (t.CommitAsync());
300+
}
301+
302+
async Task AssertQueryAsync(bool fromCache)
303+
{
304+
using (var s = OpenSession())
305+
using (var t = s.BeginTransaction())
306+
using (EnableStatisticsScope())
307+
{
308+
var l = await (s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org")
309+
.AddScalar("regionCode", NHibernateUtil.String)
310+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
311+
.SetCacheable(true)
312+
.ListAsync());
313+
await (t.CommitAsync());
314+
315+
Assert.AreEqual(1, l.Count);
316+
//TODO: Uncomment if we properly fix caching auto discovery type queries with transformers
317+
// var msg = "results are expected from " + (fromCache ? "cache" : "DB");
318+
// Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg);
319+
// Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg);
320+
}
321+
}
322+
323+
await (AssertQueryAsync(false));
324+
await (AssertQueryAsync(true));
325+
326+
using (var s = OpenSession())
327+
using (var t = s.BeginTransaction())
328+
{
329+
await (s.DeleteAsync(ifa));
330+
await (t.CommitAsync());
331+
}
332+
}
333+
285334
[Test]
286335
public async Task ResultSetMappingDefinitionAsync()
287336
{

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,55 @@ IList GetCacheableSqlQueryResults()
264264
}
265265
}
266266

267+
class ResultDto
268+
{
269+
public string regionCode { get; set; }
270+
}
271+
272+
[Test(Description = "GH-3169")]
273+
public void CacheableScalarSQLQueryWithTransformer()
274+
{
275+
Organization ifa = new Organization("IFA");
276+
277+
using (ISession s = OpenSession())
278+
using (ITransaction t = s.BeginTransaction())
279+
{
280+
s.Save(ifa);
281+
t.Commit();
282+
}
283+
284+
void AssertQuery(bool fromCache)
285+
{
286+
using (var s = OpenSession())
287+
using (var t = s.BeginTransaction())
288+
using (EnableStatisticsScope())
289+
{
290+
var l = s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org")
291+
.AddScalar("regionCode", NHibernateUtil.String)
292+
.SetResultTransformer(Transformers.AliasToBean<ResultDto>())
293+
.SetCacheable(true)
294+
.List();
295+
t.Commit();
296+
297+
Assert.AreEqual(1, l.Count);
298+
//TODO: Uncomment if we properly fix caching auto discovery type queries with transformers
299+
// var msg = "results are expected from " + (fromCache ? "cache" : "DB");
300+
// Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg);
301+
// Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg);
302+
}
303+
}
304+
305+
AssertQuery(false);
306+
AssertQuery(true);
307+
308+
using (var s = OpenSession())
309+
using (var t = s.BeginTransaction())
310+
{
311+
s.Delete(ifa);
312+
t.Commit();
313+
}
314+
}
315+
267316
[Test]
268317
public void ResultSetMappingDefinition()
269318
{

src/NHibernate/Loader/Loader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,8 @@ protected IList List(ISessionImplementor session, QueryParameters queryParameter
18291829

18301830
internal bool IsCacheable(QueryParameters queryParameters)
18311831
{
1832-
return _factory.Settings.IsQueryCacheEnabled && queryParameters.Cacheable;
1832+
return _factory.Settings.IsQueryCacheEnabled && queryParameters.Cacheable
1833+
&& !(queryParameters.HasAutoDiscoverScalarTypes && queryParameters.ResultTransformer != null);
18331834
}
18341835

18351836
private IList ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)

0 commit comments

Comments
 (0)