Selaa lähdekoodia

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

Damien Accorsi 6 vuotta sitten
vanhempi
commit
5d24b2656e
No account linked to committer's email
2 muutettua tiedostoa jossa 26 lisäystä ja 8 poistoa
  1. 14 0
      tracim/lib/utils/request.py
  2. 12 8
      tracim/lib/webdav/middlewares.py

+ 14 - 0
tracim/lib/utils/request.py Näytä tiedosto

34
         )
34
         )
35
         self._current_workspace = None  # type: Workspace
35
         self._current_workspace = None  # type: Workspace
36
         self._current_user = None  # type: User
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
     @property
40
     @property
39
     def current_workspace(self) -> Workspace:
41
     def current_workspace(self) -> Workspace:
74
             )
76
             )
75
         self._current_user = user
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
 # Utils for TracimRequest
93
 # Utils for TracimRequest

+ 12 - 8
tracim/lib/webdav/middlewares.py Näytä tiedosto

263
         self.app_config.configure_filedepot()
263
         self.app_config.configure_filedepot()
264
 
264
 
265
     def __call__(self, environ, start_response):
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
 class TracimUserSession(BaseMiddleware):
279
 class TracimUserSession(BaseMiddleware):
285
             session=environ['tracim_dbsession'],
289
             session=environ['tracim_dbsession'],
286
             config=environ['tracim_cfg'],
290
             config=environ['tracim_cfg'],
287
         ).get_one_by_email(environ['http_authenticator.username'])
291
         ).get_one_by_email(environ['http_authenticator.username'])
288
-        return self._application(environ, start_response)
292
+        return self._application(environ, start_response)