If you do not plan on performing any writes in your transaction, a
Snapshot is a better solution which does not
require a commit or rollback and does not lock any data.
Transactions may raise Google\Cloud\Core\Exception\AbortedException errors
when the transaction cannot complete for any reason. In this case, the entire
operation (all reads and writes) should be reapplied atomically. Google Cloud
PHP handles this transparently when using
Database::runTransaction(). In other cases, it is
highly recommended that applications implement their own retry logic.
Example:
use Google\Cloud\Spanner\SpannerClient;
$spanner = new SpannerClient();
$database = $spanner->connect('my-instance', 'my-database');
$database->runTransaction(function (Transaction $t) {
// do stuff.
$t->commit();
});
// Get a transaction to manage manually.
$transaction = $database->transaction();
Consumed internally for properly map mutation data.
getCommitStats
Get the commit stats for this transaction. Commit stats are only available after commit has been called with
return_commit_stats => true. If commit is called multiple times, only the commitStats for the last commit will
be available.
A key/value array of Query Parameters, where the key is represented in the query string prefixed by a @ symbol.
↳ types
array
A key/value array of Query Parameter types. Generally, Google Cloud PHP can infer types. Explicit type declarations are required in the case of struct parameters, or when a null value exists as a parameter. Accepted values for primitive types are defined as constants on Database, and are as follows: Database::TYPE_BOOL, Database::TYPE_INT64, Database::TYPE_FLOAT64, Database::TYPE_TIMESTAMP, Database::TYPE_DATE, Database::TYPE_STRING, Database::TYPE_BYTES. If the value is an array, use ArrayType to declare the array parameter types. Likewise, for structs, use StructType.
↳ requestOptions
array
Request options. For more information on available options, please see the upstream documentation. Please note, if using the priority setting you may utilize the constants available on V1\RequestOptions\Priority to set a value. Please note, the transactionTag setting will be ignored as the transaction tag should have already been set when creating the transaction.
A list of DML statements to run. Each statement
must contain a sql key, where the value is a DML string. If the
DML contains placeholders, values are provided as a key/value array
in key parameters. If parameter types are required, they must be
provided in key types. Generally, Google Cloud PHP can
infer types. Explicit type declarations are required in the case
of struct parameters, or when a null value exists as a parameter.
Accepted values for primitive types are defined as constants on
Database, and are as follows:
Database::TYPE_BOOL, Database::TYPE_INT64,
Database::TYPE_FLOAT64, Database::TYPE_TIMESTAMP,
Database::TYPE_DATE, Database::TYPE_STRING,
Database::TYPE_BYTES. If the value is an array, use
ArrayType to declare the array
parameter types. Likewise, for structs, use
StructType.
options
array
Configuration Options.
↳ requestOptions
array
Request options. For more information on available options, please see the upstream documentation. Please note, if using the priority setting you may utilize the constants available on V1\RequestOptions\Priority to set a value. Please note, the transactionTag setting will be ignored as the transaction tag should have already been set when creating the transaction.
Rolls back a transaction, releasing any locks it holds. It is a good idea
to call this for any transaction that includes one or more Read or
ExecuteSql requests and ultimately decides not to commit.
This closes the transaction, preventing any future API calls inside it.
Rollback will NOT error if the transaction is not found or was already aborted.
Example:
$transaction->rollback();
Parameter
Name
Description
options
array
[optional] Configuration Options.
Returns
Type
Description
void
commit
Commit and end the transaction.
It is advised that transactions be run inside
Database::runTransaction() in order to take
advantage of automated transaction retry in case of a transaction aborted
error.
Example:
$transaction->commit();
Parameters
Name
Description
options
array
Configuration Options.
↳ mutations
array
An array of mutations to commit. May be used instead of or in addition to enqueing mutations separately.
The amount of latency this request is willing to incur in order to improve throughput. Defaults to null.
↳ requestOptions
array
Request options. For more information on available options, please see the upstream documentation. Please note, if using the priority setting you may utilize the constants available on V1\RequestOptions\Priority to set a value. Please note, the requestTag setting will be ignored as it is not supported for commit requests.
Will be one of Transaction::STATE_ACTIVE,
Transaction::STATE_COMMITTED, or Transaction::STATE_ROLLED_BACK.
Example:
$state = $transaction->state();
Returns
Type
Description
int
isRetry
Check whether the current transaction is a retry transaction.
When using Database::runTransaction(),
transactions are automatically retried when a conflict causes it to abort.
In such cases, subsequent invocations of the transaction callable will
provide a transaction where $transaction->isRetry() is true. This can
be useful for debugging and understanding how code is working.
Example:
if ($transaction->isRetry()) {
echo 'This is a retry transaction!';
}
[[["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 Spanner Client - Class Transaction (1.104.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.104.0 (latest)](/php/docs/reference/cloud-spanner/latest/Transaction)\n- [1.103.0](/php/docs/reference/cloud-spanner/1.103.0/Transaction)\n- [1.102.0](/php/docs/reference/cloud-spanner/1.102.0/Transaction)\n- [1.101.0](/php/docs/reference/cloud-spanner/1.101.0/Transaction)\n- [1.100.0](/php/docs/reference/cloud-spanner/1.100.0/Transaction)\n- [1.98.0](/php/docs/reference/cloud-spanner/1.98.0/Transaction)\n- [1.97.0](/php/docs/reference/cloud-spanner/1.97.0/Transaction)\n- [1.96.0](/php/docs/reference/cloud-spanner/1.96.0/Transaction)\n- [1.95.0](/php/docs/reference/cloud-spanner/1.95.0/Transaction)\n- [1.94.0](/php/docs/reference/cloud-spanner/1.94.0/Transaction)\n- [1.93.1](/php/docs/reference/cloud-spanner/1.93.1/Transaction)\n- [1.92.1](/php/docs/reference/cloud-spanner/1.92.1/Transaction)\n- [1.91.0](/php/docs/reference/cloud-spanner/1.91.0/Transaction)\n- [1.90.0](/php/docs/reference/cloud-spanner/1.90.0/Transaction)\n- [1.89.0](/php/docs/reference/cloud-spanner/1.89.0/Transaction)\n- [1.88.0](/php/docs/reference/cloud-spanner/1.88.0/Transaction)\n- [1.87.0](/php/docs/reference/cloud-spanner/1.87.0/Transaction)\n- [1.86.0](/php/docs/reference/cloud-spanner/1.86.0/Transaction)\n- [1.85.0](/php/docs/reference/cloud-spanner/1.85.0/Transaction)\n- [1.84.0](/php/docs/reference/cloud-spanner/1.84.0/Transaction)\n- [1.83.0](/php/docs/reference/cloud-spanner/1.83.0/Transaction)\n- [1.82.0](/php/docs/reference/cloud-spanner/1.82.0/Transaction)\n- [1.81.0](/php/docs/reference/cloud-spanner/1.81.0/Transaction)\n- [1.80.0](/php/docs/reference/cloud-spanner/1.80.0/Transaction)\n- [1.79.0](/php/docs/reference/cloud-spanner/1.79.0/Transaction)\n- [1.78.0](/php/docs/reference/cloud-spanner/1.78.0/Transaction)\n- [1.77.0](/php/docs/reference/cloud-spanner/1.77.0/Transaction)\n- [1.76.1](/php/docs/reference/cloud-spanner/1.76.1/Transaction)\n- [1.68.0](/php/docs/reference/cloud-spanner/1.68.0/Transaction)\n- [1.67.0](/php/docs/reference/cloud-spanner/1.67.0/Transaction)\n- [1.66.0](/php/docs/reference/cloud-spanner/1.66.0/Transaction)\n- [1.65.0](/php/docs/reference/cloud-spanner/1.65.0/Transaction)\n- [1.64.0](/php/docs/reference/cloud-spanner/1.64.0/Transaction)\n- [1.63.2](/php/docs/reference/cloud-spanner/1.63.2/Transaction)\n- [1.62.1](/php/docs/reference/cloud-spanner/1.62.1/Transaction)\n- [1.61.0](/php/docs/reference/cloud-spanner/1.61.0/Transaction)\n- [1.60.0](/php/docs/reference/cloud-spanner/1.60.0/Transaction)\n- [1.59.0](/php/docs/reference/cloud-spanner/1.59.0/Transaction)\n- [1.58.4](/php/docs/reference/cloud-spanner/1.58.4/Transaction)\n- [1.57.0](/php/docs/reference/cloud-spanner/1.57.0/Transaction)\n- [1.56.0](/php/docs/reference/cloud-spanner/1.56.0/Transaction)\n- [1.55.0](/php/docs/reference/cloud-spanner/1.55.0/Transaction)\n- [1.54.2](/php/docs/reference/cloud-spanner/1.54.2/Transaction) \nReference documentation and code samples for the Cloud Spanner Client class Transaction.\n\nManages interaction with Cloud Spanner inside a Transaction.\n\nTransactions can be started via\n[Database::runTransaction()](/php/docs/reference/cloud-spanner/latest/Database#_Google_Cloud_Spanner_Database__runTransaction__) (recommended) or via\n[Database::transaction()](/php/docs/reference/cloud-spanner/latest/Database#_Google_Cloud_Spanner_Database__transaction__). Transactions should\nalways call [Transaction::commit()](/php/docs/reference/cloud-spanner/latest/Transaction#_Google_Cloud_Spanner_Transaction__commit__) or\n[Transaction::rollback()](/php/docs/reference/cloud-spanner/latest/Transaction#_Google_Cloud_Spanner_Transaction__rollback__) to ensure that locks are\nreleased in a timely manner.\n\nIf you do not plan on performing any writes in your transaction, a\n[Snapshot](/php/docs/reference/cloud-spanner/latest/Snapshot) is a better solution which does not\nrequire a commit or rollback and does not lock any data.\n\nTransactions may raise [Google\\\\Cloud\\\\Core\\\\Exception\\\\AbortedException](https://cloud.google.com/php/docs/reference/cloud-core/latest/Exception.AbortedException.html) errors\nwhen the transaction cannot complete for any reason. In this case, the entire\noperation (all reads and writes) should be reapplied atomically. Google Cloud\nPHP handles this transparently when using\n[Database::runTransaction()](/php/docs/reference/cloud-spanner/latest/Database#_Google_Cloud_Spanner_Database__runTransaction__). In other cases, it is\nhighly recommended that applications implement their own retry logic.\n\nExample: \n\n use Google\\Cloud\\Spanner\\SpannerClient;\n\n $spanner = new SpannerClient();\n\n $database = $spanner-\u003econnect('my-instance', 'my-database');\n\n $database-\u003erunTransaction(function (Transaction $t) {\n // do stuff.\n\n $t-\u003ecommit();\n });\n\n // Get a transaction to manage manually.\n $transaction = $database-\u003etransaction();\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Spanner\n\nMethods\n-------\n\n### __construct\n\n### getCommitStats\n\nGet the commit stats for this transaction. Commit stats are only available after commit has been called with\n`return_commit_stats` =\\\u003e true. If commit is called multiple times, only the commitStats for the last commit will\nbe available.\n\nExample: \n\n $transaction-\u003ecommit([\"returnCommitStats\" =\u003e true]);\n $commitStats = $transaction-\u003egetCommitStats();\n\n### executeUpdate\n\nSee also:\n\n- [ExecuteSqlRequest](https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)\n- [DML Syntax Guide](https://cloud.google.com/spanner/docs/dml-syntax)\n\n### executeUpdateBatch\n\nSee also:\n\n- [ExecuteBatchDmlRequest](https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteBatchDmlRequest)\n\n### rollback\n\nRoll back a transaction.\n\nRolls back a transaction, releasing any locks it holds. It is a good idea\nto call this for any transaction that includes one or more Read or\nExecuteSql requests and ultimately decides not to commit.\n\nThis closes the transaction, preventing any future API calls inside it.\n\nRollback will NOT error if the transaction is not found or was already aborted.\n\nExample: \n\n $transaction-\u003erollback();\n\n### commit\n\nCommit and end the transaction.\n\nIt is advised that transactions be run inside\n[Database::runTransaction()](/php/docs/reference/cloud-spanner/latest/Database#_Google_Cloud_Spanner_Database__runTransaction__) in order to take\nadvantage of automated transaction retry in case of a transaction aborted\nerror.\n\nExample: \n\n $transaction-\u003ecommit();\n\n### state\n\nRetrieve the Transaction State.\n\nWill be one of `Transaction::STATE_ACTIVE`,\n`Transaction::STATE_COMMITTED`, or `Transaction::STATE_ROLLED_BACK`.\n\nExample: \n\n $state = $transaction-\u003estate();\n\n### isRetry\n\nCheck whether the current transaction is a retry transaction.\n\nWhen using [Database::runTransaction()](/php/docs/reference/cloud-spanner/latest/Database#_Google_Cloud_Spanner_Database__runTransaction__),\ntransactions are automatically retried when a conflict causes it to abort.\nIn such cases, subsequent invocations of the transaction callable will\nprovide a transaction where `$transaction-\u003eisRetry()` is true. This can\nbe useful for debugging and understanding how code is working.\n\nExample: \n\n if ($transaction-\u003eisRetry()) {\n echo 'This is a retry transaction!';\n }"]]