Browse Source

Merge pull request #78 from tracim/fix/62_fix_sqlite_same_thread_issue

Damien Accorsi 6 years ago
parent
commit
5d24b2656e
No account linked to committer's email
2 changed files with 26 additions and 8 deletions
  1. 14 0
      tracim/lib/utils/request.py
  2. 12 8
      tracim/lib/webdav/middlewares.py

+ 14 - 0
tracim/lib/utils/request.py View File

@@ -34,6 +34,8 @@ class TracimRequest(Request):
34 34
         )
35 35
         self._current_workspace = None  # type: Workspace
36 36
         self._current_user = None  # type: User
37
+        # INFO - G.M - 18-05-2018 - Close db at the end of the request
38
+        self.add_finished_callback(self._cleanup)
37 39
 
38 40
     @property
39 41
     def current_workspace(self) -> Workspace:
@@ -74,6 +76,18 @@ class TracimRequest(Request):
74 76
             )
75 77
         self._current_user = user
76 78
 
79
+    def _cleanup(self, request: 'TracimRequest') -> None:
80
+        """
81
+        Close dbsession at the end of the request in order to avoid exception
82
+        about not properly closed session or "object created in another thread"
83
+        issue
84
+        see https://github.com/tracim/tracim_backend/issues/62
85
+        :param request: same as self, request
86
+        :return: nothing.
87
+        """
88
+        self._current_user = None
89
+        self._current_workspace = None
90
+        self.dbsession.close()
77 91
 
78 92
 ###
79 93
 # Utils for TracimRequest

+ 12 - 8
tracim/lib/webdav/middlewares.py View File

@@ -263,13 +263,17 @@ class TracimEnv(BaseMiddleware):
263 263
         self.app_config.configure_filedepot()
264 264
 
265 265
     def __call__(self, environ, start_response):
266
-        with transaction.manager as tm:
267
-            dbsession = get_tm_session(self.session_factory, transaction.manager)
268
-            environ['tracim_tm'] = tm
269
-            environ['tracim_dbsession'] = dbsession
270
-            environ['tracim_cfg'] = self.app_config
271
-
272
-            return self._application(environ, start_response)
266
+        # TODO - G.M - 18-05-2018 - This code should not create trouble
267
+        # with thread and database, this should be verify.
268
+        # see https://github.com/tracim/tracim_backend/issues/62
269
+        tm = transaction.manager
270
+        dbsession = get_tm_session(self.session_factory, tm)
271
+        environ['tracim_tm'] = tm
272
+        environ['tracim_dbsession'] = dbsession
273
+        environ['tracim_cfg'] = self.app_config
274
+        app = self._application(environ, start_response)
275
+        dbsession.close()
276
+        return app
273 277
 
274 278
 
275 279
 class TracimUserSession(BaseMiddleware):
@@ -285,4 +289,4 @@ class TracimUserSession(BaseMiddleware):
285 289
             session=environ['tracim_dbsession'],
286 290
             config=environ['tracim_cfg'],
287 291
         ).get_one_by_email(environ['http_authenticator.username'])
288
-        return self._application(environ, start_response)
292
+        return self._application(environ, start_response)