1. Alexander Solovyov
  2. svarga

Commits

Alexander Solovyov  committed 9433dc8

commit databases on exit from command as well as on response

  • Participants
  • Parent commits 95108a4
  • Branches default

Comments (0)

Files changed (2)

File svarga/core/database.py

View file
     def __call__(self, *args, **kwargs):
         return self.default(*args, **kwargs)
 
+    def _execall(self, action):
+        for db in self.databases.itervalues():
+            if hasattr(db, action):
+                getattr(db, action)()
+
+    def commit(self):
+        self._execall('commit')
+
+    def rollback(self):
+        self._execall('rollback')
+
+    def close(self):
+        self._execall('close')
+
 
 @middleware
 def autocommit(request, view, kwargs, nexthandler):
-    def execute(action):
-        for v in db.databases.itervalues():
-            if hasattr(v, action):
-                getattr(v, action)()
-
     try:
         response = nexthandler(request, view, kwargs)
-        execute('commit')
+        db.commit()
     except:
-        execute('rollback')
+        db.rollback()
         raise
     finally:
-        execute('close')
+        db.close()
     return response
 
 

File svarga/core/dispatch.py

View file
 import logging
 
-from svarga.core.env import env, Environment
+from svarga import db
+from svarga import Environment
 from opster import dispatch, command # keep command here
-from svarga.utils.imports import module_not_found
+from svarga.utils.imports import import_module
 
 ROOTLOGGERS = ['svarga', 'sqlalchemy', 'sqlalchemy.engine']
 log = logging.getLogger(__name__)
 
     import svarga.core.commands
     for app in settings.APPS:
-        try:
-            __import__('%s.commands' % app, fromlist=['true'])
-        except ImportError:
-            if not module_not_found():
-                raise
+        import_module(app + '.commands', required=False)
 
     try:
         dispatch(globaloptions=global_setup.options, middleware=global_setup)
     finally:
-        if env.is_valid():
-            # TODO: Get rid of sqla assumption. Other data providers
-            # may need commit as well
-            if hasattr(env, 'sqla'):
-                env.sqla.session.commit()
+        try:
+            db.commit()
+        except:
+            db.rollback()
+            raise
+        finally:
+            db.close()
 
 def global_setup(func):
     def extract_dict(source, keys):