Cloud Datastore Client - Class Transaction (2.0.3) Stay organized with collections Save and categorize content based on your preferences.
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. Eachtransaction is guaranteed to be atomic, which means that transactions arenever partially applied. Either all of the operations in the transaction areapplied, or none of them are applied.
It is highly recommended that users read and understand the underlyingconcepts inTransactionsbefore beginning.
Mutations (i.e. insert, update and delete) are not executed immediately.Calls to those methods (and their batch equivalents) will enqueue a newmutation. CallingTransaction::commit() willexecute all the mutations in the order they were enqueued, and end thetransaction.
Lookups and queries can be run in a transaction, so long as they are runprior to committing or rolling back the transaction.
Transactions are anoptional feature 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 \ DatastoreMethods
insert
Insert an entity.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
If entities with incomplete keys are provided, this method will immediatelytrigger a service call to allocate IDs to the entities.
Example:
$key = $datastore->key('Person', 'Bob');$entity = $datastore->entity($key, ['firstName' => 'Bob']);$transaction->insert($entity);$transaction->commit();| Parameter | |
|---|---|
| Name | Description |
entity | EntityInterfaceThe entity to insert. |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
insertBatch
Insert multiple entities.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
If entities with incomplete keys are provided, this method will immediatelytrigger a service call to allocate IDs to the entities.
Example:
$entities = [ $datastore->entity('Person', ['firstName' => 'Bob']), $datastore->entity('Person', ['firstName' => 'John'])];$transaction->insertBatch($entities);$transaction->commit();| Parameter | |
|---|---|
| Name | Description |
entities | array<EntityInterface>The entities to insert. |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
update
Update an entity.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
Example:
$entity['firstName'] = 'Bob';$transaction->update($entity);$transaction->commit();| Parameters | |
|---|---|
| Name | Description |
entity | EntityInterfaceThe entity to update. |
options | arrayConfiguration Options |
↳ allowOverwrite | boolEntities 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 to |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
updateBatch
Update multiple entities.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
Example:
$entities[0]['firstName'] = 'Bob';$entities[1]['firstName'] = 'John';$transaction->updateBatch($entities);$transaction->commit();| Parameters | |
|---|---|
| Name | Description |
entities | array<EntityInterface>The entities to update. |
options | arrayConfiguration Options |
↳ allowOverwrite | boolEntities 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 to |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
upsert
Upsert an entity.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
Upsert will create a record if one does not already exist, or overwriteexisting record if one already exists.
If entities with incomplete keys are provided, this method will immediatelytrigger a service call to allocate IDs to the entities.
Example:
$key = $datastore->key('Person', 'Bob');$entity = $datastore->entity($key, ['firstName' => 'Bob']);$transaction->upsert($entity);$transaction->commit();| Parameter | |
|---|---|
| Name | Description |
entity | EntityInterfaceThe entity to upsert. |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
upsertBatch
Upsert multiple entities.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
Upsert will create a record if one does not already exist, or overwriteexisting record if one already exists.
If entities with incomplete keys are provided, this method will immediatelytrigger a service call to allocate IDs to the entities.
Example:
$keys = [ $datastore->key('Person', 'Bob'), $datastore->key('Person', 'John')];$entities = [ $datastore->entity($keys[0], ['firstName' => 'Bob']), $datastore->entity($keys[1], ['firstName' => 'John'])];$transaction->upsertBatch($entities);$transaction->commit();| Parameter | |
|---|---|
| Name | Description |
entities | array<EntityInterface>The entities to upsert. |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
delete
Delete a record.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
Example:
$key = $datastore->key('Person', 'Bob');$transaction->delete($key);$transaction->commit();| Parameter | |
|---|---|
| Name | Description |
key | KeyThe key to delete |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
deleteBatch
Delete multiple records.
Changes are not immediately committed to Cloud Datastore when callingthis method. UseTransaction::commit() tocommit changes and end the transaction.
Example:
$keys = [ $datastore->key('Person', 'Bob'), $datastore->key('Person', 'John')];$transaction->deleteBatch($keys);$transaction->commit();| Parameter | |
|---|---|
| Name | Description |
keys | array<Key>The keys to delete. |
| Returns | |
|---|---|
| Type | Description |
Transaction | |
commit
See also:
| Parameter | |
|---|---|
| Name | Description |
options | array[optional] Configuration Options. |
| Returns | |
|---|---|
| Type | Description |
array | [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body) |
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-01-24 UTC.