Skip to content

Skip caching for auto discovery type queries with result transformer #3172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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