[session/transaction] handle cnxset repository logic in transaction
Interacting with the repo is now handled by the Transaction code directly.
--- a/server/session.py Fri Mar 29 12:34:53 2013 +0100
+++ b/server/session.py Fri Mar 29 17:25:56 2013 +0100
@@ -341,6 +341,9 @@
actual query. This allows multiple transaction with a reasonable low
connection set pool size. control mechanism is detailed below
+ .. automethod:: cubicweb.server.session.Transaction.set_cnxset
+ .. automethod:: cubicweb.server.session.Transaction.free_cnxset
+
:attr:`mode`, string telling the connections set handling mode, may be one
of 'read' (connections set may be freed), 'write' (some write was done in
the connections set, it can't be freed before end of the transaction),
@@ -459,6 +462,35 @@
self._cnxset = new_cnxset
self.ctx_count += 1
+ def set_cnxset(self):
+ """the transaction need a connections set to execute some queries"""
+ if self.cnxset is None:
+ cnxset = self.repo._get_cnxset()
+ try:
+ self.cnxset = cnxset
+ try:
+ cnxset.cnxset_set()
+ except:
+ self.cnxset = None
+ raise
+ except:
+ self.repo._free_cnxset(cnxset)
+ raise
+ return self.cnxset
+
+ def free_cnxset(self, ignoremode=False):
+ """the transaction is no longer using its connections set, at least for some time"""
+ # cnxset may be none if no operation has been done since last commit
+ # or rollback
+ cnxset = self.cnxset
+ if cnxset is not None and (ignoremode or self.mode == 'read'):
+ try:
+ self.cnxset = None
+ finally:
+ cnxset.cnxset_freed()
+ self.repo._free_cnxset(cnxset)
+
+
# Entity cache management #################################################
#
# The transaction entity cache as held in tx.data it is removed at end the
@@ -1057,33 +1089,8 @@
if self._closed:
self.free_cnxset(True)
raise Exception('try to set connections set on a closed session %s' % self.id)
- tx = self._tx
- if tx.cnxset is None:
- cnxset = self.repo._get_cnxset()
- try:
- self._tx.cnxset = cnxset
- try:
- cnxset.cnxset_set()
- except:
- self._tx.cnxset = None
- raise
- except:
- self.repo._free_cnxset(cnxset)
- raise
- return tx.cnxset
-
- def free_cnxset(self, ignoremode=False):
- """the session is no longer using its connections set, at least for some time"""
- # cnxset may be none if no operation has been done since last commit
- # or rollback
- tx = self._tx
- cnxset = tx.cnxset
- if cnxset is not None and (ignoremode or self.mode == 'read'):
- try:
- tx.cnxset = None
- finally:
- cnxset.cnxset_freed()
- self.repo._free_cnxset(cnxset)
+ return self._tx.set_cnxset()
+ free_cnxset = tx_meth('free_cnxset')
def _touch(self):
"""update latest session usage timestamp and reset mode to read"""