Skip to content

NH-2241 - IStatelessSession is accidentally 2LC enabled in some cases #652

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 2 commits into from
Jun 24, 2017
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
44 changes: 44 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2241/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH2241
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var country = new Country {CountryCode = "SE", CountryName = "Sweden"};
session.Save(country);
tran.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Delete("from Country");
session.Delete("from User");
tran.Commit();
}
}

[Test]
public void CanInsertUsingStatelessEvenWhenAssociatedEntityHasCacheStategy()
{
using (var ss = Sfi.OpenStatelessSession())
using (var tx = ss.BeginTransaction())
{
var user = new User();
user.Country = new Country {CountryCode = "SE", CountryName = "Sweden"};

ss.Insert(user);
tx.Commit();
}
}
}
}
25 changes: 25 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2241/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH2241">

<class name="User" table="Users">
<id name="Id">
<generator class="native"/>
</id>

<many-to-one name="Country" column="CountryCode" cascade="none" />
</class>

<class name="Country">
<cache region="ShortTerm" usage="read-write" />

<id name="CountryCode">
<generator class="assigned"/>
</id>

<property name="CountryName" />

</class>

</hibernate-mapping>
16 changes: 16 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2241/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace NHibernate.Test.NHSpecificTest.NH2241
{
public class User
{
public virtual int Id { get; set; }

public virtual Country Country { get; set; }
}

public class Country
{
public virtual string CountryCode { get; set; }

public virtual string CountryName { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,8 @@
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Fixture.cs" />
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\FooExample.cs" />
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
<Compile Include="NHSpecificTest\NH2241\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2241\Model.cs" />
<Compile Include="Insertordering\NH3931Entities.cs" />
<Compile Include="NHSpecificTest\NH4004\Entity.cs" />
<Compile Include="NHSpecificTest\NH4004\Fixture.cs" />
Expand Down Expand Up @@ -3284,6 +3286,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH2241\Mappings.hbm.xml" />
<EmbeddedResource Include="Futures\Mappings.hbm.xml" />
<EmbeddedResource Include="SessionBuilder\Mappings.hbm.xml" />
<EmbeddedResource Include="IdTest\IdentityClass.hbm.xml" />
Expand Down
6 changes: 3 additions & 3 deletions src/NHibernate/Engine/BatchFetchQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSiz

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

private bool IsCached(object collectionKey, ICollectionPersister persister)
{
if (persister.HasCache)
if (persister.HasCache && context.Session.CacheMode.HasFlag(CacheMode.Get))
{
CacheKey cacheKey = context.Session.GenerateCacheKey(collectionKey, persister.KeyType, persister.Role);
return persister.Cache.Cache.Get(cacheKey) != null;
}
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Engine/Loading/CollectionLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void EndLoadingCollection(LoadingCollectionEntry lce, ICollectionPersist
}

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

if (addToCache)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Engine/TwoPhaseLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static void InitializeEntity(object entity, bool readOnly, ISessionImplem

ISessionFactoryImplementor factory = session.Factory;

if (persister.HasCache && ((session.CacheMode & CacheMode.Put) == CacheMode.Put))
if (persister.HasCache && session.CacheMode.HasFlag(CacheMode.Put))
{
if (log.IsDebugEnabled)
log.Debug("adding entity to second-level cache: " + MessageHelper.InfoString(persister, id, session.Factory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private bool InitializeCollectionFromCache(object id, ICollectionPersister persi
return false;
}

bool useCache = persister.HasCache && ((source.CacheMode & CacheMode.Get) == CacheMode.Get);
bool useCache = persister.HasCache && source.CacheMode.HasFlag(CacheMode.Get);

if (!useCache)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Event/Default/DefaultLoadEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ protected virtual object LoadFromSessionCache(LoadEvent @event, EntityKey keyToL
protected virtual object LoadFromSecondLevelCache(LoadEvent @event, IEntityPersister persister, LoadType options)
{
ISessionImplementor source = @event.Session;
bool useCache = persister.HasCache && ((source.CacheMode & CacheMode.Get) == CacheMode.Get)
bool useCache = persister.HasCache && source.CacheMode .HasFlag(CacheMode.Get)
&& @event.LockMode.LessThan(LockMode.Read);

if (useCache)
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Impl/MultiCriteriaImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private IList ListUsingQueryCache()
log.Debug("Cache miss for multi criteria query");
IList list = DoList();
result = list;
if ((session.CacheMode & CacheMode.Put) == CacheMode.Put)
if (session.CacheMode.HasFlag(CacheMode.Put))
{
bool put = queryCache.Put(key, new ICacheAssembler[] { assembler }, new object[] { list }, combinedParameters.NaturalKeyLookup, session);
if (put && factory.Statistics.IsStatisticsEnabled)
Expand Down Expand Up @@ -476,4 +476,4 @@ private void ThrowIfKeyAlreadyExists(string key)
throw new InvalidOperationException(String.Format("The key '{0}' already exists", key));
}
}
}
}
6 changes: 3 additions & 3 deletions src/NHibernate/Loader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ private IList GetResultFromQueryCache(ISessionImplementor session, QueryParamete
{
IList result = null;

if ((!queryParameters.ForceCacheRefresh) && (session.CacheMode & CacheMode.Get) == CacheMode.Get)
if (!queryParameters.ForceCacheRefresh && session.CacheMode.HasFlag(CacheMode.Get))
{
IPersistenceContext persistenceContext = session.PersistenceContext;

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

#endregion
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ public virtual object InitializeLazyProperty(string fieldName, object entity, IS
MessageHelper.InfoString(this, id, Factory), fieldName));
}

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

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