Skip to content

Commit b46afe6

Browse files
committed
Move cache check from AbstractEntityPersister.IsTransient to ForeignKeys.IsTransientSlow
1 parent ad676cc commit b46afe6

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/NHibernate/Engine/ForeignKeys.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
using System;
3+
using System.Threading;
4+
using System.Threading.Tasks;
25
using NHibernate.Id;
36
using NHibernate.Persister.Entity;
47
using NHibernate.Proxy;
@@ -192,25 +195,37 @@ public static bool IsNotTransientSlow(string entityName, object entity, ISession
192195
/// </remarks>
193196
public static bool IsTransientSlow(string entityName, object entity, ISessionImplementor session)
194197
{
195-
return IsTransientFast(entityName, entity, session) ??
196-
HasDbSnapshot(entityName, entity, session);
198+
bool? isTransient = IsTransientFast(entityName, entity, session);
199+
if (isTransient.HasValue)
200+
return isTransient.Value;
201+
202+
var persister = session.GetEntityPersister(entityName, entity);
203+
var id = persister.GetIdentifier(entity);
204+
205+
// check to see if it is in the second-level cache
206+
if (persister.HasCache && session.CacheMode.HasFlag(CacheMode.Get))
207+
{
208+
var ck = session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
209+
if (persister.Cache.Get(ck, session.Timestamp) != null)
210+
return false;
211+
}
212+
213+
return HasDbSnapshot(persister, id, session);
197214
}
198215

199-
static bool HasDbSnapshot(string entityName, object entity, ISessionImplementor session)
216+
static bool HasDbSnapshot(IEntityPersister persister, object identifier, ISessionImplementor session)
200217
{
201-
IEntityPersister persister = session.GetEntityPersister(entityName, entity);
202218
if (persister.IdentifierGenerator is Assigned)
203219
{
204220
// When using assigned identifiers we cannot tell if an entity
205221
// is transient or detached without querying the database.
206222
// This could potentially cause Select N+1 in cascaded saves, so warn the user.
207223
log.Warn("Unable to determine if {0} with assigned identifier {1} is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.",
208-
entity, persister.GetIdentifier(entity));
224+
persister.EntityName, identifier);
209225
}
210226

211227
// hit the database, after checking the session cache for a snapshot
212-
System.Object[] snapshot =
213-
session.PersistenceContext.GetDatabaseSnapshot(persister.GetIdentifier(entity), persister);
228+
System.Object[] snapshot = session.PersistenceContext.GetDatabaseSnapshot(identifier, persister);
214229
return snapshot == null;
215230
}
216231

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,14 +4230,7 @@ public virtual void AfterReassociate(object entity, ISessionImplementor session)
42304230
}
42314231
}
42324232

4233-
// check to see if it is in the second-level cache
4234-
if (HasCache && session.CacheMode.HasFlag(CacheMode.Get))
4235-
{
4236-
CacheKey ck = session.GenerateCacheKey(id, IdentifierType, RootEntityName);
4237-
if (Cache.Get(ck, session.Timestamp) != null)
4238-
return false;
4239-
}
4240-
4233+
//Note: second level cache check is moved to ForeignKeys.IsTransientSlow as a potentially slow operation
42414234
return null;
42424235
}
42434236

0 commit comments

Comments
 (0)