Skip to content

transactional rollback doesn't seem to be working #398

@maininformer

Description

@maininformer

Hello, I hope you are having a great Thursday.

In the old NDB, when there was an exception in a transaction, a rollback was applied, as one does. In this new NDB, even though I see the a call to rollback it does not seem to take effect.

In the following stand alone file, I expect the second transaction to be rolled back, but it does not, after the exception I see the model updated in the datastore.

from google.cloud import ndb

class Library(ndb.Model):
    books = ndb.JsonProperty()


@ndb.transactional()
def add_book(library_id, book_name, pages):
    library = Library.get_by_id(library_id)
    if not library.books:
        library.books = {}
    library.books[book_name] = pages
    library.put()

class LeRandomException(Exception):
    pass

@ndb.transactional()
def add_book_with_exception(library_id, book_name, pages):
    library = Library.get_by_id(library_id)
    library.books[book_name] = pages
    raise LeRandomException


if __name__ == '__main__':
    with ndb.Client().context():
        lib = Library()
        key = lib.put()
        add_book(key.id(), 'behave', 'so many')

        assert Library.get_by_id(key.id()).books['behave'] == 'so many'

        try:
            add_book_with_exception(key.id(), 'hooked', 'less than behave')
        except LeRandomException:
            books = Library.get_by_id(key.id()).books
            assert books.get('hooked', None) is None, books

version is 1.2.0

Metadata

Metadata

Assignees

Labels

🚨This issue needs some love.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