OtsClientStartTransaction Method Aliyun Open Services SDK for .NET
在表或表组上开始一个事务(Transaction),并得到该事务ID。

Namespace: Aliyun.OpenServices.OpenTableService
Assembly: Aliyun.OpenServices (in Aliyun.OpenServices.dll) Version: 1.0.5290.21916
Syntax

public string StartTransaction(
	string entityName,
	PartitionKeyValue partitionKeyValue
)

Parameters

entityName
Type: SystemString
表(Table)名或表组(Table Group)名。
partitionKeyValue
Type: Aliyun.OpenServices.OpenTableServicePartitionKeyValue
表示事务(Transaction)建立在哪个数据分片键(Partition Key)上。

Return Value

Type: String
事务(Transaction)ID。

Implements

IOtsStartTransaction(String, PartitionKeyValue)
Exceptions

ExceptionCondition
ArgumentException

entityName为空引用或空字符串

- 或 -

entityName违反OTS名称的命名规范。

OtsException

OTS访问返回错误消息。

WebException

由于网络原因请求失败,

- 或 -

访问超时。

InvalidOperationException

返回结果解析错误。

Remarks

用户必须指定数据分片键,并保证所有在这个事务中的操作的数据分片键的值等于在此API中指定的数据分片键的值。

若此数据分片键已经存在另一个事务中且该事务没有完成或被取消,则本次事务会直接失败,用户可以重试。

Examples

下面的示例代码演示如何开始一个事务(Transaction),并异步地批量提交数据修改。该示例中使用CreateTable(TableMeta)方法示例代码所创建的表。
using System;
using System.Linq;
using System.Net;
using Aliyun.OpenServices.OpenTableService;

namespace Aliyun.OpenServices.Samples.OpenTableService
{
    class AsyncBatchModifyDataSample
    {
        string endpoint = "http://ots.aliyuncs.com";
        string accessId = "<your access id>";
        string accessKey = "<your access key>";
        OtsClient otsClient;

        public AsyncBatchModifyDataSample()
        {
            otsClient = new OtsClient(endpoint, accessId, accessKey);
        }

        public void BatchInsertData(string tableName)
        {
            int uid = 1;
            // 将5条RowPutChange放到列表中
            var rowChanges = new List<RowChange>();

            for (int i = 0; i < 5; i++)
            {
                var rowChange = new RowPutChange();
                rowChange.PrimaryKeys["uid"] = uid;
                rowChange.PrimaryKeys["flag"] = true;
                rowChange.PrimaryKeys["name"] = "contact " + i.ToString();

                rowChange.AttributeColumns["groupid"] = 1;
                rowChange.AttributeColumns["address"] = "中国某地";

                rowChanges.Add(rowChange);
            }

            // 开始一个事务
            try
            {
                var transactionId = otsClient.StartTransaction(tableName, uid);

                // 开始批量修改数据的异步操作
                otsClient.BeginBatchModifyData(tableName, rowChanges, transactionId, BatchModifyDataCallback, transactionId);

                // 这里进行其他操作
            }
            catch (OtsException ex)
            {
                Console.WriteLine("操作失败。OTS异常消息:" + ex.Message);
                // RequestId和HostId可以在有问题时用于联系客服诊断异常
                Console.WriteLine("Request ID: {0}\tHostID: {1}", ex.RequestId, ex.HostId);
            }
            catch (WebException ex)
            {
                Console.WriteLine("操作失败。网络异常:{0}。请检查Endpoint或网络链接。", ex.Message);
            }
        }

        private void BatchModifyDataCallback(IAsyncResult asyncResult)
        {
            try
            {
                var transactionId = asyncResult.AsyncState as string;

                // 操作结束时必须调用EndBatchModifyData方法,否则可能会产生资源泄露
                otsClient.EndBatchModifyData(asyncResult);

                // 需要调用ComitTransaction方法提交数据修改
                otsClient.CommitTransaction(transactionId);
            }
            catch (OtsException ex)
            {
                Console.WriteLine("操作失败。OTS异常消息:" + ex.Message);
                // RequestId和HostId可以在有问题时用于联系客服诊断异常
                Console.WriteLine("Request ID: {0}\tHostID: {1}", ex.RequestId, ex.HostId);
            }
            catch (WebException ex)
            {
                Console.WriteLine("操作失败。网络异常:{0}。请检查Endpoint或网络链接。", ex.Message);
            }
        }
    }
}
See Also

Reference