Start Transaction - PowerShell


Overview

Start-Transaction initiates a new transaction, a unit of work executed as an atomic operation within a session. Transactions ensure data integrity by ensuring that changes made within a transaction are either fully completed or rolled back if the transaction fails.

Syntax

Start-Transaction [-ConnectionUri <Uri>] [-Database <String>] [-AsJob] [-TransactionName <String>]

Options/Flags

  • -ConnectionUri: Specifies the URI of the Azure Cosmos DB account.
  • -Database: Specifies the database within the Azure Cosmos DB account.
  • -AsJob: Starts the transaction as a background job, allowing the command to return immediately.
  • -TransactionName: Assigns a unique name to the transaction.

Examples

Simple Transaction:

Start-Transaction -ConnectionUri "https://contoso.documents.azure.com:443/" -Database "myDatabase"

Transaction with Custom Name:

Start-Transaction -ConnectionUri "https://contoso.documents.azure.com:443/" -Database "myDatabase" -TransactionName "MyTransaction"

Common Issues

  • Transaction timeouts: Transactions have a maximum duration. Exceeding this duration will cause the transaction to fail.
  • Database inconsistency: Changes made within a transaction are not visible to other sessions until the transaction is committed.

Integration

Rolling Back Transactions:

Start-Transaction -ConnectionUri "https://contoso.documents.azure.com:443/" -Database "myDatabase"

# Execute operations within the transaction

Get-Transaction -ConnectionUri "https://contoso.documents.azure.com:443/" | Rollback-Transaction
  • Get-Transaction: Retrieves information about active transactions.
  • Commit-Transaction: Commits the changes made within a transaction.
  • Rollback-Transaction: Rolls back the changes made within a transaction.