Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #136: roll back transactions on exceptions. #187

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions gcloud/datastore/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def test_context_manager_no_raise(self):
self.assertEqual(xact.id(), None)

def test_context_manager_w_raise(self):
# See https://github.com/GoogleCloudPlatform/gcloud-python/issues/136
class Foo(Exception):
pass
_DATASET = 'DATASET'
Expand All @@ -127,13 +126,10 @@ class Foo(Exception):
self.assertTrue(connection._xact is xact)
raise Foo()
except Foo:
pass # XXX
# self.assertEqual(xact.id(), None)
# self.assertEqual(connection._rolled_back, _DATASET))
# self.assertEqual(connection._xact, None)
# XXX should *not* have committed
self.assertEqual(connection._committed, (_DATASET, mutation))
# self.assertEqual(connection._committed, None)
self.assertEqual(xact.id(), None)
self.assertEqual(connection._rolled_back, _DATASET)
self.assertEqual(connection._xact, None)
self.assertEqual(connection._committed, None)
self.assertTrue(connection._xact is None)
self.assertEqual(xact.id(), None)

Expand Down
16 changes: 8 additions & 8 deletions gcloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ class Transaction(object):
... entity1.save()
... entity2.save()

To rollback a transaction if there is an error::
By default, the transaction is rolled back is an error::

>>> from gcloud import datastore
>>> dataset = datastore.get_dataset('dataset-id', email, key_path)
>>> with dataset.transaction() as t:
... try:
... do_some_work()
... entity1.save()
... except:
... t.rollback()
... do_some_work()
... raise Exception() # rolls back

This comment was marked as spam.


If the transaction isn't rolled back,
If the transaction block exists without an exception,
it will commit by default.

.. warning::
Expand Down Expand Up @@ -249,4 +246,7 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.commit()
if exc_type is None:
self.commit()
else:
self.rollback()