Example #1
0
 /// <summary> Delete a entity. </summary>
 /// <param name="entityName">The entityName for the entity to be deleted </param>
 /// <param name="entity">a detached entity instance </param>
 /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
 public async Task DeleteAsync(string entityName, object entity, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (BeginProcess())
     {
         IEntityPersister persister = GetEntityPersister(entityName, entity);
         object           id        = persister.GetIdentifier(entity);
         object           version   = persister.GetVersion(entity);
         await(persister.DeleteAsync(id, version, entity, this, cancellationToken)).ConfigureAwait(false);
     }
 }
Example #2
0
 /// <summary> Delete a entity. </summary>
 /// <param name="entityName">The entityName for the entity to be deleted </param>
 /// <param name="entity">a detached entity instance </param>
 /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
 public async Task DeleteAsync(string entityName, object entity, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (new SessionIdLoggingContext(SessionId))
     {
         CheckAndUpdateSessionStatus();
         IEntityPersister persister = GetEntityPersister(entityName, entity);
         object           id        = persister.GetIdentifier(entity);
         object           version   = persister.GetVersion(entity);
         await(persister.DeleteAsync(id, version, entity, this, cancellationToken)).ConfigureAwait(false);
     }
 }
Example #3
0
        public override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            object              id        = Id;
            IEntityPersister    persister = Persister;
            ISessionImplementor session   = Session;
            object              instance  = Instance;

            bool      statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled;
            Stopwatch stopwatch    = null;

            if (statsEnabled)
            {
                stopwatch = Stopwatch.StartNew();
            }

            bool veto = await(PreDeleteAsync(cancellationToken)).ConfigureAwait(false);

            object tmpVersion = version;

            if (persister.IsVersionPropertyGenerated)
            {
                // we need to grab the version value from the entity, otherwise
                // we have issues with generated-version entities that may have
                // multiple actions queued during the same flush
                tmpVersion = persister.GetVersion(instance);
            }

            CacheKey ck;

            if (persister.HasCache)
            {
                ck    = session.GenerateCacheKey(id, persister.IdentifierType, persister.RootEntityName);
                sLock = await(persister.Cache.LockAsync(ck, version, cancellationToken)).ConfigureAwait(false);
            }
            else
            {
                ck = null;
            }

            if (!isCascadeDeleteEnabled && !veto)
            {
                await(persister.DeleteAsync(id, tmpVersion, instance, session, cancellationToken)).ConfigureAwait(false);
            }

            //postDelete:
            // After actually deleting a row, record the fact that the instance no longer
            // exists on the database (needed for identity-column key generation), and
            // remove it from the session cache
            IPersistenceContext persistenceContext = session.PersistenceContext;

            EntityEntry entry = persistenceContext.RemoveEntry(instance);

            if (entry == null)
            {
                throw new AssertionFailure("Possible nonthreadsafe access to session");
            }
            entry.PostDelete();

            EntityKey key = session.GenerateEntityKey(entry.Id, entry.Persister);

            persistenceContext.RemoveEntity(key);
            persistenceContext.RemoveProxy(key);

            if (persister.HasCache)
            {
                await(persister.Cache.EvictAsync(ck, cancellationToken)).ConfigureAwait(false);
            }

            await(PostDeleteAsync(cancellationToken)).ConfigureAwait(false);

            if (statsEnabled && !veto)
            {
                stopwatch.Stop();
                Session.Factory.StatisticsImplementor.DeleteEntity(Persister.EntityName, stopwatch.Elapsed);
            }
        }