| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using System.Transactions; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 10 | | { |
| | 11 | | internal class AmqpTransactionManager |
| | 12 | | { |
| 0 | 13 | | private readonly object _syncRoot = new object(); |
| 0 | 14 | | private readonly Dictionary<string, AmqpTransactionEnlistment> _enlistmentMap = new Dictionary<string, AmqpTrans |
| | 15 | |
|
| 0 | 16 | | public static AmqpTransactionManager Instance { get; } = new AmqpTransactionManager(); |
| | 17 | |
|
| | 18 | | public async Task<ArraySegment<byte>> EnlistAsync( |
| | 19 | | Transaction transaction, |
| | 20 | | AmqpConnectionScope connectionScope, |
| | 21 | | TimeSpan timeout) |
| | 22 | | { |
| 0 | 23 | | if (transaction.IsolationLevel != IsolationLevel.Serializable) |
| | 24 | | { |
| 0 | 25 | | throw new InvalidOperationException($"The only supported IsolationLevel is {nameof(IsolationLevel.Serial |
| | 26 | | } |
| | 27 | |
|
| 0 | 28 | | string transactionId = transaction.TransactionInformation.LocalIdentifier; |
| | 29 | | AmqpTransactionEnlistment transactionEnlistment; |
| | 30 | |
|
| 0 | 31 | | lock (_syncRoot) |
| | 32 | | { |
| 0 | 33 | | if (!_enlistmentMap.TryGetValue(transactionId, out transactionEnlistment)) |
| | 34 | | { |
| 0 | 35 | | transactionEnlistment = new AmqpTransactionEnlistment( |
| 0 | 36 | | transaction, |
| 0 | 37 | | this, |
| 0 | 38 | | connectionScope, |
| 0 | 39 | | timeout); |
| 0 | 40 | | _enlistmentMap.Add(transactionId, transactionEnlistment); |
| | 41 | |
|
| 0 | 42 | | if (!transaction.EnlistPromotableSinglePhase(transactionEnlistment)) |
| | 43 | | { |
| 0 | 44 | | _enlistmentMap.Remove(transactionId); |
| 0 | 45 | | throw new InvalidOperationException("Local transactions are not supported with other resource ma |
| | 46 | | } |
| | 47 | | } |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | transactionEnlistment = await transactionEnlistment.GetOrCreateAsync(timeout).ConfigureAwait(false); |
| 0 | 51 | | return transactionEnlistment.AmqpTransactionId; |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public void RemoveEnlistment(string transactionId) |
| | 55 | | { |
| 0 | 56 | | lock (_syncRoot) |
| | 57 | | { |
| 0 | 58 | | _enlistmentMap.Remove(transactionId); |
| 0 | 59 | | } |
| 0 | 60 | | } |
| | 61 | | } |
| | 62 | | } |