| | | 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.Collections.Generic; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Transactions; |
| | | 10 | | |
| | | 11 | | internal class AmqpTransactionManager |
| | | 12 | | { |
| | 0 | 13 | | readonly object syncRoot = new object(); |
| | 0 | 14 | | readonly Dictionary<string, AmqpTransactionEnlistment> enlistmentMap = new Dictionary<string, AmqpTransactionEnl |
| | | 15 | | |
| | 0 | 16 | | public static AmqpTransactionManager Instance { get; } = new AmqpTransactionManager(); |
| | | 17 | | |
| | | 18 | | public async Task<ArraySegment<byte>> EnlistAsync( |
| | | 19 | | Transaction transaction, |
| | | 20 | | ServiceBusConnection serviceBusConnection) |
| | | 21 | | { |
| | 0 | 22 | | if (transaction.IsolationLevel != IsolationLevel.Serializable) |
| | | 23 | | { |
| | 0 | 24 | | throw new InvalidOperationException($"The only supported IsolationLevel is {nameof(IsolationLevel.Serial |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | string transactionId = transaction.TransactionInformation.LocalIdentifier; |
| | | 28 | | AmqpTransactionEnlistment transactionEnlistment; |
| | | 29 | | |
| | 0 | 30 | | lock (this.syncRoot) |
| | | 31 | | { |
| | 0 | 32 | | if (!this.enlistmentMap.TryGetValue(transactionId, out transactionEnlistment)) |
| | | 33 | | { |
| | 0 | 34 | | transactionEnlistment = new AmqpTransactionEnlistment(transaction, this, serviceBusConnection); |
| | 0 | 35 | | this.enlistmentMap.Add(transactionId, transactionEnlistment); |
| | | 36 | | |
| | 0 | 37 | | if (!transaction.EnlistPromotableSinglePhase(transactionEnlistment)) |
| | | 38 | | { |
| | 0 | 39 | | this.enlistmentMap.Remove(transactionId); |
| | 0 | 40 | | throw new InvalidOperationException("Local transactions are not supported with other resource ma |
| | | 41 | | } |
| | | 42 | | } |
| | 0 | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | transactionEnlistment = await transactionEnlistment.GetOrCreateAsync(serviceBusConnection.OperationTimeout). |
| | 0 | 46 | | return transactionEnlistment.AmqpTransactionId; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | public void RemoveEnlistment(string transactionId) |
| | | 50 | | { |
| | 0 | 51 | | lock (this.syncRoot) |
| | | 52 | | { |
| | 0 | 53 | | this.enlistmentMap.Remove(transactionId); |
| | 0 | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | } |
| | | 57 | | } |