Skip to content

Commit 4c18e27

Browse files
authored
Spanner: run in transaction documentation (googleapis#4742)
1 parent 73b48d6 commit 4c18e27

1 file changed

Lines changed: 70 additions & 25 deletions

File tree

docs/spanner/transaction-usage.rst

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,43 +171,50 @@ Non-existent rows do not cause errors.
171171
'citizens', keyset=['bharney@example.com', 'nonesuch@example.com'])
172172
173173
174-
Commit changes for a Transaction
175-
--------------------------------
176-
177-
After describing the modifications to be made to table data via the
178-
:meth:`Transaction.insert`, :meth:`Transaction.update`,
179-
:meth:`Transaction.insert_or_update`, :meth:`Transaction.replace`, and
180-
:meth:`Transaction.delete` methods above, send them to
181-
the back-end by calling :meth:`Transaction.commit`, which makes the ``Commit``
182-
API call.
174+
Using :meth:`~Database.run_in_transaction`
175+
------------------------------------------
176+
177+
Rather than calling :meth:`~Transaction.commit` or :meth:`~Transaction.rollback`
178+
manually, you should use :meth:`~Database.run_in_transaction` to run the
179+
function that you need. The transaction's :meth:`~Transaction.commit` method
180+
will be called automatically if the ``with`` block exits without raising an
181+
exception. The function will automatically be retried for
182+
:class:`~google.api_core.exceptions.Aborted` errors, but will raise on
183+
:class:`~google.api_core.exceptions.GoogleAPICallError` and
184+
:meth:`~Transaction.rollback` will be called on all others.
183185

184186
.. code:: python
185187
186-
transaction.commit()
187-
188+
def _unit_of_work(transaction):
188189
189-
Roll back changes for a Transaction
190-
-----------------------------------
190+
transaction.insert(
191+
'citizens', columns=['email', 'first_name', 'last_name', 'age'],
192+
values=[
193+
['phred@exammple.com', 'Phred', 'Phlyntstone', 32],
194+
['bharney@example.com', 'Bharney', 'Rhubble', 31],
195+
])
191196
192-
After describing the modifications to be made to table data via the
193-
:meth:`Transaction.insert`, :meth:`Transaction.update`,
194-
:meth:`Transaction.insert_or_update`, :meth:`Transaction.replace`, and
195-
:meth:`Transaction.delete` methods above, cancel the transaction on the
196-
the back-end by calling :meth:`Transaction.rollback`, which makes the
197-
``Rollback`` API call.
197+
transaction.update(
198+
'citizens', columns=['email', 'age'],
199+
values=[
200+
['phred@exammple.com', 33],
201+
['bharney@example.com', 32],
202+
])
198203
199-
.. code:: python
204+
...
200205
201-
transaction.rollback()
206+
transaction.delete('citizens',
207+
keyset['bharney@example.com', 'nonesuch@example.com'])
208+
209+
db.run_in_transaction(_unit_of_work)
202210
203211
204212
Use a Transaction as a Context Manager
205213
--------------------------------------
206214

207-
Rather than calling :meth:`Transaction.commit` or :meth:`Transaction.rollback`
208-
manually, you should use the :class:`Transaction` instance as a context manager.
209-
The transaction's :meth:`~Transaction.commit` method will be called automatically
210-
if the ``with`` block exits without raising an exception.
215+
Alternatively, you can use the :class:`Transaction` instance as a context
216+
manager. The transaction's :meth:`~Transaction.commit` method will be called
217+
automatically if the ``with`` block exits without raising an exception.
211218

212219
If an exception is raised inside the ``with`` block, the transaction's
213220
:meth:`~Transaction.rollback` method will automatically be called.
@@ -234,3 +241,41 @@ If an exception is raised inside the ``with`` block, the transaction's
234241
235242
transaction.delete('citizens',
236243
keyset['bharney@example.com', 'nonesuch@example.com'])
244+
245+
246+
Commit changes for a Transaction
247+
--------------------------------
248+
249+
This function should not be used manually. Rather, should consider using
250+
:meth:`~Database.run_in_transaction` or the context manager as described
251+
above.
252+
253+
After modifications to be made to table data via the
254+
:meth:`Transaction.insert`, :meth:`Transaction.update`,
255+
:meth:`Transaction.insert_or_update`, :meth:`Transaction.replace`, and
256+
:meth:`Transaction.delete` methods above, send them to
257+
the back-end by calling :meth:`Transaction.commit`, which makes the ``Commit``
258+
API call.
259+
260+
.. code:: python
261+
262+
transaction.commit()
263+
264+
265+
Roll back changes for a Transaction
266+
-----------------------------------
267+
268+
This function should not be used manually. Rather, should consider using
269+
:meth:`~Database.run_in_transaction` or the context manager as described
270+
above.
271+
272+
After describing the modifications to be made to table data via the
273+
:meth:`Transaction.insert`, :meth:`Transaction.update`,
274+
:meth:`Transaction.insert_or_update`, :meth:`Transaction.replace`, and
275+
:meth:`Transaction.delete` methods above, cancel the transaction on the
276+
the back-end by calling :meth:`Transaction.rollback`, which makes the
277+
``Rollback`` API call.
278+
279+
.. code:: python
280+
281+
transaction.rollback()

0 commit comments

Comments
 (0)