Closed
Description
Somebody asks in the Slack:
Question about spanner python APIs, specifically transactions.
In a read-write transaction, is it possible to return the updated
value without hitting the database again?let's say I have the following transaction (pretty much the example from the docs)
second_album_result = transaction.read( table='Albums', columns=('MarketingBudget',), keyset=second_album_keyset, limit=1) second_album_row = list(second_album_result)[0] second_album_budget = second_album_row[0] second_album_budget += 10000000 transaction.update(table=mytable, columns=('MarketingBudget'), keyset=second_album_keyset, values=[(second_album_budget)] ) return second_album_budget database.run_in_transaction(f)
is the
return
at all possible?
I believe that this is currently not possible in the Python library, because when you pass a closure to run_in_transaction
, the return value always gets ignored, and it just returns the commit timestamp.
However this is possible in the Cloud Spanner Java library, and being be able to return whatever you want from the body of a transactional function in the case that a transaction successfully commits is extremely useful.