All Products
Search
Document Center

Tablestore:Configure local transaction

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

API operations

This section describes the following API operations that are related to the local transaction feature: startLocalTransaction, commitTransaction, and abortTransaction.

startLocalTransaction

    /**
     * Create a local transaction and obtain the local transaction ID. 
     * @api
     * @param [] $request
     *            The request parameters, including the table name and partition key. 
     * @return [] The response. 
     * @throws OTSClientException The exception that is returned when a parameter error occurs or the Tablestore server returns a verification error. 
     * @throws OTSServerException The exception that is returned when the Tablestore server returns an error. 
     * @example "src/examples/StartLocalTransaction.php" 50
     */
    public function startLocalTransaction(array $request)

commitTransaction

    /**
     * Commit a local transaction. 
     * @api
     *
     * @param [] $request
     *          The request parameter, which is the transaction ID. 
     * @return [] The response. 
     * @throws OTSClientException The exception that is returned when a parameter error occurs or the Tablestore server returns a verification error. 
     * @throws OTSServerException The exception that is returned when the Tablestore server returns an error. 
     * @example "src/examples/CommitTransaction.php" 50
     */
    public function commitTransaction(array $request)

abortTransaction

    /**
     * Abort a local transaction. 
     * @api
     *
     * @param [] $request
     *          The request parameter, which is the transaction ID. 
     * @return [] The response. 
     * @throws OTSClientException The exception that is returned if a parameter error occurs or the Tablestore server returns a verification error. 
     * @throws OTSServerException The exception that is returned if the Tablestore server returns an error. 
     * @example "src/examples/AbortTransaction.php" 20
     */
    public function abortTransaction(array $request)

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 data row

In this example, a row of data is written to a local transaction created for a specified partition key of a table and the local transaction is committed.

// Obtain the local transaction ID.  
$response = $client->startLocalTransaction (array (
    'table_name' => 'TransactionTable',
// The primary key is [PK0:INTEGER,PK1:STRING]. 
    'key' => array(  
        array('PK0', 123)
    )
));

$attr = array();
$attr[] = ['col0', 'bbb'];

$request = [
    'table_name' => 'TransactionTable',
    'condition' => RowExistenceExpectationConst::CONST_IGNORE, // You can set condition to IGNORE, EXPECT_EXIST, or EXPECT_NOT_EXIST. 
// Specify the primary key. 
    'primary_key' => [ 
        ['PK0', 123],
        ['PK1', 'abc']
    ],
    'attribute_columns' => $attr,
    'transaction_id' => $response['transaction_id']  
];
// Execute the putRow method to write data.  
$client->putRow($request);
// Commit the transaction for all data modifications within the transaction to take effect. You can discard a local transaction to invalidate all data modifications in the local transaction.  
$client->commitTransaction(array(      
    'transaction_id' => $response['transaction_id']
));
// Abort the local transaction to move all data modifications within the transaction out of effect.  
// $client->abortTransaction(array( 
//     'transaction_id' => $response['transaction_id']
// ));

Use the local transaction feature to read a data row

In this example, a row of data is read in a local transaction created for a specified partition key of a table.

// Obtain the local transaction ID. 
$response = $client->startLocalTransaction (array (
    'table_name' => 'TransactionTable',
// The primary key is [PK0:INTEGER,PK1:STRING]. 
    'key' => array(  
        array('PK0', 123)
    )
));

$request = array(
    'table_name' => 'TransactionTable',
// Specify the primary key. 
    'primary_key' => array (
        array('PK0', 123),
        array('PK1', 'abc')
    ),
    'max_versions' => 1,
    'columns_to_get' => ['col0'],
    'transaction_id' => $response['transaction_id']
);
// Execute the getRow method to read data.  
$client->getRow($request);
// Commit or abort the local transaction. The effect on a read operation when you commit or discard a local transaction is the same. 
// Commit the transaction for all data modifications within the transaction to take effect. 
$client->commitTransaction(array(// Commit the transaction 
    'transaction_id' => $response['transaction_id']
 ));
// Abort the local transaction to move all data modifications within the transaction out of effect. 
// $client->abortTransaction(array(
//     'transaction_id' => $response['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.