Reference documentation and code samples for the Cloud Datastore Client class Transaction.
Represents a Transaction
A transaction is a set of Datastore operations on one or more entities. Each
transaction is guaranteed to be atomic, which means that transactions are
never partially applied. Either all of the operations in the transaction are
applied, or none of them are applied.
It is highly recommended that users read and understand the underlying
concepts inTransactionsbefore beginning.
Mutations (i.e. insert, update and delete) are not executed immediately.
Calls to those methods (and their batch equivalents) will enqueue a new
mutation. CallingTransaction::commit()will
execute all the mutations in the order they were enqueued, and end the
transaction.
Lookups and queries can be run in a transaction, so long as they are run
prior to committing or rolling back the transaction.
Transactions are anoptionalfeature of Google Cloud Datastore. Queries,
lookups and mutations can be executed outside of a Transaction fromDatastoreClient.
Example:
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient();
$transaction = $datastore->transaction();
Namespace
Google \ Cloud \ Datastore
Methods
insert
Insert an entity.
Changes are not immediately committed to Cloud Datastore when calling
this method. UseTransaction::commit()to
commit changes and end the transaction.
If entities with incomplete keys are provided, this method will immediately
trigger a service call to allocate IDs to the entities.
Entities must be updated as an entire resource. Patch operations are not supported. Because entities can be created manually, or obtained by a lookup or query, it is possible to accidentally overwrite an existing record with a new one when manually creating an entity. To provide additional safety, this flag must be set totruein order to update a record when the entity provided was not obtained through a lookup or query.Defaults tofalse.
Entities must be updated as an entire resource. Patch operations are not supported. Because entities can be created manually, or obtained by a lookup or query, it is possible to accidentally overwrite an existing record with a new one when manually creating an entity. To provide additional safety, this flag must be set totruein order to update a record when the entity provided was not obtained through a lookup or query.Defaults tofalse.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Datastore Client - Class Transaction (1.33.1)\n\nVersion latestkeyboard_arrow_down\n\n- [1.33.1 (latest)](/php/docs/reference/cloud-datastore/latest/Transaction)\n- [1.33.0](/php/docs/reference/cloud-datastore/1.33.0/Transaction)\n- [1.32.3](/php/docs/reference/cloud-datastore/1.32.3/Transaction)\n- [1.31.0](/php/docs/reference/cloud-datastore/1.31.0/Transaction)\n- [1.30.0](/php/docs/reference/cloud-datastore/1.30.0/Transaction)\n- [1.29.2](/php/docs/reference/cloud-datastore/1.29.2/Transaction)\n- [1.28.2](/php/docs/reference/cloud-datastore/1.28.2/Transaction)\n- [1.26.0](/php/docs/reference/cloud-datastore/1.26.0/Transaction)\n- [1.25.0](/php/docs/reference/cloud-datastore/1.25.0/Transaction)\n- [1.24.4](/php/docs/reference/cloud-datastore/1.24.4/Transaction)\n- [1.23.0](/php/docs/reference/cloud-datastore/1.23.0/Transaction)\n- [1.22.1](/php/docs/reference/cloud-datastore/1.22.1/Transaction)\n- [1.21.2](/php/docs/reference/cloud-datastore/1.21.2/Transaction)\n- [1.19.0](/php/docs/reference/cloud-datastore/1.19.0/Transaction)\n- [1.18.1](/php/docs/reference/cloud-datastore/1.18.1/Transaction)\n- [1.17.1](/php/docs/reference/cloud-datastore/1.17.1/Transaction) \nReference documentation and code samples for the Cloud Datastore Client class Transaction.\n\nRepresents a Transaction\n\nA transaction is a set of Datastore operations on one or more entities. Each\ntransaction is guaranteed to be atomic, which means that transactions are\nnever partially applied. Either all of the operations in the transaction are\napplied, or none of them are applied.\n\nIt is highly recommended that users read and understand the underlying\nconcepts in [Transactions](https://cloud.google.com/datastore/docs/concepts/transactions)\nbefore beginning.\n\nMutations (i.e. insert, update and delete) are not executed immediately.\nCalls to those methods (and their batch equivalents) will enqueue a new\nmutation. Calling [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) will\nexecute all the mutations in the order they were enqueued, and end the\ntransaction.\n\nLookups and queries can be run in a transaction, so long as they are run\nprior to committing or rolling back the transaction.\n\nTransactions are an **optional** feature of Google Cloud Datastore. Queries,\nlookups and mutations can be executed outside of a Transaction from\n[DatastoreClient](/php/docs/reference/cloud-datastore/latest/DatastoreClient).\n\nExample: \n\n use Google\\Cloud\\Datastore\\DatastoreClient;\n\n $datastore = new DatastoreClient();\n\n $transaction = $datastore-\u003etransaction();\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Datastore\n\nMethods\n-------\n\n### insert\n\nInsert an entity.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nIf entities with incomplete keys are provided, this method will immediately\ntrigger a service call to allocate IDs to the entities.\n\nExample: \n\n $key = $datastore-\u003ekey('Person', 'Bob');\n $entity = $datastore-\u003eentity($key, ['firstName' =\u003e 'Bob']);\n\n $transaction-\u003einsert($entity);\n $transaction-\u003ecommit();\n\n### insertBatch\n\nInsert multiple entities.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nIf entities with incomplete keys are provided, this method will immediately\ntrigger a service call to allocate IDs to the entities.\n\nExample: \n\n $entities = [\n $datastore-\u003eentity('Person', ['firstName' =\u003e 'Bob']),\n $datastore-\u003eentity('Person', ['firstName' =\u003e 'John'])\n ];\n\n $transaction-\u003einsertBatch($entities);\n $transaction-\u003ecommit();\n\n### update\n\nUpdate an entity.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nExample: \n\n $entity['firstName'] = 'Bob';\n\n $transaction-\u003eupdate($entity);\n $transaction-\u003ecommit();\n\n### updateBatch\n\nUpdate multiple entities.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nExample: \n\n $entities[0]['firstName'] = 'Bob';\n $entities[1]['firstName'] = 'John';\n\n $transaction-\u003eupdateBatch($entities);\n $transaction-\u003ecommit();\n\n### upsert\n\nUpsert an entity.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nUpsert will create a record if one does not already exist, or overwrite\nexisting record if one already exists.\n\nIf entities with incomplete keys are provided, this method will immediately\ntrigger a service call to allocate IDs to the entities.\n\nExample: \n\n $key = $datastore-\u003ekey('Person', 'Bob');\n $entity = $datastore-\u003eentity($key, ['firstName' =\u003e 'Bob']);\n\n $transaction-\u003eupsert($entity);\n $transaction-\u003ecommit();\n\n### upsertBatch\n\nUpsert multiple entities.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nUpsert will create a record if one does not already exist, or overwrite\nexisting record if one already exists.\n\nIf entities with incomplete keys are provided, this method will immediately\ntrigger a service call to allocate IDs to the entities.\n\nExample: \n\n $keys = [\n $datastore-\u003ekey('Person', 'Bob'),\n $datastore-\u003ekey('Person', 'John')\n ];\n\n $entities = [\n $datastore-\u003eentity($keys[0], ['firstName' =\u003e 'Bob']),\n $datastore-\u003eentity($keys[1], ['firstName' =\u003e 'John'])\n ];\n\n $transaction-\u003eupsertBatch($entities);\n $transaction-\u003ecommit();\n\n### delete\n\nDelete a record.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nExample: \n\n $key = $datastore-\u003ekey('Person', 'Bob');\n\n $transaction-\u003edelete($key);\n $transaction-\u003ecommit();\n\n### deleteBatch\n\nDelete multiple records.\n\nChanges are not immediately committed to Cloud Datastore when calling\nthis method. Use [Transaction::commit()](/php/docs/reference/cloud-datastore/latest/Transaction#_Google_Cloud_Datastore_Transaction__commit__) to\ncommit changes and end the transaction.\n\nExample: \n\n $keys = [\n $datastore-\u003ekey('Person', 'Bob'),\n $datastore-\u003ekey('Person', 'John')\n ];\n\n $transaction-\u003edeleteBatch($keys);\n $transaction-\u003ecommit();\n\n### commit\n\nSee also:\n\n- [Commit API documentation](https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit)"]]