From 3c4d3b737b6c820d4cbdd8b28e61d2093e243931 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Tue, 4 Oct 2022 13:30:01 +0300 Subject: [PATCH] Skip caching for auto discovery type queries with result transformer (#3172) Fixes #3169 --- .../SqlTest/Query/NativeSQLQueriesFixture.cs | 49 +++++++++++++++++++ .../SqlTest/Query/NativeSQLQueriesFixture.cs | 49 +++++++++++++++++++ src/NHibernate/Loader/Loader.cs | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs b/src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs index e0bc9034324..5ef4ebad9d8 100644 --- a/src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs +++ b/src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs @@ -282,6 +282,55 @@ Task GetCacheableSqlQueryResultsAsync() } } + class ResultDto + { + public string regionCode { get; set; } + } + + [Test(Description = "GH-3169")] + public async Task CacheableScalarSQLQueryWithTransformerAsync() + { + Organization ifa = new Organization("IFA"); + + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + await (s.SaveAsync(ifa)); + await (t.CommitAsync()); + } + + async Task AssertQueryAsync(bool fromCache) + { + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + using (EnableStatisticsScope()) + { + var l = await (s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org") + .AddScalar("regionCode", NHibernateUtil.String) + .SetResultTransformer(Transformers.AliasToBean()) + .SetCacheable(true) + .ListAsync()); + await (t.CommitAsync()); + + Assert.AreEqual(1, l.Count); + //TODO: Uncomment if we properly fix caching auto discovery type queries with transformers + // var msg = "results are expected from " + (fromCache ? "cache" : "DB"); + // Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg); + // Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg); + } + } + + await (AssertQueryAsync(false)); + await (AssertQueryAsync(true)); + + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + { + await (s.DeleteAsync(ifa)); + await (t.CommitAsync()); + } + } + [Test] public async Task ResultSetMappingDefinitionAsync() { diff --git a/src/NHibernate.Test/SqlTest/Query/NativeSQLQueriesFixture.cs b/src/NHibernate.Test/SqlTest/Query/NativeSQLQueriesFixture.cs index 6cfe83845e3..80eeb3c4c73 100644 --- a/src/NHibernate.Test/SqlTest/Query/NativeSQLQueriesFixture.cs +++ b/src/NHibernate.Test/SqlTest/Query/NativeSQLQueriesFixture.cs @@ -264,6 +264,55 @@ IList GetCacheableSqlQueryResults() } } + class ResultDto + { + public string regionCode { get; set; } + } + + [Test(Description = "GH-3169")] + public void CacheableScalarSQLQueryWithTransformer() + { + Organization ifa = new Organization("IFA"); + + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + s.Save(ifa); + t.Commit(); + } + + void AssertQuery(bool fromCache) + { + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + using (EnableStatisticsScope()) + { + var l = s.CreateSQLQuery("select org.NAME as regionCode from ORGANIZATION org") + .AddScalar("regionCode", NHibernateUtil.String) + .SetResultTransformer(Transformers.AliasToBean()) + .SetCacheable(true) + .List(); + t.Commit(); + + Assert.AreEqual(1, l.Count); + //TODO: Uncomment if we properly fix caching auto discovery type queries with transformers + // var msg = "results are expected from " + (fromCache ? "cache" : "DB"); + // Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(fromCache ? 0 : 1), msg); + // Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(fromCache ? 1 : 0), msg); + } + } + + AssertQuery(false); + AssertQuery(true); + + using (var s = OpenSession()) + using (var t = s.BeginTransaction()) + { + s.Delete(ifa); + t.Commit(); + } + } + [Test] public void ResultSetMappingDefinition() { diff --git a/src/NHibernate/Loader/Loader.cs b/src/NHibernate/Loader/Loader.cs index 8069cc9a544..ba56146dbbd 100644 --- a/src/NHibernate/Loader/Loader.cs +++ b/src/NHibernate/Loader/Loader.cs @@ -1829,7 +1829,8 @@ protected IList List(ISessionImplementor session, QueryParameters queryParameter internal bool IsCacheable(QueryParameters queryParameters) { - return _factory.Settings.IsQueryCacheEnabled && queryParameters.Cacheable; + return _factory.Settings.IsQueryCacheEnabled && queryParameters.Cacheable + && !(queryParameters.HasAutoDiscoverScalarTypes && queryParameters.ResultTransformer != null); } private IList ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)