|
1 | 1 |
|
| 2 | +using System; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
2 | 5 | using NHibernate.Id; |
3 | 6 | using NHibernate.Persister.Entity; |
4 | 7 | using NHibernate.Proxy; |
@@ -211,25 +214,37 @@ public static bool IsNotTransientSlow(string entityName, object entity, ISession |
211 | 214 | /// </remarks> |
212 | 215 | public static bool IsTransientSlow(string entityName, object entity, ISessionImplementor session) |
213 | 216 | { |
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); |
216 | 233 | } |
217 | 234 |
|
218 | | - static bool HasDbSnapshot(string entityName, object entity, ISessionImplementor session) |
| 235 | + static bool HasDbSnapshot(IEntityPersister persister, object identifier, ISessionImplementor session) |
219 | 236 | { |
220 | | - IEntityPersister persister = session.GetEntityPersister(entityName, entity); |
221 | 237 | if (persister.IdentifierGenerator is Assigned) |
222 | 238 | { |
223 | 239 | // When using assigned identifiers we cannot tell if an entity |
224 | 240 | // is transient or detached without querying the database. |
225 | 241 | // This could potentially cause Select N+1 in cascaded saves, so warn the user. |
226 | 242 | 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); |
228 | 244 | } |
229 | 245 |
|
230 | 246 | // 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); |
233 | 248 | return snapshot == null; |
234 | 249 | } |
235 | 250 |
|
|
0 commit comments