Skip to content

Commit

Permalink
Reuse transaction objects when doing tm.begin() so old objects that…
Browse files Browse the repository at this point in the history
… reference

  old transaction object will still work and not be orphaned.
  • Loading branch information
vangheem committed Apr 6, 2017
1 parent 1728f18 commit 4a7ec9b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
@@ -1,6 +1,10 @@
1.0.0a6 (unreleased)
--------------------

- Reuse transaction objects when doing `tm.begin()` so old objects that reference
old transaction object will still work and not be orphaned.
[vangheem]

- Fix container objects not having current transaction when new objects are
registered for them
[vangheem]
Expand Down
5 changes: 5 additions & 0 deletions guillotina/db/transaction.py
Expand Up @@ -30,6 +30,11 @@ class Status:
class Transaction(object):

def __init__(self, manager, request=None):
# so we can reuse the same transaction object
self.init(manager, request)

def init(self, manager, request=None):
# so you can re-initialize a transaction object
self._txn_time = None
self._tid = None
self.status = Status.ACTIVE
Expand Down
6 changes: 4 additions & 2 deletions guillotina/db/transaction_manager.py
Expand Up @@ -42,8 +42,10 @@ async def begin(self, request=None):
self._pool = LifoQueue()
# Save the actual transaction and start a new one
self._pool.put(self._txn)

self._txn = txn = Transaction(self, request=request)
self._txn.init() # reset existing transaction
txn = self._txn
else:
self._txn = txn = Transaction(self, request=request)

# CACHE!!

Expand Down

0 comments on commit 4a7ec9b

Please sign in to comment.