Beispiel #1
0
        public void SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
        {
            if (this.disposed)
            {
                return;
            }

            try
            {
                if (this.dataAccessObjectDataContext != null)
                {
                    this.commandsContext = this.GetSqlTransactionalCommandsContext();

                    this.dataAccessObjectDataContext.Commit(this.commandsContext, false);
                    this.commandsContext.Commit();
                }

                singlePhaseEnlistment.Committed();
            }
            catch (Exception e)
            {
                ActionUtils.IgnoreExceptions(() => this.commandsContext.Rollback());

                singlePhaseEnlistment.Aborted(e);
            }
            finally
            {
                this.Dispose();
            }
        }
Beispiel #2
0
        public void Commit()
        {
            if (this.disposed)
            {
                return;
            }

            try
            {
                if (this.dataAccessObjectDataContext != null)
                {
                    this.commandsContext = this.GetSqlTransactionalCommandsContext();
                    this.dataAccessObjectDataContext.Commit(this.commandsContext, false);
                    this.commandsContext.Commit();
                }
            }
            catch (Exception e)
            {
                ActionUtils.IgnoreExceptions(() => this.commandsContext?.Rollback());

                throw new DataAccessTransactionAbortedException(e);
            }
            finally
            {
                this.Dispose();
            }
        }
 private void CommitUpdated(SqlTransactionalCommandsContext commandsContext)
 {
     foreach (var cache in this.cachesByType)
     {
         CommitUpdated(commandsContext, cache.Value);
     }
 }
Beispiel #4
0
        public virtual void Commit(SqlTransactionalCommandsContext commandsContext, bool forFlush)
        {
            foreach (var cache in this.cachesByType)
            {
                cache.Value.AssertObjectsAreReadyForCommit();
            }

            try
            {
                var context = new DataAccessModelHookSubmitContext(this, forFlush);

                ((IDataAccessModelInternal)this.DataAccessModel).OnHookBeforeSubmit(context);

                this.isCommiting = true;

                this.CommitNew(commandsContext);
                this.CommitUpdated(commandsContext);
                this.CommitDeleted(commandsContext);

                ((IDataAccessModelInternal)this.DataAccessModel).OnHookAfterSubmit(context);
            }
            finally
            {
                this.isCommiting = false;
            }

            foreach (var cache in this.cachesByType)
            {
                cache.Value.ProcessAfterCommit();
            }
        }
        public virtual void Commit(SqlTransactionalCommandsContext commandsContext, bool forFlush)
        {
            foreach (var cache in this.cachesByType)
            {
                cache.Value.AssertObjectsAreReadyForCommit();
            }

            try
            {
                this.isCommiting = true;

                this.CommitNew(commandsContext);
                this.CommitUpdated(commandsContext);
                this.CommitDeleted(commandsContext);
            }
            finally
            {
                this.isCommiting = false;
            }

            foreach (var cache in this.cachesByType)
            {
                cache.Value.ProcessAfterCommit();
            }
        }
Beispiel #6
0
        public virtual SqlTransactionalCommandsContext GetSqlTransactionalCommandsContext()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(TransactionContext));
            }

            return(this.commandsContext ?? (this.commandsContext = this.GetSqlDatabaseContext().CreateSqlTransactionalCommandsContext(this)));
        }
        private void CommitNew(SqlTransactionalCommandsContext commandsContext)
        {
            var insertResultsByType = new Dictionary <TypeAndTransactionalCommandsContext, InsertResults>();
            var fixups = new Dictionary <TypeAndTransactionalCommandsContext, IReadOnlyList <DataAccessObject> >();

            foreach (var value in this.cachesByType.Values)
            {
                CommitNewPhase1(commandsContext, value, insertResultsByType, fixups);
            }

            var currentInsertResultsByType = insertResultsByType;
            var newInsertResultsByType     = new Dictionary <TypeAndTransactionalCommandsContext, InsertResults>();

            while (true)
            {
                var didRetry = false;

                // Perform the retry list
                foreach (var i in currentInsertResultsByType)
                {
                    var type = i.Key.Type;
                    var persistenceTransactionContext = i.Key.CommandsContext;
                    var retryListForType = i.Value.ToRetry;

                    if (retryListForType.Count == 0)
                    {
                        continue;
                    }

                    didRetry = true;

                    newInsertResultsByType[new TypeAndTransactionalCommandsContext(type, persistenceTransactionContext)] = persistenceTransactionContext.Insert(type, retryListForType);
                }

                if (!didRetry)
                {
                    break;
                }

                MathUtils.Swap(ref currentInsertResultsByType, ref newInsertResultsByType);

                newInsertResultsByType.Clear();
            }

            // Perform fixups
            foreach (var i in fixups)
            {
                var type = i.Key.Type;
                var databaseTransactionContext = i.Key.CommandsContext;

                databaseTransactionContext.Update(type, i.Value);
            }
        }
        private static void CommitNewPhase1(SqlTransactionalCommandsContext commandsContext, IObjectsByIdCache cache, Dictionary <TypeAndTransactionalCommandsContext, InsertResults> insertResultsByType, Dictionary <TypeAndTransactionalCommandsContext, IReadOnlyList <DataAccessObject> > fixups)
        {
            var key = new TypeAndTransactionalCommandsContext(cache.Type, commandsContext);

            var currentInsertResults = commandsContext.Insert(cache.Type, cache.GetNewObjects());

            if (currentInsertResults.ToRetry.Count > 0)
            {
                insertResultsByType[key] = currentInsertResults;
            }

            if (currentInsertResults.ToFixUp.Count > 0)
            {
                fixups[key] = currentInsertResults.ToFixUp;
            }
        }
Beispiel #9
0
        public virtual void Prepare(PreparingEnlistment preparingEnlistment)
        {
            if (this.disposed)
            {
                return;
            }

            var dispose = true;

            try
            {
                if (this.dataAccessObjectDataContext != null)
                {
                    this.commandsContext = this.GetSqlTransactionalCommandsContext();
                    this.dataAccessObjectDataContext.Commit(this.commandsContext, false);

                    if (this.commandsContext.SqlDatabaseContext.SupportsPreparedTransactions)
                    {
                        this.commandsContext.Prepare();
                    }
                }

                preparingEnlistment.Prepared();

                dispose = false;
            }
            catch (TransactionAbortedException)
            {
                throw;
            }
            catch (Exception e)
            {
                ActionUtils.IgnoreExceptions(() => this.commandsContext?.Rollback());

                preparingEnlistment.ForceRollback(e);
            }
            finally
            {
                if (dispose)
                {
                    this.Dispose();
                }
            }
        }
 public TypeAndTransactionalCommandsContext(Type type, SqlTransactionalCommandsContext sqlTransactionalCommandsContext)
     : this()
 {
     this.Type            = type;
     this.CommandsContext = sqlTransactionalCommandsContext;
 }
 public DatabaseTransactionContextAcquisition(TransactionContext transactionContext, SqlDatabaseContext sqlDatabaseContext, SqlTransactionalCommandsContext sqlDatabaseCommandsContext)
 {
     this.TransactionContext         = transactionContext;
     this.SqlDatabaseContext         = sqlDatabaseContext;
     this.SqlDatabaseCommandsContext = sqlDatabaseCommandsContext;
 }
 private static void CommitUpdated(SqlTransactionalCommandsContext commandsContext, IObjectsByIdCache cache)
 {
     commandsContext.Update(cache.Type, cache.GetObjectsById());
     commandsContext.Update(cache.Type, cache.GetObjectsByPredicate());
 }
 private static void CommitDeleted(SqlTransactionalCommandsContext commandsContext, IObjectsByIdCache cache)
 {
     commandsContext.Delete(cache.Type, cache.GetDeletedObjects());
 }
Beispiel #14
0
 public TransactionEntry(SqlTransactionalCommandsContext value)
 {
     this.sqlDatabaseCommandsContext = value;
 }