예제 #1
0
        public async Task <ArraySegment <byte> > EnlistAsync(
            Transaction transaction,
            ServiceBusConnection serviceBusConnection)
        {
            if (transaction.IsolationLevel != IsolationLevel.Serializable)
            {
                throw new InvalidOperationException($"The only supported IsolationLevel is {nameof(IsolationLevel.Serializable)}");
            }

            string transactionId = transaction.TransactionInformation.LocalIdentifier;
            bool   fresh         = false;
            AmqpTransactionEnlistment transactionEnlistment = this.enlistmentMap.GetOrAdd(transactionId, txId =>
            {
                fresh = true;
                return(new AmqpTransactionEnlistment(transaction, this, serviceBusConnection));
            });

            if (fresh && !transaction.EnlistPromotableSinglePhase(transactionEnlistment))
            {
                this.enlistmentMap.TryRemove(transactionId, out var _);
                throw new InvalidOperationException("Local transactions are not supported with other resource managers/DTC.");
            }

            transactionEnlistment = await transactionEnlistment.GetOrCreateAsync(serviceBusConnection.OperationTimeout).ConfigureAwait(false);

            return(transactionEnlistment.AmqpTransactionId);
        }
예제 #2
0
        static async Task KeepBusy(CancellationToken token, IAmqpTransactionManager manager)
        {
            var random = new Random(Interlocked.Increment(ref seed));

            await Task.Delay(random.Next(10, 100)).ConfigureAwait(false);

            while (!token.IsCancellationRequested)
            {
                var tx = new Transaction();
                var serviceBusConnection = new ServiceBusConnection();
                await Task.WhenAll(manager.EnlistAsync(tx, serviceBusConnection), manager.EnlistAsync(tx, serviceBusConnection), manager.EnlistAsync(tx, serviceBusConnection));

                manager.RemoveEnlistment(tx.TransactionInformation.LocalIdentifier);
            }
        }
예제 #3
0
 public AmqpTransactionEnlistment(Transaction transaction, IAmqpTransactionManager amqpTransactionManager, ServiceBusConnection serviceBusConnection)
 {
     this.transaction            = transaction;
     this.amqpTransactionManager = amqpTransactionManager;
     this.serviceBusConnection   = serviceBusConnection;
 }