Esempio n. 1
0
        /// <inheritdoc />
        public async Task CommitAsync()
        {
            if (State != TransactionState.Active)
            {
                throw new InvalidOperationException("There is no active transaction to commit.");
            }

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

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

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

                _     = TransactionCommitCodec.DecodeResponse(responseMessage);
                State = TransactionState.Committed;
            }
            catch
            {
                State = TransactionState.RollingBack;
                throw;
            }
            finally
            {
                lock (_inTransactionMutex) AsyncContext.Current.InTransaction = false;
            }
        }
Esempio n. 2
0
 internal void Commit(bool prepareAndCommit)
 {
     try
     {
         if (_state != TransactionState.Active)
         {
             throw new TransactionNotActiveException("Transaction is not active");
         }
         CheckThread();
         CheckTimeout();
         var request = TransactionCommitCodec.EncodeRequest(_txnId, _threadId);
         Invoke(request);
         _state = TransactionState.Committed;
     }
     catch (Exception e)
     {
         _state = TransactionState.RollingBack;
         throw ExceptionUtil.Rethrow(e);
     }
     finally
     {
         _threadFlag = null;
     }
 }