GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Transaction does not get rolled back if exception happens during commit. · Issue #457 · googleapis/google-cloud-python

Transaction does not get rolled back if exception happens during commit. #457

New issue
Copy link
New issue
Copy link
Closed
Closed
Transaction does not get rolled back if exception happens during commit.#457
Copy link
Assignees
dhermes
Labels
🚨This issue needs some love.api: datastoreIssues related to the Datastore API.triage meI really want to be triaged.

Description

@dhermes
dhermes
opened on Dec 23, 2014
Issue body actions

For example:

# >>> entity.kind()
# 'Knd'
with datastore.transaction.Transaction():
    # entity1 has an incomplete key, hence the delete fails
    entity1.delete()
# Exception: Request failed. Error was: Key path element must not be incomplete: [Knd: ]

with datastore.transaction.Transaction():
    entity2.save()
# ValueError: Cannot start a transaction with another already in progress. 

Maybe this isn't such a big deal since most code will stop working after the first uncaught exception? The only reason I noticed is because using a test-runner, the exception is caught and printed, but the process just moves on to the next.

Activity

Next
  1. dhermes
    added
    api: datastoreIssues related to the Datastore API.
    on Dec 23, 2014
  2. dhermes
    changed the title [-]Transaction does not complete if exception happens during transaction commit.[/-] [+]Transaction does not get rolled back if exception happens during commit.[/+] on Dec 23, 2014
  3. tseaver commented on Jan 6, 2015

    @tseaver
    tseaver
    on Jan 6, 2015
    Contributor
    More actions

    Hmm, list(query.fetch()) is returning empty, while list(query.fetch(limit=100)) is returning 3 posts.

    DJH NOTE: This was in reference to #488

  4. dhermes commented on Jan 6, 2015

    @dhermes
    dhermes
    on Jan 6, 2015
    ContributorAuthor
    More actions

    I think 0 is different than not setting in datastore_pb.Query.

    DJH NOTE: This was in reference to #488

  5. dhermes commented on Jan 7, 2015

    @dhermes
    dhermes
    on Jan 7, 2015
    ContributorAuthor
    More actions

    I just realized this was filed and closed as #136. However, I think it should be addressed as discussed by Tres.

  6. dhermes commented on Jan 9, 2015

    @dhermes
    dhermes
    on Jan 9, 2015
    ContributorAuthor
    More actions

    @tseaver This will probably be fixed as part of #514. This only causes failure of future code because the cleanup

    self.connection.transaction(None)

    doesn't get a chance to occur either in Transaction.commit() or Transaction.rollback().

    Once we rely on Batch.__exit__ to pop the current Transaction off the stack in finally:, this bug should go away.

    However, it's unclear to me if we need to rollback() a transaction which fails on commit(). It seems like we should, but maybe it doesn't matter.


    Working code to re-create this bug (as of 0525530):

    >>> from gcloud import datastore
    >>> from gcloud.datastore import _implicit_environ
    >>> from gcloud.datastore.key import Key
    >>> from gcloud.datastore.transaction import Transaction
    >>>
    >>> print _implicit_environ.CONNECTION
    None
    >>>
    >>> datastore.set_default_connection()
    >>> datastore.set_default_dataset_id('foo')
    >>>
    >>> print _implicit_environ.CONNECTION
    <gcloud.datastore.connection.Connection object at 0x7f64c2577190>
    >>> print _implicit_environ.CONNECTION.transaction()
    None
    >>>
    >>> with Transaction():
    ...     partial_key = Key('Knd')
    ...     partial_key.delete()
    ...
    Traceback (most recent call last):
      File "<stdin>", line 3, in <module>
      File "gcloud/datastore/batch.py", line 277, in __exit__
        self.commit()
      File "gcloud/datastore/transaction.py", line 182, in commit
        super(Transaction, self).commit()
      File "gcloud/datastore/batch.py", line 252, in commit
        response = self.connection.commit(self._dataset_id, self.mutation)
      File "gcloud/datastore/connection.py", line 376, in commit
        datastore_pb.CommitResponse)
      File "gcloud/datastore/connection.py", line 100, in _rpc
        data=request_pb.SerializeToString())
      File "gcloud/datastore/connection.py", line 77, in _request
        raise six.moves.http_client.HTTPException(message)
    httplib.HTTPException: Request failed with status code 400. Error was: Key path element must not be incomplete: [Knd: ]
    >>> print _implicit_environ.CONNECTION
    <gcloud.datastore.connection.Connection object at 0x7f64c2577190>
    >>> print _implicit_environ.CONNECTION.transaction()
    <gcloud.datastore.transaction.Transaction object at 0x7f64c2464510>
  7. tseaver commented on Jan 12, 2015

    @tseaver
    tseaver
    on Jan 12, 2015
    Contributor
    More actions

    If the back-end returns an error in our call to its commit, I don't think we should be rolling anything back: the "token" for the transaction is already known to be invalid on their side, which is the only reason to call their rollback.

  8. dhermes commented on Jan 12, 2015

    @dhermes
    dhermes
    on Jan 12, 2015
    ContributorAuthor
    More actions

    I'm not clear why the "token" for the transaction is known to be valid. I'd like to get input from someone on the datastore team to see if rollback is necessary / a "best practice" in those cases.

  9. tseaver commented on Jan 12, 2015

    @tseaver
    tseaver
    on Jan 12, 2015
    Contributor
    More actions

    rollback exists to allow the application to signal the back-end that it will never call commit, and therefore that the back-end can release any resources for that transaction.

    I cannot see any possible way to reuse the opaque transaction field (returned in beginTransaction) after commit API using it fails. The back-end folks will have torn down / released anything they allocated in beginTransaction when the failed the commit, and don't need us to tell them to do so by calling rollback (in fact, I would bet that rollback after a failed commit would error out).

  10. dhermes commented on Jan 12, 2015

    @dhermes
    dhermes
    on Jan 12, 2015
    ContributorAuthor
    More actions

    I wasn't sure that

    will have torn down / released anything they allocated in beginTransaction when
    they failed the commit

    is actually true. I just tried to commit twice and it clearly fails.

    I was "distracted" by the fact that a transaction can have multiple runQuery calls (i.e. read options are set in a query).

  11. dhermes commented on Jan 12, 2015

    @dhermes
    dhermes
    on Jan 12, 2015
    ContributorAuthor
    More actions

    To summarize all that just happened, the only thing left to fix this bug is the removal of

    self.connection.transaction(None)

    which will happen by the end of #514.

  12. dhermes commented on Jan 15, 2015

    @dhermes
    dhermes
    on Jan 15, 2015
    ContributorAuthor
    More actions

    Fixed as of #550

  13. dhermes
    closed this as completedon Jan 15, 2015
  14. jgeewax
    modified the milestone: This milestone has been deleted on Jan 30, 2015
  15. 17 remaining items

  16. parthea
    added a commit that references this issue on Aug 21, 2025

    docs: fix changelog header to consistent size (#457)

    8b00326
  17. parthea
    added 2 commits that reference this issue on Sep 16, 2025

    docs: fix changelog header to consistent size (#457)

    a29275a

    chore: use python-samples-reviewers (#457)

    ecb9d10
  18. parthea
    added 9 commits that reference this issue on Nov 24, 2025

    chore: release 0.24.2 (#457)

    ccaa049

    fix(deps): allow protobuf 5.x (#457)

    540b415

    chore: update docfx minimum Python version (#457)

    ee757d2

    chore: use gapic-generator-python 0.51.2 (#457)

    dfaf4a2

    chore: update .repo-metadata.json (#457)

    156867d

    feat: convert grpc errors to api core exceptions (#457)

    85b850f

    chore: update .repo-metadata.json (#457)

    653d086

    feat: add mTLS ADC support for HTTP (#457)

    dce9fc6

    chore: release 1.12.0 (#463)

    8ada9dc
  19. parthea
    added a commit that references this issue on Mar 2, 2026

    chore: update staging bucket to dev (#457)

    051dc78
  20. parthea
    added a commit that references this issue on Mar 9, 2026

    chore(deps): update dependency typing-extensions to v4.12.2 (#457)

    7cfd1aa
  21. ohmayr
    added a commit that references this issue on Mar 25, 2026

    build(deps): Bump django from 4.2.25 to 4.2.29 in /.kokoro (#457)

    86aa42f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dhermes
    dhermes

Labels

🚨This issue needs some love.api: datastoreIssues related to the Datastore API.triage meI really want to be triaged.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.