Skip to content

Commit a4cbae9

Browse files
igiturhazzik
authored andcommitted
NH-2241 - IStatelessSession is accidentally 2LC enabled in some cases (#652)
1 parent 281fa80 commit a4cbae9

File tree

12 files changed

+102
-14
lines changed

12 files changed

+102
-14
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH2241
4+
{
5+
[TestFixture]
6+
public class Fixture : BugTestCase
7+
{
8+
protected override void OnSetUp()
9+
{
10+
using (var session = OpenSession())
11+
using (var tran = session.BeginTransaction())
12+
{
13+
var country = new Country {CountryCode = "SE", CountryName = "Sweden"};
14+
session.Save(country);
15+
tran.Commit();
16+
}
17+
}
18+
19+
protected override void OnTearDown()
20+
{
21+
using (var session = OpenSession())
22+
using (var tran = session.BeginTransaction())
23+
{
24+
session.Delete("from Country");
25+
session.Delete("from User");
26+
tran.Commit();
27+
}
28+
}
29+
30+
[Test]
31+
public void CanInsertUsingStatelessEvenWhenAssociatedEntityHasCacheStategy()
32+
{
33+
using (var ss = Sfi.OpenStatelessSession())
34+
using (var tx = ss.BeginTransaction())
35+
{
36+
var user = new User();
37+
user.Country = new Country {CountryCode = "SE", CountryName = "Sweden"};
38+
39+
ss.Insert(user);
40+
tx.Commit();
41+
}
42+
}
43+
}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH2241">
5+
6+
<class name="User" table="Users">
7+
<id name="Id">
8+
<generator class="native"/>
9+
</id>
10+
11+
<many-to-one name="Country" column="CountryCode" cascade="none" />
12+
</class>
13+
14+
<class name="Country">
15+
<cache region="ShortTerm" usage="read-write" />
16+
17+
<id name="CountryCode">
18+
<generator class="assigned"/>
19+
</id>
20+
21+
<property name="CountryName" />
22+
23+
</class>
24+
25+
</hibernate-mapping>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH2241
2+
{
3+
public class User
4+
{
5+
public virtual int Id { get; set; }
6+
7+
public virtual Country Country { get; set; }
8+
}
9+
10+
public class Country
11+
{
12+
public virtual string CountryCode { get; set; }
13+
14+
public virtual string CountryName { get; set; }
15+
}
16+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,8 @@
760760
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Fixture.cs" />
761761
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\FooExample.cs" />
762762
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
763+
<Compile Include="NHSpecificTest\NH2241\Fixture.cs" />
764+
<Compile Include="NHSpecificTest\NH2241\Model.cs" />
763765
<Compile Include="Insertordering\NH3931Entities.cs" />
764766
<Compile Include="NHSpecificTest\NH4004\Entity.cs" />
765767
<Compile Include="NHSpecificTest\NH4004\Fixture.cs" />
@@ -3284,6 +3286,7 @@
32843286
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
32853287
</ItemGroup>
32863288
<ItemGroup>
3289+
<EmbeddedResource Include="NHSpecificTest\NH2241\Mappings.hbm.xml" />
32873290
<EmbeddedResource Include="Futures\Mappings.hbm.xml" />
32883291
<EmbeddedResource Include="SessionBuilder\Mappings.hbm.xml" />
32893292
<EmbeddedResource Include="IdTest\IdentityClass.hbm.xml" />

src/NHibernate/Engine/BatchFetchQueue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSiz
236236

237237
private bool IsCached(EntityKey entityKey, IEntityPersister persister)
238238
{
239-
if (persister.HasCache)
239+
if (persister.HasCache && context.Session.CacheMode.HasFlag(CacheMode.Get))
240240
{
241241
CacheKey key = context.Session.GenerateCacheKey(entityKey.Identifier, persister.IdentifierType, entityKey.EntityName);
242242
return persister.Cache.Cache.Get(key) != null;
@@ -246,12 +246,12 @@ private bool IsCached(EntityKey entityKey, IEntityPersister persister)
246246

247247
private bool IsCached(object collectionKey, ICollectionPersister persister)
248248
{
249-
if (persister.HasCache)
249+
if (persister.HasCache && context.Session.CacheMode.HasFlag(CacheMode.Get))
250250
{
251251
CacheKey cacheKey = context.Session.GenerateCacheKey(collectionKey, persister.KeyType, persister.Role);
252252
return persister.Cache.Cache.Get(cacheKey) != null;
253253
}
254254
return false;
255255
}
256256
}
257-
}
257+
}

src/NHibernate/Engine/Loading/CollectionLoadContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void EndLoadingCollection(LoadingCollectionEntry lce, ICollectionPersist
260260
}
261261

262262
bool addToCache = hasNoQueuedAdds && persister.HasCache &&
263-
((session.CacheMode & CacheMode.Put) == CacheMode.Put) && !ce.IsDoremove; // and this is not a forced initialization during flush
263+
session.CacheMode.HasFlag(CacheMode.Put) && !ce.IsDoremove; // and this is not a forced initialization during flush
264264

265265
if (addToCache)
266266
{

src/NHibernate/Engine/TwoPhaseLoad.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static void InitializeEntity(object entity, bool readOnly, ISessionImplem
9898

9999
ISessionFactoryImplementor factory = session.Factory;
100100

101-
if (persister.HasCache && ((session.CacheMode & CacheMode.Put) == CacheMode.Put))
101+
if (persister.HasCache && session.CacheMode.HasFlag(CacheMode.Put))
102102
{
103103
if (log.IsDebugEnabled)
104104
log.Debug("adding entity to second-level cache: " + MessageHelper.InfoString(persister, id, session.Factory));

src/NHibernate/Event/Default/DefaultInitializeCollectionEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private bool InitializeCollectionFromCache(object id, ICollectionPersister persi
7070
return false;
7171
}
7272

73-
bool useCache = persister.HasCache && ((source.CacheMode & CacheMode.Get) == CacheMode.Get);
73+
bool useCache = persister.HasCache && source.CacheMode.HasFlag(CacheMode.Get);
7474

7575
if (!useCache)
7676
{

src/NHibernate/Event/Default/DefaultLoadEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ protected virtual object LoadFromSessionCache(LoadEvent @event, EntityKey keyToL
413413
protected virtual object LoadFromSecondLevelCache(LoadEvent @event, IEntityPersister persister, LoadType options)
414414
{
415415
ISessionImplementor source = @event.Session;
416-
bool useCache = persister.HasCache && ((source.CacheMode & CacheMode.Get) == CacheMode.Get)
416+
bool useCache = persister.HasCache && source.CacheMode .HasFlag(CacheMode.Get)
417417
&& @event.LockMode.LessThan(LockMode.Read);
418418

419419
if (useCache)

src/NHibernate/Impl/MultiCriteriaImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private IList ListUsingQueryCache()
137137
log.Debug("Cache miss for multi criteria query");
138138
IList list = DoList();
139139
result = list;
140-
if ((session.CacheMode & CacheMode.Put) == CacheMode.Put)
140+
if (session.CacheMode.HasFlag(CacheMode.Put))
141141
{
142142
bool put = queryCache.Put(key, new ICacheAssembler[] { assembler }, new object[] { list }, combinedParameters.NaturalKeyLookup, session);
143143
if (put && factory.Statistics.IsStatisticsEnabled)
@@ -476,4 +476,4 @@ private void ThrowIfKeyAlreadyExists(string key)
476476
throw new InvalidOperationException(String.Format("The key '{0}' already exists", key));
477477
}
478478
}
479-
}
479+
}

src/NHibernate/Loader/Loader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ private IList GetResultFromQueryCache(ISessionImplementor session, QueryParamete
15821582
{
15831583
IList result = null;
15841584

1585-
if ((!queryParameters.ForceCacheRefresh) && (session.CacheMode & CacheMode.Get) == CacheMode.Get)
1585+
if (!queryParameters.ForceCacheRefresh && session.CacheMode.HasFlag(CacheMode.Get))
15861586
{
15871587
IPersistenceContext persistenceContext = session.PersistenceContext;
15881588

@@ -1620,7 +1620,7 @@ private IList GetResultFromQueryCache(ISessionImplementor session, QueryParamete
16201620
private void PutResultInQueryCache(ISessionImplementor session, QueryParameters queryParameters, IType[] resultTypes,
16211621
IQueryCache queryCache, QueryKey key, IList result)
16221622
{
1623-
if ((session.CacheMode & CacheMode.Put) == CacheMode.Put)
1623+
if (session.CacheMode.HasFlag(CacheMode.Put))
16241624
{
16251625
bool put = queryCache.Put(key, key.ResultTransformer.GetCachedResultTypes(resultTypes), result, queryParameters.NaturalKeyLookup, session);
16261626
if (put && _factory.Statistics.IsStatisticsEnabled)
@@ -1858,4 +1858,4 @@ protected bool TryGetLimitString(Dialect.Dialect dialect, SqlString queryString,
18581858

18591859
#endregion
18601860
}
1861-
}
1861+
}

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ public virtual object InitializeLazyProperty(string fieldName, object entity, IS
12451245
MessageHelper.InfoString(this, id, Factory), fieldName));
12461246
}
12471247

1248-
if (HasCache)
1248+
if (HasCache && session.CacheMode.HasFlag(CacheMode.Get))
12491249
{
12501250
CacheKey cacheKey = session.GenerateCacheKey(id, IdentifierType, EntityName);
12511251
object ce = Cache.Get(cacheKey, session.Timestamp);
@@ -3818,7 +3818,7 @@ public virtual void AfterReassociate(object entity, ISessionImplementor session)
38183818
}
38193819

38203820
// check to see if it is in the second-level cache
3821-
if (HasCache)
3821+
if (HasCache && session.CacheMode.HasFlag(CacheMode.Get))
38223822
{
38233823
CacheKey ck = session.GenerateCacheKey(id, IdentifierType, RootEntityName);
38243824
if (Cache.Get(ck, session.Timestamp) != null)

0 commit comments

Comments
 (0)