Reference documentation and code samples for the Cloud Spanner V1 API module Google::Cloud::Spanner::V1::ReadRequest::LockHint.
A lock hint mechanism for reads done within a transaction.
Constants
LOCK_HINT_UNSPECIFIED
value: 0 Default value.
LOCK_HINT_UNSPECIFIED is equivalent to LOCK_HINT_SHARED.
LOCK_HINT_SHARED
value: 1 Acquire shared locks.
By default when you perform a read as part of a read-write transaction,
Spanner acquires shared read locks, which allows other reads to still
access the data until your transaction is ready to commit. When your
transaction is committing and writes are being applied, the transaction
attempts to upgrade to an exclusive lock for any data you are writing.
For more information about locks, see Lock
modes.
LOCK_HINT_EXCLUSIVE
value: 2 Acquire exclusive locks.
Requesting exclusive locks is beneficial if you observe high write
contention, which means you notice that multiple transactions are
concurrently trying to read and write to the same data, resulting in a
large number of aborts. This problem occurs when two transactions
initially acquire shared locks and then both try to upgrade to exclusive
locks at the same time. In this situation both transactions are waiting
for the other to give up their lock, resulting in a deadlocked situation.
Spanner is able to detect this occurring and force one of the
transactions to abort. However, this is a slow and expensive operation
and results in lower performance. In this case it makes sense to acquire
exclusive locks at the start of the transaction because then when
multiple transactions try to act on the same data, they automatically get
serialized. Each transaction waits its turn to acquire the lock and
avoids getting into deadlock situations.
Because the exclusive lock hint is just a hint, it should not be
considered equivalent to a mutex. In other words, you should not use
Spanner exclusive locks as a mutual exclusion mechanism for the execution
of code outside of Spanner.
Note: Request exclusive locks judiciously because they block others
from reading that data for the entire transaction, rather than just when
the writes are being performed. Unless you observe high write contention,
you should use the default of shared read locks so you don't prematurely
block other clients from reading the data that you're writing to.
[[["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-08-28 UTC."],[],[],null,["# Cloud Spanner V1 API - Module Google::Cloud::Spanner::V1::ReadRequest::LockHint (v1.10.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.10.0 (latest)](/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.9.1](/ruby/docs/reference/google-cloud-spanner-v1/1.9.1/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.8.0](/ruby/docs/reference/google-cloud-spanner-v1/1.8.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.7.1](/ruby/docs/reference/google-cloud-spanner-v1/1.7.1/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.6.0](/ruby/docs/reference/google-cloud-spanner-v1/1.6.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.5.0](/ruby/docs/reference/google-cloud-spanner-v1/1.5.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.4.0](/ruby/docs/reference/google-cloud-spanner-v1/1.4.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.3.0](/ruby/docs/reference/google-cloud-spanner-v1/1.3.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.2.0](/ruby/docs/reference/google-cloud-spanner-v1/1.2.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.1.0](/ruby/docs/reference/google-cloud-spanner-v1/1.1.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [1.0.2](/ruby/docs/reference/google-cloud-spanner-v1/1.0.2/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.27.0](/ruby/docs/reference/google-cloud-spanner-v1/0.27.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.26.0](/ruby/docs/reference/google-cloud-spanner-v1/0.26.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.25.0](/ruby/docs/reference/google-cloud-spanner-v1/0.25.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.24.0](/ruby/docs/reference/google-cloud-spanner-v1/0.24.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.23.0](/ruby/docs/reference/google-cloud-spanner-v1/0.23.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.22.2](/ruby/docs/reference/google-cloud-spanner-v1/0.22.2/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.21.0](/ruby/docs/reference/google-cloud-spanner-v1/0.21.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.20.1](/ruby/docs/reference/google-cloud-spanner-v1/0.20.1/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.19.0](/ruby/docs/reference/google-cloud-spanner-v1/0.19.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.18.0](/ruby/docs/reference/google-cloud-spanner-v1/0.18.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.17.0](/ruby/docs/reference/google-cloud-spanner-v1/0.17.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.16.0](/ruby/docs/reference/google-cloud-spanner-v1/0.16.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.15.1](/ruby/docs/reference/google-cloud-spanner-v1/0.15.1/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.14.1](/ruby/docs/reference/google-cloud-spanner-v1/0.14.1/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.13.0](/ruby/docs/reference/google-cloud-spanner-v1/0.13.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.12.0](/ruby/docs/reference/google-cloud-spanner-v1/0.12.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.11.0](/ruby/docs/reference/google-cloud-spanner-v1/0.11.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.10.0](/ruby/docs/reference/google-cloud-spanner-v1/0.10.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.9.0](/ruby/docs/reference/google-cloud-spanner-v1/0.9.0/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.8.1](/ruby/docs/reference/google-cloud-spanner-v1/0.8.1/Google-Cloud-Spanner-V1-ReadRequest-LockHint)\n- [0.7.4](/ruby/docs/reference/google-cloud-spanner-v1/0.7.4/Google-Cloud-Spanner-V1-ReadRequest-LockHint) \nReference documentation and code samples for the Cloud Spanner V1 API module Google::Cloud::Spanner::V1::ReadRequest::LockHint.\n\nA lock hint mechanism for reads done within a transaction.\n\nConstants\n---------\n\n### LOCK_HINT_UNSPECIFIED\n\n**value:** 0 \nDefault value.\n\n\n\u003cbr /\u003e\n\nLOCK_HINT_UNSPECIFIED is equivalent to LOCK_HINT_SHARED.\n\n### LOCK_HINT_SHARED\n\n**value:** 1 \nAcquire shared locks.\n\n\n\u003cbr /\u003e\n\nBy default when you perform a read as part of a read-write transaction,\nSpanner acquires shared read locks, which allows other reads to still\naccess the data until your transaction is ready to commit. When your\ntransaction is committing and writes are being applied, the transaction\nattempts to upgrade to an exclusive lock for any data you are writing.\nFor more information about locks, see [Lock\nmodes](https://cloud.google.com/spanner/docs/introspection/lock-statistics#explain-lock-modes).\n\n### LOCK_HINT_EXCLUSIVE\n\n**value:** 2 \nAcquire exclusive locks.\n\n\nRequesting exclusive locks is beneficial if you observe high write\ncontention, which means you notice that multiple transactions are\nconcurrently trying to read and write to the same data, resulting in a\nlarge number of aborts. This problem occurs when two transactions\ninitially acquire shared locks and then both try to upgrade to exclusive\nlocks at the same time. In this situation both transactions are waiting\nfor the other to give up their lock, resulting in a deadlocked situation.\nSpanner is able to detect this occurring and force one of the\ntransactions to abort. However, this is a slow and expensive operation\nand results in lower performance. In this case it makes sense to acquire\nexclusive locks at the start of the transaction because then when\nmultiple transactions try to act on the same data, they automatically get\nserialized. Each transaction waits its turn to acquire the lock and\navoids getting into deadlock situations.\n\nBecause the exclusive lock hint is just a hint, it should not be\nconsidered equivalent to a mutex. In other words, you should not use\nSpanner exclusive locks as a mutual exclusion mechanism for the execution\nof code outside of Spanner.\n\n\u003cbr /\u003e\n\n**Note:** Request exclusive locks judiciously because they block others\nfrom reading that data for the entire transaction, rather than just when\nthe writes are being performed. Unless you observe high write contention,\nyou should use the default of shared read locks so you don't prematurely\nblock other clients from reading the data that you're writing to."]]