Skip to content

Commit

Permalink
Skip caching for auto discovery type queries with result transformer (#…
Browse files Browse the repository at this point in the history
…3172)

Fixes #3169
  • Loading branch information
bahusoid authored Oct 4, 2022
1 parent ba574e9 commit 3c4d3b7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/NHibernate.Test/Async/SqlTest/Query/NativeSQLQueriesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,55 @@ Task<IList> 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<ResultDto>())
.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()
{
Expand Down
49 changes: 49 additions & 0 deletions src/NHibernate.Test/SqlTest/Query/NativeSQLQueriesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResultDto>())
.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()
{
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c4d3b7

Please sign in to comment.