Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 7da3a4f

Browse files
authored
Merge branch 'main' into propagate-x_goog_spanner_request_id-to-OTEL-span
2 parents 51ff812 + de322f8 commit 7da3a4f

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

docs/transaction-usage.rst

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ A :class:`~google.cloud.spanner_v1.transaction.Transaction` represents a
55
transaction: when the transaction commits, it will send any accumulated
66
mutations to the server.
77

8-
To understand more about how transactions work, visit [Transaction](https://cloud.google.com/spanner/docs/reference/rest/v1/Transaction).
8+
To understand more about how transactions work, visit
9+
`Transaction <https://cloud.google.com/spanner/docs/reference/rest/v1/Transaction>`_.
910
To learn more about how to use them in the Python client, continue reading.
1011

1112

@@ -90,8 +91,8 @@ any of the records already exists.
9091
Update records using a Transaction
9192
----------------------------------
9293

93-
:meth:`Transaction.update` updates one or more existing records in a table. Fails
94-
if any of the records does not already exist.
94+
:meth:`Transaction.update` updates one or more existing records in a table.
95+
Fails if any of the records does not already exist.
9596

9697
.. code:: python
9798
@@ -178,35 +179,40 @@ Using :meth:`~Database.run_in_transaction`
178179

179180
Rather than calling :meth:`~Transaction.commit` or :meth:`~Transaction.rollback`
180181
manually, you should use :meth:`~Database.run_in_transaction` to run the
181-
function that you need. The transaction's :meth:`~Transaction.commit` method
182+
function that you need. The transaction's :meth:`~Transaction.commit` method
182183
will be called automatically if the ``with`` block exits without raising an
183-
exception. The function will automatically be retried for
184+
exception. The function will automatically be retried for
184185
:class:`~google.api_core.exceptions.Aborted` errors, but will raise on
185186
:class:`~google.api_core.exceptions.GoogleAPICallError` and
186187
:meth:`~Transaction.rollback` will be called on all others.
187188

188189
.. code:: python
189190
190191
def _unit_of_work(transaction):
191-
192192
transaction.insert(
193-
'citizens', columns=['email', 'first_name', 'last_name', 'age'],
193+
'citizens',
194+
columns=['email', 'first_name', 'last_name', 'age'],
194195
values=[
195196
['phred@exammple.com', 'Phred', 'Phlyntstone', 32],
196197
['bharney@example.com', 'Bharney', 'Rhubble', 31],
197-
])
198+
]
199+
)
198200
199201
transaction.update(
200-
'citizens', columns=['email', 'age'],
202+
'citizens',
203+
columns=['email', 'age'],
201204
values=[
202205
['phred@exammple.com', 33],
203206
['bharney@example.com', 32],
204-
])
207+
]
208+
)
205209
206210
...
207211
208-
transaction.delete('citizens',
209-
keyset['bharney@example.com', 'nonesuch@example.com'])
212+
transaction.delete(
213+
'citizens',
214+
keyset=['bharney@example.com', 'nonesuch@example.com']
215+
)
210216
211217
db.run_in_transaction(_unit_of_work)
212218
@@ -242,7 +248,7 @@ If an exception is raised inside the ``with`` block, the transaction's
242248
...
243249
244250
transaction.delete('citizens',
245-
keyset['bharney@example.com', 'nonesuch@example.com'])
251+
keyset=['bharney@example.com', 'nonesuch@example.com'])
246252
247253
248254
Begin a Transaction

tests/mockserver_tests/test_basics.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ def test_dbapi_partitioned_dml(self):
9393
TransactionOptions(dict(partitioned_dml={})), begin_request.options
9494
)
9595

96+
def test_batch_create_sessions_unavailable(self):
97+
add_select1_result()
98+
add_error(SpannerServicer.BatchCreateSessions.__name__, unavailable_status())
99+
with self.database.snapshot() as snapshot:
100+
results = snapshot.execute_sql("select 1")
101+
result_list = []
102+
for row in results:
103+
result_list.append(row)
104+
self.assertEqual(1, row[0])
105+
self.assertEqual(1, len(result_list))
106+
requests = self.spanner_service.requests
107+
self.assertEqual(3, len(requests), msg=requests)
108+
# The BatchCreateSessions call should be retried.
109+
self.assertTrue(isinstance(requests[0], BatchCreateSessionsRequest))
110+
self.assertTrue(isinstance(requests[1], BatchCreateSessionsRequest))
111+
self.assertTrue(isinstance(requests[2], ExecuteSqlRequest))
112+
96113
def test_execute_streaming_sql_unavailable(self):
97114
add_select1_result()
98115
# Add an UNAVAILABLE error that is returned the first time the

0 commit comments

Comments
 (0)