Skip to content

Commit 900566c

Browse files
Merge branch '5.1.x' into Release5.1.3
2 parents 0d56f3b + 193bfb9 commit 900566c

File tree

8 files changed

+184
-18
lines changed

8 files changed

+184
-18
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Cfg;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.GH1730
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class FixtureAsync : BugTestCase
19+
{
20+
protected override void Configure(Configuration configuration)
21+
{
22+
cfg.SetProperty(Environment.GenerateStatistics, "true");
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
session.CreateQuery("delete from Entity").ExecuteUpdate();
31+
32+
transaction.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public async Task HitCacheInSameSessionAsync()
38+
{
39+
await (Sfi.EvictQueriesAsync());
40+
Sfi.Statistics.Clear();
41+
var entities = new System.Collections.Generic.List<Entity>();
42+
43+
using (var session = OpenSession())
44+
{
45+
using (var transaction = session.BeginTransaction())
46+
{
47+
for (int i = 0; i < 3; i++)
48+
{
49+
var e = new Entity { Name = "Name" + i };
50+
entities.Add(e);
51+
await (session.SaveAsync(e));
52+
}
53+
await (transaction.CommitAsync());
54+
}
55+
56+
var queryString = "from Entity";
57+
58+
using (var tx = session.BeginTransaction())
59+
{
60+
// this query will hit the database and create the cache
61+
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
62+
await (tx.CommitAsync());
63+
}
64+
65+
using (var transaction = session.BeginTransaction())
66+
{
67+
//and this one SHOULD served by the cache
68+
await (session.CreateQuery(queryString).SetCacheable(true).ListAsync());
69+
await (transaction.CommitAsync());
70+
}
71+
72+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
73+
Assert.AreEqual(1, qs.CacheHitCount);
74+
Assert.AreEqual(1, qs.CachePutCount);
75+
}
76+
}
77+
}
78+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1730
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using NHibernate.Cfg;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1730
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
protected override void Configure(Configuration configuration)
10+
{
11+
cfg.SetProperty(Environment.GenerateStatistics, "true");
12+
}
13+
14+
protected override void OnTearDown()
15+
{
16+
using (var session = OpenSession())
17+
using (var transaction = session.BeginTransaction())
18+
{
19+
session.CreateQuery("delete from Entity").ExecuteUpdate();
20+
21+
transaction.Commit();
22+
}
23+
}
24+
25+
[Test]
26+
public void HitCacheInSameSession()
27+
{
28+
Sfi.EvictQueries();
29+
Sfi.Statistics.Clear();
30+
var entities = new System.Collections.Generic.List<Entity>();
31+
32+
using (var session = OpenSession())
33+
{
34+
using (var transaction = session.BeginTransaction())
35+
{
36+
for (int i = 0; i < 3; i++)
37+
{
38+
var e = new Entity { Name = "Name" + i };
39+
entities.Add(e);
40+
session.Save(e);
41+
}
42+
transaction.Commit();
43+
}
44+
45+
var queryString = "from Entity";
46+
47+
using (var tx = session.BeginTransaction())
48+
{
49+
// this query will hit the database and create the cache
50+
session.CreateQuery(queryString).SetCacheable(true).List();
51+
tx.Commit();
52+
}
53+
54+
using (var transaction = session.BeginTransaction())
55+
{
56+
//and this one SHOULD served by the cache
57+
session.CreateQuery(queryString).SetCacheable(true).List();
58+
transaction.Commit();
59+
}
60+
61+
var qs = Sfi.Statistics.GetQueryStatistics(queryString);
62+
Assert.AreEqual(1, qs.CacheHitCount);
63+
Assert.AreEqual(1, qs.CachePutCount);
64+
}
65+
}
66+
}
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH1730">
4+
5+
<class name="Entity">
6+
<id name="Id" generator="guid.comb"/>
7+
<property name="Name"/>
8+
</class>
9+
10+
</hibernate-mapping>

src/NHibernate/Async/Cache/StandardQueryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task<bool> PutAsync(QueryKey key, ICacheAssembler[] returnTypes, IL
4141
if (isNaturalKeyLookup && result.Count == 0)
4242
return false;
4343

44-
long ts = session.Timestamp;
44+
long ts = session.Factory.Settings.CacheProvider.NextTimestamp();
4545

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

src/NHibernate/Async/Loader/Loader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using System;
1212
using System.Collections;
13+
using System.Collections.Concurrent;
1314
using System.Collections.Generic;
1415
using System.Data;
1516
using System.Data.Common;

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public bool Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, bool
6060
if (isNaturalKeyLookup && result.Count == 0)
6161
return false;
6262

63-
long ts = session.Timestamp;
63+
long ts = session.Factory.Settings.CacheProvider.NextTimestamp();
6464

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

src/NHibernate/Loader/Loader.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using System.Data;
56
using System.Data.Common;
@@ -31,17 +32,21 @@ namespace NHibernate.Loader
3132
/// Abstract superclass of object loading (and querying) strategies.
3233
/// </summary>
3334
/// <remarks>
34-
/// <p>
35+
/// <para>
3536
/// This class implements useful common functionality that concrete loaders would delegate to.
3637
/// It is not intended that this functionality would be directly accessed by client code (Hence,
3738
/// all methods of this class are declared <c>protected</c> or <c>private</c>.) This class relies heavily upon the
38-
/// <see cref="ILoadable" /> interface, which is the contract between this class and
39+
/// <see cref="ILoadable" /> interface, which is the contract between this class and
3940
/// <see cref="IEntityPersister" />s that may be loaded by it.
40-
/// </p>
41-
/// <p>
42-
/// The present implementation is able to load any number of columns of entities and at most
41+
/// </para>
42+
/// <para>
43+
/// The present implementation is able to load any number of columns of entities and at most
4344
/// one collection role per query.
44-
/// </p>
45+
/// </para>
46+
/// <para>
47+
/// All this class members are thread safe. Entity and collection loaders are held in persisters shared among
48+
/// sessions built from the same session factory. They must be thread safe.
49+
/// </para>
4550
/// </remarks>
4651
/// <seealso cref="NHibernate.Persister.Entity.ILoadable"/>
4752
public abstract partial class Loader
@@ -63,8 +68,8 @@ public abstract partial class Loader
6368
/// <summary>
6469
/// Caches subclass entity aliases for given persister index in <see cref="EntityPersisters"/> and subclass entity name
6570
/// </summary>
66-
private readonly Dictionary<Tuple<int, string>, string[][]> _subclassEntityAliasesMap = new Dictionary<Tuple<int, string>, string[][]>();
67-
71+
private readonly ConcurrentDictionary<Tuple<int, string>, string[][]> _subclassEntityAliasesMap = new ConcurrentDictionary<Tuple<int, string>, string[][]>();
72+
6873
protected Loader(ISessionFactoryImplementor factory)
6974
{
7075
_factory = factory;
@@ -1103,14 +1108,9 @@ private void LoadFromResultSet(DbDataReader rs, int i, object obj, string instan
11031108
private string[][] GetSubclassEntityAliases(int i, ILoadable persister)
11041109
{
11051110
var cacheKey = System.Tuple.Create(i, persister.EntityName);
1106-
if (_subclassEntityAliasesMap.TryGetValue(cacheKey, out string[][] cols))
1107-
{
1108-
return cols;
1109-
}
1110-
1111-
cols = EntityAliases[i].GetSuffixedPropertyAliases(persister);
1112-
_subclassEntityAliasesMap[cacheKey] = cols;
1113-
return cols;
1111+
return _subclassEntityAliasesMap.GetOrAdd(
1112+
cacheKey,
1113+
k => EntityAliases[i].GetSuffixedPropertyAliases(persister));
11141114
}
11151115

11161116
/// <summary>

0 commit comments

Comments
 (0)