Ejemplo n.º 1
0
 public void Rollback()
 {
     try
     {
         _tx?.Rollback();
     }
     finally
     {
         if (_tx != null)
         {
             _tx.Dispose();
             _tx = null;
         }
     }
 }
Ejemplo n.º 2
0
 public void RollbackTransaction()
 {
     try
     {
         _currentTransaction?.Rollback();
     }
     finally
     {
         if (_currentTransaction != null)
         {
             _currentTransaction.Dispose();
             _currentTransaction = null;
         }
     }
 }
Ejemplo n.º 3
0
 public void RollbackTransaction()
 {
     try
     {
         _dbContextTransaction?.Rollback();
     }
     finally
     {
         if (_dbContextTransaction != null)
         {
             _dbContextTransaction.Dispose();
             _dbContextTransaction = null;
         }
     }
 }
Ejemplo n.º 4
0
 public void RollbackTransaction()
 {
     try
     {
         transaction?.Rollback();
     }
     finally
     {
         if (transaction != null)
         {
             transaction.Dispose();
             transaction = null;
         }
     }
 }
Ejemplo n.º 5
0
 private void RollBackTransaction(IDbContextTransaction transaction)
 {
     try
     {
         transaction?.Rollback();
     }
     catch (Exception e)
     {
         logger.LogDebug(CoreMessages.RollbackFailed + Environment.NewLine + e.Message);
     }
     try
     {
         transaction?.Dispose();
     }
     catch {}
 }
Ejemplo n.º 6
0
 public void RollbackTransaction()
 {
     try
     {
         _currentTransaction?.Rollback();
         _logger.LogWarning($"Rollback transaction id: {_currentTransaction.TransactionId}.");
     }
     finally
     {
         if (_currentTransaction != null)
         {
             _currentTransaction.Dispose();
             _currentTransaction = null;
         }
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 回滚所有事务
        /// </summary>
        public virtual void Rollback()
        {
            dbContextTransaction?.Rollback();

            if (dbContext.IsRelationalTransaction())
            {
                CleanChanges(dbContext);
                if (dbContext.Database.CurrentTransaction != null)
                {
                    dbContext.Database.CurrentTransaction.Rollback();
                    dbContext.Database.CurrentTransaction.Dispose();
                }
            }
            dbContext.Database.RollbackTransaction();

            HasCommitted = true;
        }
Ejemplo n.º 8
0
        public Task RollbackTransactionAsync()
        {
            try
            {
                _currentTransaction?.Rollback();
            }
            finally
            {
                if (_currentTransaction is not null)
                {
                    _currentTransaction.Dispose();
                    _currentTransaction = null;
                }
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 9
0
        public int Execute(
            IEnumerable <ModificationCommandBatch> commandBatches,
            IRelationalConnection connection)
        {
            var registrosAfetados = 0;

            connection.Open();
            IDbContextTransaction startedTransaction = null;

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


                foreach (var commandbatch in commandBatches)
                {
                    commandbatch.Execute(connection);
                    registrosAfetados += commandbatch.ModificationCommands.Count;
                }
                startedTransaction?.Commit();
                startedTransaction?.Dispose();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                try
                {
                    startedTransaction?.Rollback();
                    startedTransaction?.Dispose();
                }
                catch
                {
                    // if the connection was lost, rollback command will fail.  prefer to throw original exception in that case
                }
                throw;
            }
            finally
            {
                connection.Close();
            }

            return(registrosAfetados);
        }
Ejemplo n.º 10
0
        public async Task RollbackAsync(CancellationToken cancellationToken)
        {
            if (_transaction != null)
            {
                await _transactionSempahore.WaitAsync(cancellationToken);

                try
                {
                    _transaction?.Rollback();
                    _transaction = null;
                }
                finally
                {
                    _transactionSempahore.Release();
                }
            }
        }
        public int Execute(IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection)
        {
            var recordAffecteds = 0;

            if (connection?.DbConnection?.State != System.Data.ConnectionState.Open)
            {
                connection.Open();
            }

            IDbContextTransaction currentTransaction = null;

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

                foreach (var commandbatch in commandBatches)
                {
                    commandbatch.Execute(connection);
                    recordAffecteds += commandbatch.ModificationCommands.Count;
                }
                currentTransaction?.Commit();
                currentTransaction?.Dispose();
            }
            catch (Exception ex)
            {
                try
                {
                    currentTransaction?.Rollback();
                    currentTransaction?.Dispose();
                }
                catch
                {
                    //
                }
                throw ex;
            }
            finally
            {
                connection?.Close();
            }
            return(recordAffecteds);
        }
Ejemplo n.º 12
0
 public void Commit(IDbContextTransaction?transaction)
 {
     try
     {
         Context.SaveChanges();
         transaction?.Commit();
     }
     catch (ValidationException ex)
     {
         var errors = FormatError(ex);
         throw new Exception(errors, ex);
     }
     catch
     {
         transaction?.Rollback();
         throw;
     }
 }
Ejemplo n.º 13
0
 public void Commit()
 {
     try
     {
         if (_transaction == null)
         {
             DbContext.SaveChanges();
         }
         else
         {
             _transaction.Commit();
         }
     }
     catch (Exception ex)
     {
         _transaction?.Rollback();
     }
 }
Ejemplo n.º 14
0
 ///<summary>
 /// 回滚事物
 /// </summary>
 public void RollbackTransaction()
 {
     try
     {
         _currentTransation?.Rollback();
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         if (_currentTransation != null)
         {
             _currentTransation.Dispose();
             _currentTransation = null;
         }
     }
 }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
 public void Rollback()
 {
     if (_context == null)
     {
         throw new ArgumentNullException(nameof(_context));
     }
     try
     {
         _dbContextTransaction?.Rollback();
     }
     catch
     {
         if (_dbContextTransaction != null)
         {
             _dbContextTransaction.Dispose();
             _dbContextTransaction = null;
         }
     }
 }
Ejemplo n.º 17
0
        private static void ExecuteSql(DbContext context, string sqlName, IDbContextTransaction t, string[] sqlParts)
        {
            foreach (var part in sqlParts)
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(part))
                    {
                        continue;
                    }

                    context.Database.ExecuteSqlRaw(part);
                }
                catch (Exception ex)
                {
                    t?.Rollback();
                    throw new Exception(sqlName + Environment.NewLine + ex.Message, ex);
                }
            }
        }
Ejemplo n.º 18
0
        public bool UpdateLocation(LocationViewModel obj)
        {
            bool result;
            IDbContextTransaction transaction = _context.Database.BeginTransaction();
            var location = _location.GetLocationByID(obj.ID);

            if (location != null)
            {
                try
                {
                    obj.SubCategory = _generatecode.SubCategoryLocation(obj.LocationTypeCD, obj.Floor);
                    obj.Code        = _generatecode.GenerateCode(obj.CompanyCode, obj.ApplicationCode, obj.MainCategory, obj.SubCategory, location.No);
                    if (location.Code != obj.Code)
                    {
                        location.Code = obj.Code;
                    }
                    location.Description    = obj.Description;
                    location.Name           = obj.Name;
                    location.Floor          = obj.Floor;
                    location.LocationTypeCD = obj.LocationTypeCD;
                    location.StatusCD       = obj.StatusCD;
                    location.UpdatedBy      = obj.UpdatedBy;
                    location.UpdatedDate    = DateTime.Now.Date.ToString("ddMMyyyy");

                    _context.Entry(location).State = EntityState.Modified;
                    _context.SaveChanges();
                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    result = false;
                }
            }
            else
            {
                result = false;
            }
            return(result);
        }
Ejemplo n.º 19
0
        public bool CreateLocation(LocationViewModel obj)
        {
            bool result;
            IDbContextTransaction transaction = _context.Database.BeginTransaction();

            if (obj != null)
            {
                try
                {
                    obj.Location.No   = _location.GetLastNumberLocation();
                    obj.SubCategory   = _generatecode.SubCategoryLocation(Convert.ToInt16(obj.Location.LocationTypeCD), obj.Location.Floor);
                    obj.Number        = _generatecode.Number(obj.Location.No);
                    obj.Location.Code = _generatecode.GenerateCode(obj.CompanyCode, obj.ApplicationCode, obj.MainCategory, obj.SubCategory, obj.Number);

                    Location location = new Location();
                    location.Code           = obj.Location.Code;
                    location.Description    = obj.Location.Description;
                    location.No             = obj.Number;
                    location.Name           = obj.Location.Name;
                    location.Floor          = obj.Location.Floor;
                    location.LocationTypeCD = Convert.ToInt16(obj.Location.LocationTypeCD);
                    location.StatusCD       = obj.Location.StatusCD;
                    location.CreatedBy      = obj.CreatedBy;
                    location.CreatedDate    = obj.CreatedDate;//DateTime.Now;
                    _context.Location.Add(location);
                    _context.SaveChanges();
                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    result = false;
                }
            }
            else
            {
                result = false;
            }
            return(result);
        }
Ejemplo n.º 20
0
        private void usageCompleted(DatabaseWriteUsage usage)
        {
            int usages = Interlocked.Decrement(ref currentWriteUsages);

            try
            {
                currentWriteDidWrite |= usage.PerformedWrite;
                currentWriteDidError |= usage.Errors.Any();

                if (usages == 0)
                {
                    if (currentWriteDidError)
                    {
                        rollbacks.Value++;
                        currentWriteTransaction?.Rollback();
                    }
                    else
                    {
                        commits.Value++;
                        currentWriteTransaction?.Commit();
                    }

                    if (currentWriteDidWrite || currentWriteDidError)
                    {
                        // explicitly dispose to ensure any outstanding flushes happen as soon as possible (and underlying resources are purged).
                        usage.Context.Dispose();

                        // once all writes are complete, we want to refresh thread-specific contexts to make sure they don't have stale local caches.
                        recycleThreadContexts();
                    }

                    currentWriteTransaction = null;
                    currentWriteDidWrite    = false;
                    currentWriteDidError    = false;
                }
            }
            finally
            {
                Monitor.Exit(writeLock);
            }
        }
Ejemplo n.º 21
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            if (_shouldRollback)
            {
                _transaction?.Rollback();
            }
            else
            {
                _transaction.Commit();
            }

            _transaction?.Dispose();
            _context.Dispose();

            _disposed = true;
        }
Ejemplo n.º 22
0
        public void ProcessTransaction(Action action)
        {
            if (_transaction != null)
            {
                throw new InvalidOperationException("Transaction in process");
            }

            try
            {
                using (_transaction = _context.Database.BeginTransaction())
                {
                    action();
                    _transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                _transaction.Rollback();
                throw;
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing">
        /// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release
        /// only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (_rollbackOnDispose)
            {
                _log.Warn("Rolling back Unit Of Work Transaction...");
                _transaction.Rollback();
            }
            else
            {
                SaveChanges();
                _transaction.Commit();
            }

            _transaction.Dispose();
            _disposed = true;
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Rolls back the transaction.
        /// </summary>
        /// <remarks>
        /// This is internal because it is only intended to be called by a ChildTransactionScope
        /// </remarks>
        internal void Rollback()
        {
            switch (_transactionState)
            {
            case TransactionState.Open:
                _dbContextTransaction.Rollback();
                _transactionState = TransactionState.RolledBack;
                break;

            case TransactionState.RolledBack:
                // The transaction has already been rolled back
                break;

            case TransactionState.Closed:
            case TransactionState.Commited:
                throw new InvalidOperationException("Cannot rollback the transaction because the transaction is already " + _transactionState.ToString().ToLower());

            default:
                throw new NotImplementedException("Unrecognised TransactionState: " + _transactionState);
            }
        }
Ejemplo n.º 25
0
        public int SaveChanges()
        {
            int resultOfSaveChanges = -1;

            try
            {
                resultOfSaveChanges = _context.SaveChanges();
                _transaction.Commit();
            }
            catch (Exception ex)
            {
                _transaction.Rollback();
                resultOfSaveChanges = _context.SaveChanges();
                //Hatalar Loglanabilir
            }
            finally {
                _transaction.Dispose();
                _transaction = _context.Database.BeginTransaction();
            }
            return(resultOfSaveChanges);
        }
Ejemplo n.º 26
0
        public async Task ExecuteScopeAsync(Func <IServiceProvider, Task> action)
        {
            IDbContextTransaction transaction = null;

            using (var scope = ScopeFactory.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetService <CatalogContext>();
                try
                {
                    transaction = dbContext.Database.BeginTransaction();

                    await action(scope.ServiceProvider);
                }
                catch (Exception)
                {
                    transaction?.Rollback();
                    throw;
                }
                transaction?.Commit();
            }
        }
Ejemplo n.º 27
0
 public void Dispose()
 {
     try
     {
         if (Invoke == null)
         {
             throw new Exception("Please set Transaction.Invoke!");
         }
         Invoke?.Invoke();
         _transation.Commit();
     }
     catch
     {
         _transation.Rollback();
         throw;
     }
     finally
     {
         _transation.Dispose();
     }
 }
        public void Rollback()
        {
            _transaction.Rollback();
            foreach (var entry in _context.ChangeTracker.Entries())
            {
                switch (entry.State)
                {
                case EntityState.Modified:
                    entry.State = EntityState.Unchanged;
                    break;

                case EntityState.Added:
                    entry.State = EntityState.Detached;
                    break;

                case EntityState.Deleted:
                    entry.State = EntityState.Unchanged;
                    break;
                }
            }
        }
Ejemplo n.º 29
0
        public User Add(UserIdentity userIdentity, User user)
        {
            using IDbContextTransaction transaction = _db.Database.BeginTransaction();
            try
            {
                UserIdentity addedIdentity = _db.UserIdentities.Add(userIdentity).Entity;
                _db.SaveChanges();
                user.UserId       = addedIdentity.UserId;
                user.UserIdentity = addedIdentity;
                User addedUser = _db.Users.Add(user).Entity;
                _db.SaveChanges();
                transaction.Commit();

                return(addedUser);
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
Ejemplo n.º 30
0
        public async Task <ActionResult <Questions> > PostQuestions(Questions questions)
        {
            // context.Database.Log = Console.Write;
            // maintaing transaction while inserting data
            using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.Questions.Add(questions);
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (System.Exception ex)
                {
                    transaction.Rollback();
                }
            }

            return(CreatedAtAction("GetQuestions", new { id = questions.QuestionID }, questions));
        }