< Summary

Class:Azure.Messaging.ServiceBus.Amqp.AmqpTransactionEnlistment
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\AmqpTransactionEnlistment.cs
Covered lines:0
Uncovered lines:65
Coverable lines:65
Total lines:128
Line coverage:0% (0 of 65)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
get_AmqpTransactionId()-0%100%
OnCreateAsync()-0%100%
OnSafeClose(...)-0%100%
System.Transactions.IPromotableSinglePhaseNotification.Initialize()-0%100%
System.Transactions.IPromotableSinglePhaseNotification.SinglePhaseCommit(...)-0%100%
SinglePhaseCommitAsync()-0%100%
System.Transactions.IPromotableSinglePhaseNotification.Rollback(...)-0%100%
RollbackAsync()-0%100%
System.Transactions.ITransactionPromoter.Promote()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\AmqpTransactionEnlistment.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading.Tasks;
 6using System.Transactions;
 7using Microsoft.Azure.Amqp;
 8using Microsoft.Azure.Amqp.Transaction;
 9using Azure.Messaging.ServiceBus.Diagnostics;
 10
 11namespace Azure.Messaging.ServiceBus.Amqp
 12{
 13    internal class AmqpTransactionEnlistment : Singleton<AmqpTransactionEnlistment>, IPromotableSinglePhaseNotification
 14    {
 15        private readonly string _transactionId;
 16        private readonly AmqpTransactionManager _transactionManager;
 17        private readonly AmqpConnectionScope _connectionScope;
 18        private readonly TimeSpan _timeout;
 19
 020        public AmqpTransactionEnlistment(
 021            Transaction transaction,
 022            AmqpTransactionManager transactionManager,
 023            AmqpConnectionScope connectionScope,
 024            TimeSpan timeout)
 25        {
 026            _transactionId = transaction.TransactionInformation.LocalIdentifier;
 027            _transactionManager = transactionManager;
 028            _connectionScope = connectionScope;
 029            _timeout = timeout;
 030        }
 31
 032        public ArraySegment<byte> AmqpTransactionId { get; private set; }
 33
 34        protected override async Task<AmqpTransactionEnlistment> OnCreateAsync(TimeSpan timeout)
 35        {
 36            try
 37            {
 038                FaultTolerantAmqpObject<Controller> faultTolerantController = _connectionScope.TransactionController;
 039                Controller controller = await faultTolerantController.GetOrCreateAsync(timeout).ConfigureAwait(false);
 040                AmqpTransactionId = await controller.DeclareAsync().ConfigureAwait(false);
 041                ServiceBusEventSource.Log.TransactionDeclared(_transactionId, AmqpTransactionId);
 042                return this;
 43            }
 044            catch (Exception exception)
 45            {
 046                ServiceBusEventSource.Log.TransactionInitializeException(_transactionId, exception.ToString());
 047                _transactionManager.RemoveEnlistment(_transactionId);
 048                throw;
 49            }
 050        }
 51
 52        protected override void OnSafeClose(AmqpTransactionEnlistment value)
 53        {
 054        }
 55
 56        void IPromotableSinglePhaseNotification.Initialize()
 57        {
 058        }
 59
 60        void IPromotableSinglePhaseNotification.SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
 61        {
 062            _transactionManager.RemoveEnlistment(_transactionId);
 063            _ = SinglePhaseCommitAsync(singlePhaseEnlistment);
 064        }
 65
 66        private async Task SinglePhaseCommitAsync(SinglePhaseEnlistment singlePhaseEnlistment)
 67        {
 68            try
 69            {
 070                FaultTolerantAmqpObject<Controller> faultTolerantController = _connectionScope.TransactionController;
 071                Controller controller = await faultTolerantController.GetOrCreateAsync(_timeout)
 072                    .ConfigureAwait(false);
 73
 074                await controller.DischargeAsync(AmqpTransactionId, fail: false).ConfigureAwait(false);
 075                singlePhaseEnlistment.Committed();
 076                ServiceBusEventSource.Log.TransactionDischarged(
 077                    _transactionId,
 078                    AmqpTransactionId,
 079                    false);
 080                await CloseAsync().ConfigureAwait(false);
 081            }
 82            catch (Exception e)
 83            {
 084                Exception exception = AmqpExceptionHelper.TranslateException(e, null);
 085                ServiceBusEventSource.Log.TransactionDischargeException(
 086                    _transactionId,
 087                    AmqpTransactionId,
 088                    exception);
 089                singlePhaseEnlistment.InDoubt(exception);
 090            }
 091        }
 92
 93        void IPromotableSinglePhaseNotification.Rollback(SinglePhaseEnlistment singlePhaseEnlistment)
 94        {
 095            _transactionManager.RemoveEnlistment(_transactionId);
 096            _ = RollbackAsync(singlePhaseEnlistment);
 097        }
 98
 99        private async Task RollbackAsync(SinglePhaseEnlistment singlePhaseEnlistment)
 100        {
 101            try
 102            {
 0103                FaultTolerantAmqpObject<Controller> faultTolerantController = _connectionScope.TransactionController;
 0104                Controller controller = await faultTolerantController.GetOrCreateAsync(_timeout)
 0105                    .ConfigureAwait(false);
 106
 0107                await controller.DischargeAsync(AmqpTransactionId, fail: true).ConfigureAwait(false);
 0108                singlePhaseEnlistment.Aborted();
 0109                ServiceBusEventSource.Log.TransactionDischarged(_transactionId, AmqpTransactionId, true);
 0110            }
 111            catch (Exception e)
 112            {
 0113                Exception exception = AmqpExceptionHelper.TranslateException(e, null);
 0114                ServiceBusEventSource.Log.TransactionDischargeException(
 0115                    _transactionId,
 0116                    AmqpTransactionId,
 0117                    exception);
 0118                singlePhaseEnlistment.Aborted(exception);
 0119            }
 0120        }
 121
 122        byte[] ITransactionPromoter.Promote()
 123        {
 0124            throw new TransactionPromotionException(
 0125                "Local transactions are not supported with other resource managers/DTC.");
 126        }
 127    }
 128}