Skip to content

Commit

Permalink
No longer store the transaction on the connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Jan 14, 2015
1 parent ffe489b commit c449af6
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 66 deletions.
17 changes: 0 additions & 17 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,6 @@ def build_api_url(cls, dataset_id, method, base_url=None,
api_version=(api_version or cls.API_VERSION),
dataset_id=dataset_id, method=method)

def transaction(self, transaction=connection.Connection._EMPTY):
"""Getter/setter for the connection's transaction object.
:type transaction: :class:`gcloud.datastore.transaction.Transaction`,
(setting), or omitted (getting).
:param transaction: The new transaction (if passed).
:rtype: :class:`gcloud.datastore.transaction.Transaction`, (getting)
or :class:`gcloud.datastore.connection.Connection` (setting)
:returns: The current transaction (getting) or self (setting).
"""
if transaction is self._EMPTY:
return self._current_transaction
else:
self._current_transaction = transaction
return self

def lookup(self, dataset_id, key_pbs,
missing=None, deferred=None, eventual=False):
"""Lookup keys from a dataset in the Cloud Datastore.
Expand Down
1 change: 0 additions & 1 deletion gcloud/datastore/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ def test_implicit_connection(self):
entity = _Entity(foo=u'bar')
key = entity.key = _Key(_DATASET)


with _Monkey(_implicit_environ, CONNECTION=connection):
# Set up Batch on stack so we can check it is used.
with _NoCommitBatch(_DATASET, connection) as CURR_BATCH:
Expand Down
10 changes: 0 additions & 10 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ def test_build_api_url_w_explicit_base_version(self):
self.assertEqual(klass.build_api_url(DATASET_ID, METHOD, BASE, VER),
URI)

def test_transaction_getter_unset(self):
conn = self._makeOne()
self.assertTrue(conn.transaction() is None)

def test_transaction_setter(self):
xact = object()
conn = self._makeOne()
self.assertTrue(conn.transaction(xact) is conn)
self.assertTrue(conn.transaction() is xact)

def test_lookup_single_key_empty_response(self):
from gcloud.datastore import datastore_v1_pb2 as datastore_pb

Expand Down
28 changes: 1 addition & 27 deletions gcloud/datastore/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def test_begin(self):
xact.begin()
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun, _DATASET)
self.assertTrue(connection._xact is xact)

def test_rollback(self):
_DATASET = 'DATASET'
Expand All @@ -82,7 +81,6 @@ def test_rollback(self):
xact.rollback()
self.assertEqual(xact.id, None)
self.assertEqual(connection._rolled_back, _DATASET)
self.assertEqual(connection._xact, None)

def test_commit_no_auto_ids(self):
_DATASET = 'DATASET'
Expand All @@ -92,7 +90,6 @@ def test_commit_no_auto_ids(self):
xact.begin()
xact.commit()
self.assertEqual(connection._committed, (_DATASET, mutation))
self.assertTrue(connection._xact is None)
self.assertEqual(xact.id, None)

def test_commit_w_auto_ids(self):
Expand All @@ -109,22 +106,9 @@ def test_commit_w_auto_ids(self):
xact.begin()
xact.commit()
self.assertEqual(connection._committed, (_DATASET, mutation))
self.assertTrue(connection._xact is None)
self.assertEqual(xact.id, None)
self.assertEqual(entity.key.path, [{'kind': _KIND, 'id': _ID}])

def test_commit_w_already(self):
_DATASET = 'DATASET'
connection = _Connection(234)
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
xact._mutation = object()
xact.begin()
connection.transaction(()) # Simulate previous commit via false-ish.
xact.commit()
self.assertEqual(connection._committed, None)
self.assertTrue(connection._xact is None)
self.assertEqual(xact.id, None)

def test_context_manager_no_raise(self):
_DATASET = 'DATASET'
connection = _Connection(234)
Expand All @@ -133,9 +117,7 @@ def test_context_manager_no_raise(self):
with xact:
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun, _DATASET)
self.assertTrue(connection._xact is xact)
self.assertEqual(connection._committed, (_DATASET, mutation))
self.assertTrue(connection._xact is None)
self.assertEqual(xact.id, None)

def test_context_manager_w_raise(self):
Expand All @@ -149,14 +131,11 @@ class Foo(Exception):
with xact:
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun, _DATASET)
self.assertTrue(connection._xact is xact)
raise Foo()
except Foo:
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 All @@ -173,17 +152,12 @@ def _make_key(kind, id, dataset_id):

class _Connection(object):
_marker = object()
_begun = _rolled_back = _committed = _xact = None
_begun = _rolled_back = _committed = None

def __init__(self, xact_id=123):
self._xact_id = xact_id
self._commit_result = _CommitResult()

def transaction(self, xact=_marker):
if xact is self._marker:
return self._xact
self._xact = xact

def begin_transaction(self, dataset_id):
self._begun = dataset_id
return self._xact_id
Expand Down
12 changes: 1 addition & 11 deletions gcloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def begin(self):
to use a context manager.
"""
self._id = self.connection.begin_transaction(self._dataset_id)
self.connection.transaction(self)

def rollback(self):
"""Rolls back the current transaction.
Expand All @@ -129,7 +128,6 @@ def rollback(self):
- Sets the current transaction's ID to None.
"""
self.connection.rollback(self._dataset_id)
self.connection.transaction(None)
self._id = None

def commit(self):
Expand All @@ -141,17 +139,9 @@ def commit(self):
This method has necessary side-effects:
- Sets the current connection's transaction reference to None.
- Sets the current transaction's ID to None.
- Updates paths for any keys that needed an automatically generated ID.
"""
# It's possible that they called commit() already, in which case
# we shouldn't do any committing of our own.
if self.connection.transaction():
super(Transaction, self).commit()

# Tell the connection that the transaction is over.
self.connection.transaction(None)
super(Transaction, self).commit()

# Clear our own ID in case this gets accidentally reused.
self._id = None

0 comments on commit c449af6

Please sign in to comment.