예제 #1
0
        public int Commit()
        {
            var affectedRecords = _context.SaveChanges();

            _transaction?.Commit();

            return(affectedRecords);
        }
예제 #2
0
 /// <inheritdoc/>
 public void CommitTransaction()
 {
     try
     {
         SaveChanges();
         transaction?.Commit();
         ReleaseCurrentTransaction();
     }
     catch
     {
         RollBackTransaction();
         throw;
     }
 }
예제 #3
0
        public int Commit()
        {
            try
            {
                var i = _context.SaveChanges();

                _transaction?.Commit();

                return(i);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #4
0
        private int Execute(Tuple <IEnumerable <ModificationCommandBatch>, IRelationalConnection> parameters)
        {
            var commandBatches = parameters.Item1;
            var connection     = parameters.Item2;
            var rowsAffected   = 0;
            IDbContextTransaction startedTransaction = null;

            try
            {
                if (connection.CurrentTransaction == null &&
                    CurrentContext.Context.Database.AutoTransactionsEnabled)
                {
                    startedTransaction = connection.BeginTransaction();
                }
                else
                {
                    connection.Open();
                }

                foreach (var commandbatch in commandBatches)
                {
                    commandbatch.Execute(connection);
                    rowsAffected += commandbatch.ModificationCommands.Count;
                }

                startedTransaction?.Commit();
            }
            finally
            {
                startedTransaction?.Dispose();
                connection.Close();
            }

            return(rowsAffected);
        }
예제 #5
0
        public virtual async Task <int> ExecuteAsync(
            IEnumerable <ModificationCommandBatch> commandBatches,
            IRelationalConnection connection,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var rowsAffected = 0;
            await connection.OpenAsync(cancellationToken);

            IDbContextTransaction startedTransaction = null;

            try
            {
                if (connection.CurrentTransaction == null)
                {
                    startedTransaction = connection.BeginTransaction();
                }

                foreach (var commandbatch in commandBatches)
                {
                    await commandbatch.ExecuteAsync(connection, cancellationToken);

                    rowsAffected += commandbatch.ModificationCommands.Count;
                }

                startedTransaction?.Commit();
            }
            finally
            {
                startedTransaction?.Dispose();
                connection.Close();
            }

            return(rowsAffected);
        }
예제 #6
0
        public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            int result;
            var timestamp = _systemClock.UtcNow;

            IDbContextTransaction transaction = null;

            if (Database.CurrentTransaction == null && Database.GetEnlistedTransaction() == null && Transaction.Current == null)
            {
                transaction = await Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
            }

            using (transaction)
            {
                result = await base.SaveChangesAsync(false, cancellationToken).ConfigureAwait(false);

                var changes = ChangeTracker.Entries()
                              .Where(_ => _.ShouldAuditChanges())
                              .Select(entry => CreateChangeEvent(timestamp, entry))
                              .ToList();

                ChangeTracker.AcceptAllChanges();

                await ChangeEvents.AddRangeAsync(changes, cancellationToken).ConfigureAwait(false);

                result += await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false);

                transaction?.Commit();
            }

            return(result);
        }
예제 #7
0
        public new int SaveChanges(IDbContextTransaction transaction = null)
        {
            var ret = base.SaveChanges();

            transaction?.Commit();
            return(ret);
        }
        public void CloseTransaction(Exception exception)
        {
            try
            {
                if (exception != null)
                {
                    _currentTransaction?.Rollback();
                    return;
                }

                SaveChanges();

                _currentTransaction?.Commit();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Exception thrown while attempting to close a transaction.");
                _currentTransaction?.Rollback();
                throw;
            }
            finally
            {
                _currentTransaction?.Dispose();
                _currentTransaction = null;
            }
        }
예제 #9
0
 public virtual void Commit()
 {
     SaveChanges();
     _transaction?.Commit();
     _transaction?.Dispose();
     _transaction = null;
 }
예제 #10
0
        public virtual int Execute(
            IEnumerable <ModificationCommandBatch> commandBatches,
            IRelationalConnection connection)
        {
            var rowsAffected = 0;

            connection.Open();
            IDbContextTransaction startedTransaction = null;

            try
            {
                if (connection.CurrentTransaction == null)
                {
                    startedTransaction = connection.BeginTransaction();
                }

                foreach (var commandbatch in commandBatches)
                {
                    commandbatch.Execute(connection);
                    rowsAffected += commandbatch.ModificationCommands.Count;
                }

                startedTransaction?.Commit();
            }
            finally
            {
                startedTransaction?.Dispose();
                connection.Close();
            }

            return(rowsAffected);
        }
예제 #11
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual async Task ExecuteNonQueryAsync(
            IEnumerable <MigrationCommand> migrationCommands,
            IRelationalConnection connection,
            CancellationToken cancellationToken = default)
        {
            Check.NotNull(migrationCommands, nameof(migrationCommands));
            Check.NotNull(connection, nameof(connection));

            var transactionScope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled);

            try
            {
                await connection.OpenAsync(cancellationToken);

                try
                {
                    IDbContextTransaction transaction = null;

                    try
                    {
                        foreach (var command in migrationCommands)
                        {
                            if (transaction == null &&
                                !command.TransactionSuppressed)
                            {
                                transaction = await connection.BeginTransactionAsync(cancellationToken);
                            }

                            if (transaction != null &&
                                command.TransactionSuppressed)
                            {
                                transaction.Commit();
                                await transaction.DisposeAsync();

                                transaction = null;
                            }

                            await command.ExecuteNonQueryAsync(connection, cancellationToken : cancellationToken);
                        }

                        transaction?.Commit();
                    }
                    finally
                    {
                        if (transaction != null)
                        {
                            await transaction.DisposeAsync();
                        }
                    }
                }
                finally
                {
                    await connection.CloseAsync();
                }
            }
            finally
            {
                await transactionScope.DisposeAsyncIfAvailable();
            }
        }
예제 #12
0
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            int rowsAffected;
            var timestamp = _systemClock.UtcNow;

            IDbContextTransaction transaction = null;

            if (Database.CurrentTransaction == null && Database.GetEnlistedTransaction() == null && Transaction.Current == null)
            {
                transaction = Database.BeginTransaction();
            }

            using (transaction)
            {
                rowsAffected = base.SaveChanges(false);

                var changes = ChangeTracker.Entries()
                              .Where(_ => _.ShouldAuditChanges())
                              .Select(entry => CreateChangeEvent(timestamp, entry))
                              .ToList();

                ChangeTracker.AcceptAllChanges();

                ChangeEvents.AddRange(changes);

                rowsAffected += base.SaveChanges(acceptAllChangesOnSuccess);

                transaction?.Commit();
            }

            return(rowsAffected);
        }
예제 #13
0
 public void CommitTransaction()
 {
     if (!transactionClosed)
     {
         transaction?.Commit();
         transactionClosed = true;
     }
 }
예제 #14
0
 public void Commit()
 {
     if (State == UnitOfWorkState.Entered)
     {
         _transaction?.Commit();
         State = UnitOfWorkState.Committed;
     }
 }
예제 #15
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual void ExecuteNonQuery(
            IEnumerable <MigrationCommand> migrationCommands,
            IRelationalConnection connection)
        {
            Check.NotNull(migrationCommands, nameof(migrationCommands));
            Check.NotNull(connection, nameof(connection));

            var userTransaction = connection.CurrentTransaction;

            if (userTransaction is not null && migrationCommands.Any(x => x.TransactionSuppressed))
            {
                throw new NotSupportedException(RelationalStrings.TransactionSuppressedMigrationInUserTransaction);
            }

            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            {
                connection.Open();

                try
                {
                    IDbContextTransaction?transaction = null;

                    try
                    {
                        foreach (var command in migrationCommands)
                        {
                            if (transaction == null &&
                                !command.TransactionSuppressed &&
                                userTransaction is null)
                            {
                                transaction = connection.BeginTransaction();
                            }

                            if (transaction != null &&
                                command.TransactionSuppressed)
                            {
                                transaction.Commit();
                                transaction.Dispose();
                                transaction = null;
                            }

                            command.ExecuteNonQuery(connection);
                        }

                        transaction?.Commit();
                    }
                    finally
                    {
                        transaction?.Dispose();
                    }
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        internal static int CommitTransaction <TEntity>(DbContext context, IEnumerable <TEntity> entities, OperationType operation) where TEntity : class
        {
            // Retrieve entity type
            IEntityType entityType    = context.Model.FindEntityType(typeof(TEntity));
            EntityInfo  entityMapping = new EntityInfo(entityType);

            // Sample random table looks like this [_VssBuildDefinition_02da3b]
            string randomTableName = $"[_{entityMapping.TableName}_{Guid.NewGuid().ToString().Substring(0, 6)}]";

            // Build staging table sql command
            string stagingTableCommand = BulkOperationHelper.BuildStagingTableCommand(entityMapping, randomTableName, operation);

            // Open connection if it's closed
            if (context.Database.GetDbConnection().State == ConnectionState.Closed)
            {
                context.Database.GetDbConnection().Open();
            }

            // If caller does not specify a transaction we create an internal transaction
            IDbContextTransaction internalTrasaction = null;

            if (context.Database.CurrentTransaction == null)
            {
                internalTrasaction = context.Database.BeginTransaction();
            }

            // No entities to commit
            if (!entities.Any())
            {
                return(0);
            }

            // Final step is to create staging table and merge data into final table
            try
            {
                // Create staging table and insert data into staging table
                BulkOperationHelper.ExecuteSqlCommandNonQuery(context, stagingTableCommand);
                BulkOperationHelper.BulkInsertToTable(entityMapping, context, entities.ToList(), randomTableName, operation);

                // Merge staging table data into final table and commit
                string mergeCommand = BulkOperationHelper.BuildMergeCommand(entityMapping, randomTableName, operation);
                int    result       = BulkOperationHelper.ExecuteSqlCommandNonQuery(context, mergeCommand);

                // Only commit internal transaction and let caller's transaction commit on their own
                internalTrasaction?.Commit();

                // Return the number of records affected
                return(result);
            }
            catch (Exception)
            {
                // If anything goes wrong, we roll back to undo the operation
                internalTrasaction?.Rollback();
                context.Database.CurrentTransaction?.GetDbTransaction().Rollback();
                throw;
            }
        }
예제 #17
0
        public int SaveChanges(IDbContextTransaction transaction = null)
        {
            var ret = base.SaveChanges();

            if (ret > 0)
            {
                transaction?.Commit();
            }
            return(ret);
        }
예제 #18
0
        public Task <int> SaveAsync()
        {
            var count = _dbContext.SaveChangesAsync();

            _transaction?.Commit();
            _transaction?.Dispose();
            _transaction = null;

            return(count);
        }
예제 #19
0
        /// <summary>
        ///     Saves changes in context (SaveChanges) and, if there is an open transaction, commits as well.
        /// </summary>
        /// <returns>True if there was any entity changed or false if there wasn't</returns>
        public bool Commit()
        {
            if (_context.SaveChanges() <= 0)
            {
                return(false);
            }

            _dbContextTransaction?.Commit();
            return(true);
        }
예제 #20
0
        private int Commit()
        {
            var result = 0;

            try
            {
                result = Context.SaveChanges();
                Transaction?.Commit();
                return(result);
            }
            catch (Exception e)
            {
                result = -1;
                Transaction?.Rollback();
                Log.Error("Context Transaction Error");
                Log.Error(e.Message);
            }
            return(result);
        }
예제 #21
0
 public void Commit()
 {
     try
     {
         _dbTransaction?.Commit();
     }
     finally
     {
         Dispose();
     }
 }
예제 #22
0
 public void CommitTransaction()
 {
     try
     {
         _currentTransaction?.Commit();
     }
     catch
     {
         RollbackTransaction();
         throw;
     }
     finally
     {
         if (_currentTransaction != null)
         {
             _currentTransaction.Dispose();
             _currentTransaction = null;
         }
     }
 }
예제 #23
0
 public void Commit()
 {
     if (_rollbackCount > 0)
     {
         _tran?.Rollback();
     }
     else
     {
         _tran?.Commit();
     }
 }
예제 #24
0
 public void Commit()
 {
     if (dbContext != null)
     {
         dbContext.SaveChanges();
         dbContextTransaction?.Commit();
     }
     else
     {
         GetDbContextFromStack <DbContext>(false).SaveChanges();
     }
 }
예제 #25
0
            /// <summary>
            /// EFCore 提交
            /// </summary>
            private int SaveChange()
            {
                int result;

                try
                {
                    result = _dbUnitOfWork.SaveChanges();
                    _dbContextTransaction?.Commit();
                }
                catch
                {
                    _dbContextTransaction?.Rollback();
                    throw;
                }
                finally
                {
                    _dbContextTransaction?.Dispose();
                    _dbUnitOfWork.Dispose();
                }
                return(result);
            }
예제 #26
0
        public void Commit()
        {
            try
            {
                _transaction?.Commit();
            }

            catch (Exception ex)
            {
                throw;
            }
        }
예제 #27
0
        public override void Commit()
        {
            if (rolledBack)
            {
                return;
            }

            db.SaveChangesAndDetach();
            transaction?.Commit();
            db.Dispose();
            isComplete = true;
        }
예제 #28
0
        public void Commit()
        {
            if (IsCommit)
            {
                return;
            }

            IsCommit = true;

            _dbContext.SaveChanges();
            _dbContextTransaction?.Commit();
        }
예제 #29
0
 /// <inheritdoc />
 public void OnActionExecuted(ActionExecutedContext context)
 {
     if (context.Exception == null || context.ExceptionHandled)
     {
         consentContext.SaveChanges();
         transaction?.Commit();
     }
     else
     {
         transaction?.Rollback();
     }
 }
예제 #30
0
        private void CommitAndSave()
        {
            try
            {
                SaveChanges();

                currentTransaction?.Commit();
            }
            catch
            {
                RollbackTransaction();
                throw;
            }
            finally
            {
                if (currentTransaction != null)
                {
                    currentTransaction.Dispose();
                    currentTransaction = null;
                }
            }
        }