Start Transaction - PowerShell


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.


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


  • -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.


Simple Transaction:

Start-Transaction -ConnectionUri "" -Database "myDatabase"

Transaction with Custom Name:

Start-Transaction -ConnectionUri "" -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.


Rolling Back Transactions:

Start-Transaction -ConnectionUri "" -Database "myDatabase"

# Execute operations within the transaction

Get-Transaction -ConnectionUri "" | 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.