Example #1
0
 internal void Rollback()
 {
     try
     {
         if (_state == TransactionState.NoTxn || _state == TransactionState.RolledBack)
         {
             throw new InvalidOperationException("Transaction is not active");
         }
         if (_state == TransactionState.RollingBack)
         {
             _state = TransactionState.RolledBack;
             return;
         }
         CheckThread();
         try
         {
             var request = TransactionRollbackCodec.EncodeRequest(_txnId, _threadId);
             Invoke(request);
         }
         catch
         {
             // ignored
         }
         _state = TransactionState.RolledBack;
     }
     finally
     {
         _threadFlag = null;
     }
 }
Example #2
0
        /// <inheritdoc />
        public async Task RollbackAsync()
        {
            if (State == TransactionState.RollingBack)
            {
                State = TransactionState.RolledBack;
                return;
            }

            if (State != TransactionState.Active)
            {
                throw new InvalidOperationException("There is no active transaction to roll back.");
            }

            if (_threadId != ContextId)
            {
                HConsole.WriteLine(this, $"Rollback transaction on context #{ContextId} that was started on #{_threadId}");
                throw new InvalidOperationException("Transactions cannot span multiple async contexts.");
            }

            HConsole.WriteLine(this, $"Rollback transaction on context #{ContextId}");

            try
            {
                var requestMessage  = TransactionRollbackCodec.EncodeRequest(TransactionId, ContextId);
                var responseMessage = await _cluster.Messaging.SendToMemberAsync(requestMessage, _connection).CfAwait();

                _     = TransactionRollbackCodec.DecodeResponse(responseMessage);
                State = TransactionState.RolledBack;
            }
            catch
            {
                // TODO: that is the original code - weird
                State = TransactionState.RolledBack;
            }
            finally
            {
                lock (_inTransactionMutex) AsyncContext.Current.InTransaction = false;
            }
        }