Skip to content

Query cache always missed in session having altered the entities #1731

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 1 commit into from
Jun 6, 2018
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
78 changes: 78 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1730/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using NHibernate.Cfg;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1730
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void Configure(Configuration configuration)
{
cfg.SetProperty(Environment.GenerateStatistics, "true");
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Entity").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task HitCacheInSameSessionAsync()
{
await (Sfi.EvictQueriesAsync());
Sfi.Statistics.Clear();
var entities = new System.Collections.Generic.List<Entity>();

using (var session = OpenSession())
{
using (var transaction = session.BeginTransaction())
{
for (int i = 0; i < 3; i++)
{
var e = new Entity { Name = "Name" + i };
entities.Add(e);
await (session.SaveAsync(e));
}
await (transaction.CommitAsync());
}

var queryString = "from Entity";

using (var tx = session.BeginTransaction())
{
// this query will hit the database and create the cache
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
await (tx.CommitAsync());
}

using (var transaction = session.BeginTransaction())
{
//and this one SHOULD served by the cache
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
await (transaction.CommitAsync());
}

var qs = Sfi.Statistics.GetQueryStatistics(queryString);
Assert.AreEqual(1, qs.CacheHitCount);
Assert.AreEqual(1, qs.CachePutCount);
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1730/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1730
{
class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
67 changes: 67 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1730/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using NHibernate.Cfg;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1730
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void Configure(Configuration configuration)
{
cfg.SetProperty(Environment.GenerateStatistics, "true");
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Entity").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void HitCacheInSameSession()
{
Sfi.EvictQueries();
Sfi.Statistics.Clear();
var entities = new System.Collections.Generic.List<Entity>();

using (var session = OpenSession())
{
using (var transaction = session.BeginTransaction())
{
for (int i = 0; i < 3; i++)
{
var e = new Entity { Name = "Name" + i };
entities.Add(e);
session.Save(e);
}
transaction.Commit();
}

var queryString = "from Entity";

using (var tx = session.BeginTransaction())
{
// this query will hit the database and create the cache
session.CreateQuery(queryString).SetCacheable(true).List();
tx.Commit();
}

using (var transaction = session.BeginTransaction())
{
//and this one SHOULD served by the cache
session.CreateQuery(queryString).SetCacheable(true).List();
transaction.Commit();
}

var qs = Sfi.Statistics.GetQueryStatistics(queryString);
Assert.AreEqual(1, qs.CacheHitCount);
Assert.AreEqual(1, qs.CachePutCount);
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1730/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1730">

<class name="Entity">
<id name="Id" generator="guid.comb"/>
<property name="Name"/>
</class>

</hibernate-mapping>
2 changes: 1 addition & 1 deletion src/NHibernate/Async/Cache/StandardQueryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<bool> PutAsync(QueryKey key, ICacheAssembler[] returnTypes, IL
if (isNaturalKeyLookup && result.Count == 0)
return false;

long ts = session.Timestamp;
long ts = session.Factory.Settings.CacheProvider.NextTimestamp();

if (Log.IsDebugEnabled())
Log.Debug("caching query results in region: '{0}'; {1}", _regionName, key);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Cache/StandardQueryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public bool Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, bool
if (isNaturalKeyLookup && result.Count == 0)
return false;

long ts = session.Timestamp;
long ts = session.Factory.Settings.CacheProvider.NextTimestamp();

if (Log.IsDebugEnabled())
Log.Debug("caching query results in region: '{0}'; {1}", _regionName, key);
Expand Down