How to control CosmosDB acid transactions (commit and rollback) using JAVA SDK?

Xu, Kyson K 151 Reputation points
2023-05-24T05:35:10.8333333+00:00

In the same method, first delete and then save, when the deletion succeeds but the save fails, how to implement the transaction rollback using Cosmos Java SDK? Thanks

public void deleteAndSave(){
	deleteAllData(); //succeed
	saveNewData();  //failed
}
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,441 questions
0 comments No comments
{count} votes

Accepted answer
  1. ShaktiSingh-MSFT 13,271 Reputation points Microsoft Employee
    2023-05-24T08:23:07.1166667+00:00

    Hi Xu, Kyson K •,

    Thanks for posting this question in Microsoft Q&A forum.

    As I understand, you need to know how to control CosmosDB acid transactions (commit and rollback) using JAVA SDK.

    In the .NET and Java SDKs, the TransactionalBatch class is used to define this batch of operations. If all operations succeed in the order they're described within the transactional batch operation, the transaction will be committed. However, if any operation fails, the entire transaction is rolled back.

    Reference Link: Transaction Batch Operations in Azure Cosmos DB using the .NET or Java SDK.

    When the ExecuteAsync method is called, all operations in the TransactionalBatch object are grouped, serialized into a single payload, and sent as a single request to the Azure Cosmos DB service.

    The service receives the request and executes all operations within a transactional scope, and returns a response using the same serialization protocol. This response is either a success, or a failure, and supplies individual operation responses per operation.

    The SDK exposes the response for you to verify the result and, optionally, extract each of the internal operation results.

    Hope this information helps.

    Do let us know if you have further queries. Thank you.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sedat SALMAN 13,080 Reputation points
    2023-05-24T06:43:29.8166667+00:00

    With Azure Cosmos DB, transactional batch processing was introduced to enable a series of operations (like create, update, delete) within the same partition key to be executed atomically. This means that if any operation within the transactional batch fails, all operations within that batch are rolled back, similar to a transaction in a relational database.

    However, there are two important caveats to remember:

    • These operations must be on items within the same logical partition. In other words, they must have the same partition key.
    • Transactions are not supported across different containers.

    Below is an example of how you could use transactional batch processing in the Cosmos DB Java SDK:

    public void deleteAndSave() {
        CosmosContainer container = cosmosDatabase.getContainer(containerName);
    
        List<ItemOperation> operations = new ArrayList<>();
    
        // The partition key value for operations
        String partitionKeyValue = "PartitionKey";
    
        operations.add(new CosmosItemOperation(CosmosItemOperationType.DELETE, "ItemId1", new PartitionKey(partitionKeyValue)));
        operations.add(new CosmosItemOperation(CosmosItemOperationType.CREATE, newData));
    
        try {
            TransactionalBatchResponse response = container.executeTransactionalBatch(new TransactionalBatch(partitionKeyValue, operations));
            if (!response.isSuccessStatusCode()) {
                System.out.println("Batch failed with status code: " + response.getStatusCode());
            }
        } catch (CosmosException e) {
            System.err.println(e.toString());
        }
    }