8000 Merge pull request #595 from dhermes/fix-496 · googleapis/google-cloud-python@d07958b · GitHub
[go: up one dir, main page]

Skip to content

Commit d07958b

Browse files
committed
Merge pull request #595 from dhermes/fix-496
Tracking status of Transaction as well as success / failure.
2 parents 204389a + ed0762b commit d07958b

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

gcloud/datastore/test_transaction.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def test_ctor(self):
4747
self.assertEqual(xact.dataset_id, _DATASET)
4848
self.assertEqual(xact.connection, connection)
4949
self.assertEqual(xact.id, None)
50+
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
5051
self.assertTrue(isinstance(xact.mutation, Mutation))
5152
self.assertEqual(len(xact._auto_id_entities), 0)
5253

@@ -64,6 +65,7 @@ def test_ctor_with_env(self):
6465
self.assertEqual(xact.id, None)
6566
self.assertEqual(xact.dataset_id, DATASET_ID)
6667
self.assertEqual(xact.connection, CONNECTION)
68+
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
6769

6870
def test_current(self):
6971
from gcloud.datastore.test_api import _NoCommitBatch
@@ -98,6 +100,19 @@ def test_begin(self):
98100
self.assertEqual(xact.id, 234)
99101
self.assertEqual(connection._begun, _DATASET)
100102

103+
def test_begin_tombstoned(self):
104+
_DATASET = 'DATASET'
105+
connection = _Connection(234)
106+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
107+
xact.begin()
108+
self.assertEqual(xact.id, 234)
109+
self.assertEqual(connection._begun, _DATASET)
110+
111+
xact.rollback()
112+
self.assertEqual(xact.id, None)
113+
114+
self.assertRaises(ValueError, xact.begin)
115+
101116
def test_rollback(self):
102117
_DATASET = 'DATASET'
103118
connection = _Connection(234)

gcloud/datastore/transaction.py

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ class Transaction(Batch):
3232
3333
>>> datastore.set_defaults()
3434
35-
>>> with Transaction() as xact:
36-
... datastore.put(entity1)
37-
... datastore.put(entity2)
35+
>>> with Transaction():
36+
... datastore.put([entity1, entity2])
3837
3938
Because it derives from :class:`Batch`, :class`Transaction` also provides
4039
:meth:`put` and :meth:`delete` methods::
@@ -46,7 +45,7 @@ class Transaction(Batch):
4645
By default, the transaction is rolled back if the transaction block
4746
exits with an error::
4847
49-
>>> with Transaction() as txn:
48+
>>> with Transaction():
5049
... do_some_work()
5150
... raise SomeException() # rolls back
5251
@@ -71,16 +70,34 @@ class Transaction(Batch):
7170
... entity = Entity(key=Key('Thing'))
7271
... datastore.put([entity])
7372
... assert entity.key.is_partial # There is no ID on this key.
73+
...
7474
>>> assert not entity.key.is_partial # There *is* an ID.
7575
76+
After completion, you can determine if a commit succeeded or failed.
77+
For example, trying to delete a key that doesn't exist::
78+
79+
>>> with Transaction() as xact:
80+
... xact.delete(key)
81+
...
82+
>>> xact.succeeded
83+
False
84+
85+
or successfully storing two entities:
86+
87+
>>> with Transaction() as xact:
88+
... datastore.put([entity1, entity2])
89+
...
90+
>>> xact.succeeded
91+
True
92+
7693
If you don't want to use the context manager you can initialize a
7794
transaction manually::
7895
7996
>>> transaction = Transaction()
8097
>>> transaction.begin()
8198
8299
>>> entity = Entity(key=Key('Thing'))
83-
>>> transaction.put([entity])
100+
>>> transaction.put(entity)
84101
85102
>>> if error:
86103
... transaction.rollback()
@@ -97,9 +114,22 @@ class Transaction(Batch):
97114
are not set.
98115
"""
99116

117+
_INITIAL = 0
118+
"""Enum value for _INITIAL status of transaction."""
119+
120+
_IN_PROGRESS = 1
121+
"""Enum value for _IN_PROGRESS status of transaction."""
122+
123+
_ABORTED = 2
124+
"""Enum value for _ABORTED status of transaction."""
125+
126+
_FINISHED = 3
127+
"""Enum value for _FINISHED status of transaction."""
128+
100129
def __init__(self, dataset_id=None, connection=None):
101130
super(Transaction, self).__init__(dataset_id, connection)
102131
self._id = None
132+
self._status = self._INITIAL
103133

104134
@property
105135
def id(self):
@@ -129,7 +159,12 @@ def begin(self):
129159
This method is called automatically when entering a with
130160
statement, however it can be called explicitly if you don't want
131161
to use a context manager.
162+
163+
:raises: :class:`ValueError` if the transaction has already begun.
132164
"""
165+
if self._status != self._INITIAL:
166+
raise ValueError('Transaction already started previously.')
167+
self._status = self._IN_PROGRESS
133168
self._id = self.connection.begin_transaction(self._dataset_id)
134169

135170
def rollback(self):
@@ -140,8 +175,12 @@ def rollback(self):
140175
- Sets the current connection's transaction reference to None.
141176
- Sets the current transaction's ID to None.
142177
"""
143-
self.connection.rollback(self._dataset_id, self._id)
144-
self._id = None
178+
try:
179+
self.connection.rollback(self._dataset_id, self._id)
180+
finally:
181+
self._status = self._ABORTED
182+
# Clear our own ID in case this gets accidentally reused.
183+
self._id = None
145184

146185
def commit(self):
147186
"""Commits the transaction.
@@ -154,7 +193,9 @@ def commit(self):
154193
155194
- Sets the current transaction's ID to None.
156195
"""
157-
super(Transaction, self).commit()
158-
159-
# Clear our own ID in case this gets accidentally reused.
160-
self._id = None
196+
try:
197+
super(Transaction, self).commit()
198+
finally:
199+
self._status = self._FINISHED
200+
# Clear our own ID in case this gets accidentally reused.
201+
self._id = None

0 commit comments

Comments
 (0)
0