| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using System.Transactions; |
| | 7 | | using Microsoft.Azure.Amqp; |
| | 8 | | using Microsoft.Azure.Amqp.Transaction; |
| | 9 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 10 | |
|
| | 11 | | namespace 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 | |
|
| 0 | 20 | | public AmqpTransactionEnlistment( |
| 0 | 21 | | Transaction transaction, |
| 0 | 22 | | AmqpTransactionManager transactionManager, |
| 0 | 23 | | AmqpConnectionScope connectionScope, |
| 0 | 24 | | TimeSpan timeout) |
| | 25 | | { |
| 0 | 26 | | _transactionId = transaction.TransactionInformation.LocalIdentifier; |
| 0 | 27 | | _transactionManager = transactionManager; |
| 0 | 28 | | _connectionScope = connectionScope; |
| 0 | 29 | | _timeout = timeout; |
| 0 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | public ArraySegment<byte> AmqpTransactionId { get; private set; } |
| | 33 | |
|
| | 34 | | protected override async Task<AmqpTransactionEnlistment> OnCreateAsync(TimeSpan timeout) |
| | 35 | | { |
| | 36 | | try |
| | 37 | | { |
| 0 | 38 | | FaultTolerantAmqpObject<Controller> faultTolerantController = _connectionScope.TransactionController; |
| 0 | 39 | | Controller controller = await faultTolerantController.GetOrCreateAsync(timeout).ConfigureAwait(false); |
| 0 | 40 | | AmqpTransactionId = await controller.DeclareAsync().ConfigureAwait(false); |
| 0 | 41 | | ServiceBusEventSource.Log.TransactionDeclared(_transactionId, AmqpTransactionId); |
| 0 | 42 | | return this; |
| | 43 | | } |
| 0 | 44 | | catch (Exception exception) |
| | 45 | | { |
| 0 | 46 | | ServiceBusEventSource.Log.TransactionInitializeException(_transactionId, exception.ToString()); |
| 0 | 47 | | _transactionManager.RemoveEnlistment(_transactionId); |
| 0 | 48 | | throw; |
| | 49 | | } |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | protected override void OnSafeClose(AmqpTransactionEnlistment value) |
| | 53 | | { |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | void IPromotableSinglePhaseNotification.Initialize() |
| | 57 | | { |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | void IPromotableSinglePhaseNotification.SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 61 | | { |
| 0 | 62 | | _transactionManager.RemoveEnlistment(_transactionId); |
| 0 | 63 | | _ = SinglePhaseCommitAsync(singlePhaseEnlistment); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private async Task SinglePhaseCommitAsync(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 67 | | { |
| | 68 | | try |
| | 69 | | { |
| 0 | 70 | | FaultTolerantAmqpObject<Controller> faultTolerantController = _connectionScope.TransactionController; |
| 0 | 71 | | Controller controller = await faultTolerantController.GetOrCreateAsync(_timeout) |
| 0 | 72 | | .ConfigureAwait(false); |
| | 73 | |
|
| 0 | 74 | | await controller.DischargeAsync(AmqpTransactionId, fail: false).ConfigureAwait(false); |
| 0 | 75 | | singlePhaseEnlistment.Committed(); |
| 0 | 76 | | ServiceBusEventSource.Log.TransactionDischarged( |
| 0 | 77 | | _transactionId, |
| 0 | 78 | | AmqpTransactionId, |
| 0 | 79 | | false); |
| 0 | 80 | | await CloseAsync().ConfigureAwait(false); |
| 0 | 81 | | } |
| | 82 | | catch (Exception e) |
| | 83 | | { |
| 0 | 84 | | Exception exception = AmqpExceptionHelper.TranslateException(e, null); |
| 0 | 85 | | ServiceBusEventSource.Log.TransactionDischargeException( |
| 0 | 86 | | _transactionId, |
| 0 | 87 | | AmqpTransactionId, |
| 0 | 88 | | exception); |
| 0 | 89 | | singlePhaseEnlistment.InDoubt(exception); |
| 0 | 90 | | } |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | void IPromotableSinglePhaseNotification.Rollback(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 94 | | { |
| 0 | 95 | | _transactionManager.RemoveEnlistment(_transactionId); |
| 0 | 96 | | _ = RollbackAsync(singlePhaseEnlistment); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | private async Task RollbackAsync(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 100 | | { |
| | 101 | | try |
| | 102 | | { |
| 0 | 103 | | FaultTolerantAmqpObject<Controller> faultTolerantController = _connectionScope.TransactionController; |
| 0 | 104 | | Controller controller = await faultTolerantController.GetOrCreateAsync(_timeout) |
| 0 | 105 | | .ConfigureAwait(false); |
| | 106 | |
|
| 0 | 107 | | await controller.DischargeAsync(AmqpTransactionId, fail: true).ConfigureAwait(false); |
| 0 | 108 | | singlePhaseEnlistment.Aborted(); |
| 0 | 109 | | ServiceBusEventSource.Log.TransactionDischarged(_transactionId, AmqpTransactionId, true); |
| 0 | 110 | | } |
| | 111 | | catch (Exception e) |
| | 112 | | { |
| 0 | 113 | | Exception exception = AmqpExceptionHelper.TranslateException(e, null); |
| 0 | 114 | | ServiceBusEventSource.Log.TransactionDischargeException( |
| 0 | 115 | | _transactionId, |
| 0 | 116 | | AmqpTransactionId, |
| 0 | 117 | | exception); |
| 0 | 118 | | singlePhaseEnlistment.Aborted(exception); |
| 0 | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | byte[] ITransactionPromoter.Promote() |
| | 123 | | { |
| 0 | 124 | | throw new TransactionPromotionException( |
| 0 | 125 | | "Local transactions are not supported with other resource managers/DTC."); |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |