你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ITransaction 接口

定义

作为单个逻辑工作单元执行的操作序列。

public interface ITransaction : IDisposable
type ITransaction = interface
    interface IDisposable
Public Interface ITransaction
Implements IDisposable
实现

注解

事务必须显示以下 ACID 属性。 (请参阅:https://technet.microsoft.com/en-us/library/ms190612)

  • 原子性 - 事务必须是原子工作单元;要么执行其所有数据修改,要么不执行任何修改。
  • 一致性 - 完成后,事务必须使所有数据保持一致状态。 事务结束时,所有内部数据结构必须都正确。
  • 隔离 - 并发事务所做的修改必须与任何其他并发事务所做的修改隔离开来。 用于 中的操作的 ITransaction 隔离级别由 IReliableState 执行操作的 确定。
  • 持续性 - 事务完成后,其效果在系统中永久存在。 该修改即使出现系统故障也将一直保持。

此类型的任何实例成员都不能保证线程安全。 这使得事务成为并发的单位:用户可以在任何给定时间点有多个正在进行的事务,但对于给定的事务,必须一次调用一个 API。 接受事务并返回任务的所有 IReliableCollection{T} API 必须一次等待一个。

下面是正确用法的示例。


while (true)
{
    cancellationToken.ThrowIfCancellationRequested();

    try
    {
        using (var tx = this.StateManager.CreateTransaction())
        {
            await concurrentQueue.EnqueueAsync(tx, 12L, cancellationToken);
            await tx.CommitAsync();

            return;
        }
    }
    catch (TransactionFaultedException e)
    {
        // This indicates that the transaction was internally faulted by the system. One possible cause for this is that the transaction was long running
        // and blocked a checkpoint. Increasing the "ReliableStateManagerReplicatorSettings.CheckpointThresholdInMB" will help reduce the chances of running into this exception
        Console.WriteLine("Transaction was internally faulted, retrying the transaction: " + e);
    }
    catch (FabricNotPrimaryException e)
    {
        // Gracefully exit RunAsync as the new primary should have RunAsync invoked on it and continue work.
        // If instead enqueue was being executed as part of a client request, the client would be signaled to re-resolve.
        Console.WriteLine("Replica is not primary, exiting RunAsync: " + e);
        return;
    }
    catch (FabricNotReadableException e)
    {
        // Retry until the queue is readable or a different exception is thrown.
        Console.WriteLine("Queue is not readable, retrying the transaction: " + e);
    }
    catch (FabricObjectClosedException e)
    {
        // Gracefully exit RunAsync as this is happening due to replica close.
        // If instead enqueue was being executed as part of a client request, the client would be signaled to re-resolve.
        Console.WriteLine("Replica is closing, exiting RunAsync: " + e);
        return;
    }
    catch (TimeoutException e)
    {
        Console.WriteLine("Encountered TimeoutException during EnqueueAsync, retrying the transaction: " + e);
    }

    // Delay and retry.
    await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
}

下面是具有未定义行为的错误用法的示例。

using (var txn = this.StateManager.CreateTransaction())
{
    List<Task> taskList = new List<Task>();
    taskList.Add(concurrentQueue.DequeueAsync(txn, cancellationToken));
    taskList.Add(concurrentQueue.DequeueAsync(txn, cancellationToken));

    await Task.WhenAll(taskList);
    await txn.CommitAsync();
}

属性

CommitSequenceNumber

提交操作的序列号。

TransactionId

获取标识事务的值。

方法

Abort()

中止 () 事务回滚。

CommitAsync()

提交事务。

GetVisibilitySequenceNumberAsync()

获取可见性序列号。

适用于