dbapi.py
changeset 5815 282194aa43f3
parent 5764 ec5dbbb6a549
parent 5813 0b250d72fcfa
child 5863 4495b9bc49df
--- a/dbapi.py	Mon Jun 21 15:32:58 2010 +0200
+++ b/dbapi.py	Mon Jun 21 15:34:46 2010 +0200
@@ -24,6 +24,7 @@
 
 __docformat__ = "restructuredtext en"
 
+from threading import currentThread
 from logging import getLogger
 from time import time, clock
 from itertools import count
@@ -400,6 +401,9 @@
         """no effect"""
         pass
 
+    def _txid(self):
+        return self.connection._txid(self)
+
     def execute(self, rql, args=None, eid_key=None, build_descr=True):
         """execute a rql query, return resulting rows and their description in
         a :class:`~cubicweb.rset.ResultSet` object
@@ -435,7 +439,8 @@
             warn('[3.8] eid_key is deprecated, you can safely remove this argument',
                  DeprecationWarning, stacklevel=2)
         # XXX use named argument for build_descr in case repo is < 3.8
-        rset = self._repo.execute(self._sessid, rql, args, build_descr=build_descr)
+        rset = self._repo.execute(self._sessid, rql, args,
+                                  build_descr=build_descr, txid=self._txid())
         rset.req = self.req
         return rset
 
@@ -490,6 +495,9 @@
             self.rollback()
             return False #propagate the exception
 
+    def _txid(self, cursor=None): # XXX could now handle various isolation level!
+        return currentThread().getName()
+
     def request(self):
         return DBAPIRequest(self.vreg, DBAPISession(self))
 
@@ -551,18 +559,12 @@
             esubpath = list(subpath)
             esubpath.remove('views')
             esubpath.append(join('web', 'views'))
-        cubes = reversed([config.cube_dir(p) for p in cubes])
-        vpath = config.build_vregistry_path(cubes, evobjpath=esubpath,
+        cubespath = [config.cube_dir(p) for p in cubes]
+        config.load_site_cubicweb(cubespath)
+        vpath = config.build_vregistry_path(reversed(cubespath),
+                                            evobjpath=esubpath,
                                             tvobjpath=subpath)
         self.vreg.register_objects(vpath)
-        if self._cnxtype == 'inmemory':
-            # should reinit hooks manager as well
-            hm, config = self._repo.hm, self._repo.config
-            hm.set_schema(hm.schema) # reset structure
-            hm.register_system_hooks(config)
-            # instance specific hooks
-            if self._repo.config.instance_hooks:
-                hm.register_hooks(config.load_hooks(self.vreg))
 
     def use_web_compatible_requests(self, baseurl, sitetitle=None):
         """monkey patch DBAPIRequest to fake a cw.web.request, so you should
@@ -632,7 +634,7 @@
     def describe(self, eid):
         if self._closed is not None:
             raise ProgrammingError('Closed connection')
-        return self._repo.describe(self.sessionid, eid)
+        return self._repo.describe(self.sessionid, eid, txid=self._txid())
 
     def close(self):
         """Close the connection now (rather than whenever __del__ is called).
@@ -645,7 +647,7 @@
         """
         if self._closed:
             raise ProgrammingError('Connection is already closed')
-        self._repo.close(self.sessionid)
+        self._repo.close(self.sessionid, txid=self._txid())
         del self._repo # necessary for proper garbage collection
         self._closed = 1
 
@@ -659,7 +661,7 @@
         """
         if not self._closed is None:
             raise ProgrammingError('Connection is already closed')
-        return self._repo.commit(self.sessionid)
+        return self._repo.commit(self.sessionid, txid=self._txid())
 
     def rollback(self):
         """This method is optional since not all databases provide transaction
@@ -672,7 +674,7 @@
         """
         if not self._closed is None:
             raise ProgrammingError('Connection is already closed')
-        self._repo.rollback(self.sessionid)
+        self._repo.rollback(self.sessionid, txid=self._txid())
 
     def cursor(self, req=None):
         """Return a new Cursor Object using the connection.
@@ -713,6 +715,7 @@
           and set to false.
         """
         txinfos = self._repo.undoable_transactions(self.sessionid, ueid,
+                                                   txid=self._txid(),
                                                    **actionfilters)
         if req is None:
             req = self.request()
@@ -727,7 +730,8 @@
         allowed (eg not in managers group and the transaction doesn't belong to
         him).
         """
-        txinfo = self._repo.transaction_info(self.sessionid, txuuid)
+        txinfo = self._repo.transaction_info(self.sessionid, txuuid,
+                                             txid=self._txid())
         if req is None:
             req = self.request()
         txinfo.req = req
@@ -743,7 +747,8 @@
         session's user is not allowed (eg not in managers group and the
         transaction doesn't belong to him).
         """
-        return self._repo.transaction_actions(self.sessionid, txuuid, public)
+        return self._repo.transaction_actions(self.sessionid, txuuid, public,
+                                              txid=self._txid())
 
     def undo_transaction(self, txuuid):
         """Undo the given transaction. Return potential restoration errors.
@@ -752,4 +757,5 @@
         allowed (eg not in managers group and the transaction doesn't belong to
         him).
         """
-        return self._repo.undo_transaction(self.sessionid, txuuid)
+        return self._repo.undo_transaction(self.sessionid, txuuid,
+                                           txid=self._txid())