All Products
Search
Document Center

Tablestore:Local transaction

Last Updated:Aug 23, 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

Use the local transaction feature

  1. Use startLocalTransaction 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 commitTransaction to commit the local transaction or use abortTransaction to abort 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

Required

Description

tableName

Yes

The name of the data table.

primaryKey

Yes

The primary key of the data table.

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

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

transactionId

Yes

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 a local transaction to write data and commit the local transaction

The following sample code provides an example on how to create a local transaction based on a specific partition key value, write data in the local transaction, and commit the local transaction to apply all data modifications in the local transaction to the data table:

(async () => {
    try {

        // Create a local transaction. 
        const response = await client.startLocalTransaction({
            tableName,
            primaryKey: [{  // You need to only specify the partition key value for the local transaction. 
                "id": "partitionKeyValue"
            }]
        });

        // Obtain the local transaction ID. 
        const transactionId = response.transactionId;

        // Write data in the local transaction. 
        await client.putRow({
            tableName,
            condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
            primaryKey,
            attributeColumns: [{
                col: 'updated'
            }],
            transactionId
        });

        // Commit the local transaction. 
        await client.commitTransaction({
            transactionId
        })
    } catch (e) {
        console.error(e)
    }
})();

Use a local transaction to write data and abort the local transaction

The following sample code provides an example on how to create a local transaction based on a specific partition key value, write data in the local transaction, and abort the local transaction to discard all data modifications in the local transaction:

(async () => {
    try {

        // Create a local transaction. 
        const response = await client.startLocalTransaction({
            tableName,
            primaryKey: [{  // You need to only specify the partition key value for the local transaction. 
                "id": "partitionKeyValue"
            }]
        });
        // Obtain the local transaction ID. 
        const transactionId = response.transactionId

        // Write data in the local transaction. 
        await client.putRow({
            tableName,
            condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
            primaryKey,
            attributeColumns: [{
                col: 'updated'
            }],
            transactionId
        });
        // Abort the local transaction. 
        await client.abortTransaction({
            transactionId
    })
  } catch (e) {
    console.error(e)
  }
})();

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.