| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Amqp |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using System.Transactions; |
| | 9 | | using Microsoft.Azure.Amqp; |
| | 10 | | using Microsoft.Azure.ServiceBus.Primitives; |
| | 11 | |
|
| | 12 | | class AmqpTransactionEnlistment : Singleton<AmqpTransactionEnlistment>, IPromotableSinglePhaseNotification |
| | 13 | | { |
| | 14 | | readonly string transactionId; |
| | 15 | | readonly AmqpTransactionManager transactionManager; |
| | 16 | | readonly ServiceBusConnection serviceBusConnection; |
| | 17 | |
|
| 0 | 18 | | public AmqpTransactionEnlistment( |
| 0 | 19 | | Transaction transaction, |
| 0 | 20 | | AmqpTransactionManager transactionManager, |
| 0 | 21 | | ServiceBusConnection serviceBusConnection) |
| | 22 | | { |
| 0 | 23 | | this.transactionId = transaction.TransactionInformation.LocalIdentifier; |
| 0 | 24 | | this.transactionManager = transactionManager; |
| 0 | 25 | | this.serviceBusConnection = serviceBusConnection; |
| 0 | 26 | | } |
| | 27 | |
|
| 0 | 28 | | public ArraySegment<byte> AmqpTransactionId { get; private set; } |
| | 29 | |
|
| | 30 | | protected override async Task<AmqpTransactionEnlistment> OnCreateAsync(TimeSpan timeout) |
| | 31 | | { |
| | 32 | | try |
| | 33 | | { |
| 0 | 34 | | var faultTolerantController = this.serviceBusConnection.TransactionController; |
| 0 | 35 | | var controller = await faultTolerantController.GetOrCreateAsync(this.serviceBusConnection.OperationTimeo |
| 0 | 36 | | this.AmqpTransactionId = await controller.DeclareAsync().ConfigureAwait(false); |
| 0 | 37 | | MessagingEventSource.Log.AmqpTransactionDeclared(this.transactionId, this.AmqpTransactionId); |
| 0 | 38 | | return this; |
| | 39 | | } |
| 0 | 40 | | catch (Exception exception) |
| | 41 | | { |
| 0 | 42 | | MessagingEventSource.Log.AmqpTransactionInitializeException(this.transactionId, exception); |
| 0 | 43 | | this.transactionManager.RemoveEnlistment(this.transactionId); |
| 0 | 44 | | throw; |
| | 45 | | } |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | protected override void OnSafeClose(AmqpTransactionEnlistment value) |
| | 49 | | { |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | void IPromotableSinglePhaseNotification.Initialize() |
| | 53 | | { |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | void IPromotableSinglePhaseNotification.SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 57 | | { |
| 0 | 58 | | this.transactionManager.RemoveEnlistment(this.transactionId); |
| 0 | 59 | | TaskExtensionHelper.Schedule(() => this.SinglePhaseCommitAsync(singlePhaseEnlistment)); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | async Task SinglePhaseCommitAsync(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 63 | | { |
| | 64 | | try |
| | 65 | | { |
| 0 | 66 | | var faultTolerantController = this.serviceBusConnection.TransactionController; |
| 0 | 67 | | var controller = await faultTolerantController.GetOrCreateAsync(this.serviceBusConnection.OperationTimeo |
| | 68 | |
|
| 0 | 69 | | await controller.DischargeAsync(this.AmqpTransactionId, fail: false).ConfigureAwait(false); |
| 0 | 70 | | singlePhaseEnlistment.Committed(); |
| 0 | 71 | | MessagingEventSource.Log.AmqpTransactionDischarged(this.transactionId, this.AmqpTransactionId, false); |
| 0 | 72 | | await this.CloseAsync().ConfigureAwait(false); |
| 0 | 73 | | } |
| | 74 | | catch (Exception e) |
| | 75 | | { |
| 0 | 76 | | Exception exception = AmqpExceptionHelper.GetClientException(e, null); |
| 0 | 77 | | MessagingEventSource.Log.AmqpTransactionDischargeException(this.transactionId, this.AmqpTransactionId, e |
| 0 | 78 | | singlePhaseEnlistment.InDoubt(exception); |
| 0 | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | void IPromotableSinglePhaseNotification.Rollback(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 83 | | { |
| 0 | 84 | | this.transactionManager.RemoveEnlistment(this.transactionId); |
| 0 | 85 | | TaskExtensionHelper.Schedule(() => this.RollbackAsync(singlePhaseEnlistment)); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | async Task RollbackAsync(SinglePhaseEnlistment singlePhaseEnlistment) |
| | 89 | | { |
| | 90 | | try |
| | 91 | | { |
| 0 | 92 | | var faultTolerantController = this.serviceBusConnection.TransactionController; |
| 0 | 93 | | var controller = await faultTolerantController.GetOrCreateAsync(this.serviceBusConnection.OperationTimeo |
| | 94 | |
|
| 0 | 95 | | await controller.DischargeAsync(this.AmqpTransactionId, fail: true).ConfigureAwait(false); |
| 0 | 96 | | singlePhaseEnlistment.Aborted(); |
| 0 | 97 | | MessagingEventSource.Log.AmqpTransactionDischarged(this.transactionId, this.AmqpTransactionId, true); |
| 0 | 98 | | } |
| | 99 | | catch (Exception e) |
| | 100 | | { |
| 0 | 101 | | Exception exception = AmqpExceptionHelper.GetClientException(e, null); |
| 0 | 102 | | MessagingEventSource.Log.AmqpTransactionDischargeException(this.transactionId, this.AmqpTransactionId, e |
| 0 | 103 | | singlePhaseEnlistment.Aborted(exception); |
| 0 | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | byte[] ITransactionPromoter.Promote() |
| | 108 | | { |
| 0 | 109 | | throw new TransactionPromotionException("Local transactions are not supported with other resource managers/D |
| | 110 | | } |
| | 111 | | } |
| | 112 | | } |