Skip to content

Allow returning values from transactional functions #3493

Closed
@waw-google

Description

@waw-google

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.

Metadata

Metadata

Labels

api: spannerIssues related to the Spanner API.priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions