All Products
Search
Document Center

Tablestore:Use the local transaction feature

Last Updated:Jul 01, 2024

After you enable the local transaction feature for a data table, you can create a local transaction based on the specified partition key value and perform read and write operations on the data in the local transaction. You can use the local transaction feature to perform atomic operations to read and write one or more rows.

You can use a local transaction to specify that the operations on data that shares the same partition key either all succeed or all fail. The isolation level of the local transaction is Read Committed.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

  • A data table is created and data is written to the data table.

Use the local transaction feature

  1. Use start_local_transaction to create a local transaction based on the specified partition key value and obtain the ID of the local transaction.

  2. Read and write data in the local transaction.

    You can call the GetRow, PutRow, DeleteRow, UpdateRow, BatchWriteRow, and GetRange operations to perform operations on data in the local transaction.

  3. Use commit_transaction to commit the local transaction or use abort_transaction to discard the local transaction.

Usage notes

  • You cannot use the auto-increment primary key column feature and the local transaction feature at the same time.

  • Pessimistic locking is used to control concurrent operations in a local transaction.

  • The validity period of a local transaction can be up to 60 seconds.

    If a local transaction is not committed or aborted within 60 seconds, the Tablestore server determines that the local transaction times out and aborts the transaction.

  • A transaction may be created on the Tablestore server even if a timeout error is returned. In this case, you can resend a transaction creation request after the created transaction times out.

  • If a local transaction is not committed, it may become invalid. In this case, retry the operations in this transaction.

  • If no write operation is performed on the data in a local transaction, the commit and abort operations have the same effect.

  • Tablestore imposes the following limits on read and write operations on the data in a local transaction:

    • The local transaction ID cannot be used to access data beyond the range specified based on the partition key value that is used to create the transaction.

    • The partition key values of all write requests in the same transaction must be the same as the partition key value used to create the transaction. This limit does not apply to read requests.

    • A local transaction can be used by only one request at a time. When the local transaction is in use, other operations that use the same local transaction ID fail.

    • The maximum interval for two consecutive read or write operations on the data in a local transaction is 60 seconds.

      If no read or write operation is performed on the data in a local transaction for more than 60 seconds, the Tablestore server determines that the transaction times out and aborts the transaction.

    • Up to 4 MB of data can be written to each transaction. The volume of data written to each transaction is calculated in the same way as a regular write request.

    • If you do not specify a version number for a cell, the Tablestore server automatically assigns a version number to the cell in the usual way when the cell is written to the transaction rather than the way when the transaction is committed.

    • If a BatchWriteRow request includes a local transaction ID, all rows in the request can be written only to the table that matches the local transaction ID.

    • When you use a local transaction, a write lock is added to the data of the partition key value based on which the local transaction is created. Only write requests that contain the local transaction ID and are initiated to write data in the local transaction can be successful. Other non-transactional requests or write requests that contain the IDs of other local transactions and are initiated to write data in the local transaction will fail. Data in the local transaction is unlocked if the transaction is committed or aborted, or if the transaction times out.

    • A local transaction remains valid even if a read or write request with the local transaction ID is rejected. You can specify a retry rule to resend the request, or you can abort the transaction.

Parameters

Parameter

Description

table_name

The name of the data table.

key

The partition key of the data table.

You must specify a partition key value when you create a local transaction.

primary_key

The primary key of the data table.

You must specify the values of all primary key columns when you read and write data in a local transaction.

transaction_id

The local transaction ID that uniquely identifies a local transaction.

You must specify a local transaction ID when you read and write data in a local transaction.

Examples

Use the local transaction feature to write a row of data

The following sample code provides an example on how to create a local transaction based on the specified partition key value in a table and write a row of data in the local transaction. If a row of data is written in the local transaction, commit the transaction. Otherwise, discard the transaction.

def  transaction_put_row(client):
    # Specify the name of the data table. 
    table_name = '<TABLE_NAME>'
    # Create a local transaction based on the partition key PK0. 
    key = [('PK0', 1)]
    # The returned value of the start_local_transaction method is the transaction ID. 
    transaction_id = client.start_local_transaction(table_name, key)

    # Write data. 
    primary_key = [('PK0', 1), ('PK1', 'transaction')]
    attribute_columns = [('value', 'origion value')]
    row = Row(primary_key, attribute_columns)
    condition = Condition(RowExistenceExpectation.IGNORE)
    try:
        # If you do not specify the ReturnType parameter when you call the put_row method, the value of the return_row parameter is None. 
        consumed, return_row = client.put_row(table_name, row, condition, None, transaction_id)
        # Display the number of write CUs that are consumed by the request. 
        print('put row succeed, consume %s write cu.' % consumed.write)
        # If a row of data is written in the local transaction, commit the local transaction. In this case, all data modifications in the local transaction take effect. You can discard a local transaction to invalidate all data modifications in the local transaction. 
        client.commit_transaction(transaction_id)
    # In most cases, client exceptions are caused by parameter errors or network exceptions. 
    except OTSClientError as e:
        print("put row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message()))
        # If a row of data fails to be written in the local transaction, discard the local transaction. In this case, all data modifications in the local transaction do not take effect on the data in the data table. 
        client.abort_transaction(transaction_id)
    # In most cases, server exceptions are caused by parameter or throttling errors. 
    except OTSServiceError as e:
        print("put row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id())) 
        # If a row of data fails to be written in the local transaction, discard the local transaction. In this case, all data modifications in the local transaction do not take effect on the data in the data table. 
        client.abort_transaction(transaction_id)

Use the local transaction feature to read a row of data

The following sample code provides an example on how to create a local transaction based on the specified partition key value in a table and read a row of data in the local transaction:

def transaction_get_row(client):
    # Specify the name of the data table. 
    table_name ='<TABLE_NAME>'
    # Create a local transaction based on the partition key PK0. 
    key = [('PK0', 1)]
    # The returned value of the start_local_transaction method is the transaction ID. 
    transaction_id = client.start_local_transaction(table_name, key)
    
    # Read data. 
    primary_key = [('PK0', 1), ('PK1', 'transaction')]
    columns_to_get = ['value']
    consumed, return_row, next_token = client.get_row(
        table_name, primary_key, columns_to_get, None, 1, None, None, None, None, transaction_id
    )
    for att in return_row.attribute_columns:
        print ('\tname:%s\tvalue:%s' % (att[0], att[1]))

    Commit or discard the local transaction. The effect on a read operation when you commit or discard a local transaction is the same. 
    # Commit the local transaction to allow all data modifications in the local transaction to take effect. 
    client.commit_transaction(transaction_id)
    # Discard the local transaction. In this case, all data modifications in the local transaction do not take effect on the data in the data table. 
    #client.abort_transaction(transaction_id)

References

If you want to write multiple rows of data at the same time or read data whose primary key values are within a specific range, create a local transaction. Then, refer to the sample code provided in the Write data or Read data topic to initiate a request that includes the local transaction ID.