Beispiel #1
0
        /// <summary>
        /// Submits the changes and returns the number of affected Objects. Note: only IDataObjects are counted.
        /// </summary>
        /// <returns>Number of affected Objects</returns>
        public override int SubmitChanges()
        {
            CheckDisposed();
            int result = 0;
            var ticks  = _perfCounter.IncrementSubmitChanges();

            try
            {
                DebugTraceChangedObjects();

                var notifySaveList = _ctx.ObjectStateManager
                                     .GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted)
                                     .Select(e => e.Entity)
                                     .OfType <IDataObject>()
                                     .ToList();

                NotifyChanging(notifySaveList);

                try
                {
                    result = _ctx.SaveChanges();
                    Logging.Log.InfoFormat("[{0}] changes submitted.", result);
                }
                catch (OptimisticConcurrencyException cex)
                {
                    Logging.Log.Error("OptimisticConcurrencyException during SubmitChanges", cex);
                    throw new ConcurrencyException(cex);
                }
                catch (UpdateException updex)
                {
                    Logging.Log.Error("UpdateException during SubmitChanges", updex);
                    if (updex.InnerException == null)
                    {
                        throw;
                    }
                    throw updex.InnerException;
                }

                NotifyChanged(notifySaveList);

                UpdateObjectState();
            }
            finally
            {
                _perfCounter.DecrementSubmitChanges(result, ticks);
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Submits the changes and returns the number of affected Objects. Note: only IDataObjects are counted.
        /// </summary>
        /// <returns>Number of affected Objects</returns>
        public int SubmitChanges()
        {
            CheckDisposed();
            int result = 0;
            var ticks  = _perfCounter.IncrementSubmitChanges();

            try
            {
                // TODO: Add a better Cache Refresh Strategie
                // CacheController<IDataObject>.Current.Clear();

                if (_submitChangesHandler == null)
                {
                    _submitChangesHandler = new SubmitChangesHandler();
                }
                result = _submitChangesHandler.ExchangeObjects(this);
            }
            finally
            {
                _perfCounter.DecrementSubmitChanges(result, ticks);
            }
            return(result);
        }
Beispiel #3
0
        private void FlushSession(List <NHibernatePersistenceObject> notifySaveList)
        {
            var  ticks             = _perfCounter.IncrementSubmitChanges();
            bool isItMyTransaction = !IsTransactionRunning;

            if (isItMyTransaction)
            {
                BeginTransaction();
            }
            try
            {
                foreach (var obj in RelationTopoSort(notifySaveList.Where(obj => obj.ObjectState == DataObjectState.Deleted)))
                {
                    _attachedObjects.Remove(obj);
                    _attachedObjectsByProxy.Remove(obj);

                    _nhSession.Delete(obj.NHibernateProxy);
                }

                var saveOrUpdateList = notifySaveList.Where(obj => obj.ObjectState != DataObjectState.Deleted).ToList();
                // remove objects from internal caches as the hashkey is about to change
                foreach (var obj in saveOrUpdateList)
                {
                    _attachedObjects.Remove(obj);
                    _attachedObjectsByProxy.Remove(obj);
                }

                // this will change IDs all over the place, depending on NHibernate's opinions on efficient updating
                foreach (var obj in saveOrUpdateList)
                {
                    obj.SaveOrUpdateTo(_nhSession);
                }

                // force outstanding changes into DB
                _nhSession.Flush();

                foreach (var obj in saveOrUpdateList)
                {
                    _attachedObjects.Add(obj);
                    _attachedObjectsByProxy.Add(obj);
                }

                if (isItMyTransaction)
                {
                    CommitTransaction();
                }
                Logging.Log.InfoFormat("[{0}] changes submitted.", notifySaveList.Count);
            }
            catch (StaleObjectStateException ex)
            {
                if (isItMyTransaction)
                {
                    RollbackTransaction();
                }
                var error = string.Format("Failed saving transaction: Concurrent modification on {0}#{1}", ex.EntityName, ex.Identifier);
                throw new ConcurrencyException(error, ex);
            }
            catch (Exception ex)
            {
                if (isItMyTransaction)
                {
                    RollbackTransaction();
                }
                Logging.Log.Error("Failed saving transaction", ex);
                throw;
            }
            finally
            {
                _perfCounter.DecrementSubmitChanges(notifySaveList.Count, ticks);
            }
        }