Skip to content

Commit 7a36b0f

Browse files
committed
Move cache check from AbstractEntityPersister.IsTransient to ForeignKeys.IsTransientSlow
1 parent 151194f commit 7a36b0f

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;
@@ -211,25 +214,37 @@ public static bool IsNotTransientSlow(string entityName, object entity, ISession
211214
/// </remarks>
212215
public static bool IsTransientSlow(string entityName, object entity, ISessionImplementor session)
213216
{
214-
return IsTransientFast(entityName, entity, session) ??
215-
HasDbSnapshot(entityName, entity, session);
217+
bool? isTransient = IsTransientFast(entityName, entity, session);
218+
if (isTransient.HasValue)
219+
return isTransient.Value;
220+
221+
var persister = session.GetEntityPersister(entityName, entity);
222+
var id = persister.GetIdentifier(entity);
223+
224+
// check to see if it is in the second-level cache
225+
if (persister.HasCache && session.CacheMode.HasFlag(CacheMode.Get))
226+
{
227+
var ck = session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
228+
if (persister.Cache.Get(ck, session.Timestamp) != null)
229+
return false;
230+
}
231+
232+
return HasDbSnapshot(persister, id, session);
216233
}
217234

218-
static bool HasDbSnapshot(string entityName, object entity, ISessionImplementor session)
235+
static bool HasDbSnapshot(IEntityPersister persister, object identifier, ISessionImplementor session)
219236
{
220-
IEntityPersister persister = session.GetEntityPersister(entityName, entity);
221237
if (persister.IdentifierGenerator is Assigned)
222238
{
223239
// When using assigned identifiers we cannot tell if an entity
224240
// is transient or detached without querying the database.
225241
// This could potentially cause Select N+1 in cascaded saves, so warn the user.
226242
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.",
227-
entity, persister.GetIdentifier(entity));
243+
persister.EntityName, identifier);
228244
}
229245

230246
// hit the database, after checking the session cache for a snapshot
231-
System.Object[] snapshot =
232-
session.PersistenceContext.GetDatabaseSnapshot(persister.GetIdentifier(entity), persister);
247+
System.Object[] snapshot = session.PersistenceContext.GetDatabaseSnapshot(identifier, persister);
233248
return snapshot == null;
234249
}
235250

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)