##// END OF EJS Templates
db: move Session.remove to outer wsgi layer and also add it...
dan -
r669:d3c76065 stable
parent child Browse files
Show More
@@ -39,6 +39,7 b' from routes.middleware import RoutesMidd'
39 import routes.util
39 import routes.util
40
40
41 import rhodecode
41 import rhodecode
42 from rhodecode.model import meta
42 from rhodecode.config import patches
43 from rhodecode.config import patches
43 from rhodecode.config.routing import STATIC_FILE_PREFIX
44 from rhodecode.config.routing import STATIC_FILE_PREFIX
44 from rhodecode.config.environment import (
45 from rhodecode.config.environment import (
@@ -158,6 +159,10 b' def make_pyramid_app(global_config, **se'
158 pyramid_app = config.make_wsgi_app()
159 pyramid_app = config.make_wsgi_app()
159 pyramid_app = wrap_app_in_wsgi_middlewares(pyramid_app, config)
160 pyramid_app = wrap_app_in_wsgi_middlewares(pyramid_app, config)
160 pyramid_app.config = config
161 pyramid_app.config = config
162
163 # creating the app uses a connection - return it after we are done
164 meta.Session.remove()
165
161 return pyramid_app
166 return pyramid_app
162
167
163
168
@@ -374,7 +379,25 b' def wrap_app_in_wsgi_middlewares(pyramid'
374 pyramid_app = make_gzip_middleware(
379 pyramid_app = make_gzip_middleware(
375 pyramid_app, settings, compress_level=1)
380 pyramid_app, settings, compress_level=1)
376
381
377 return pyramid_app
382
383 # this should be the outer most middleware in the wsgi stack since
384 # middleware like Routes make database calls
385 def pyramid_app_with_cleanup(environ, start_response):
386 try:
387 return pyramid_app(environ, start_response)
388 finally:
389 # Dispose current database session and rollback uncommitted
390 # transactions.
391 meta.Session.remove()
392
393 # In a single threaded mode server, on non sqlite db we should have
394 # '0 Current Checked out connections' at the end of a request,
395 # if not, then something, somewhere is leaving a connection open
396 pool = meta.Base.metadata.bind.engine.pool
397 log.debug('sa pool status: %s', pool.status())
398
399
400 return pyramid_app_with_cleanup
378
401
379
402
380 def sanitize_settings_and_apply_defaults(settings):
403 def sanitize_settings_and_apply_defaults(settings):
@@ -30,6 +30,7 b' import Pyro4'
30 import pylons
30 import pylons
31 import rhodecode
31 import rhodecode
32
32
33 from rhodecode.model import meta
33 from rhodecode.lib import hooks_base
34 from rhodecode.lib import hooks_base
34 from rhodecode.lib.utils2 import (
35 from rhodecode.lib.utils2 import (
35 AttributeDict, safe_str, get_routes_generator_for_server_url)
36 AttributeDict, safe_str, get_routes_generator_for_server_url)
@@ -64,7 +65,10 b' class HooksHttpHandler(BaseHTTPRequestHa'
64
65
65 def _call_hook(self, method, extras):
66 def _call_hook(self, method, extras):
66 hooks = Hooks()
67 hooks = Hooks()
67 result = getattr(hooks, method)(extras)
68 try:
69 result = getattr(hooks, method)(extras)
70 finally:
71 meta.Session.remove()
68 return result
72 return result
69
73
70 def log_message(self, format, *args):
74 def log_message(self, format, *args):
@@ -406,8 +406,11 b' class SimpleVCS(object):'
406 yield chunk
406 yield chunk
407 finally:
407 finally:
408 # invalidate cache on push
408 # invalidate cache on push
409 if action == 'push':
409 try:
410 self._invalidate_cache(repo_name)
410 if action == 'push':
411 self._invalidate_cache(repo_name)
412 finally:
413 meta.Session.remove()
411
414
412 def _get_repository_name(self, environ):
415 def _get_repository_name(self, environ):
413 """Get repository name out of the environmnent
416 """Get repository name out of the environmnent
@@ -40,40 +40,35 b' def pylons_compatibility_tween_factory(h'
40 from pyramid. For example while rendering an old template that uses the
40 from pyramid. For example while rendering an old template that uses the
41 'c' or 'h' objects. This tween sets up the needed pylons globals.
41 'c' or 'h' objects. This tween sets up the needed pylons globals.
42 """
42 """
43 try:
43 config = rhodecode.CONFIG
44 config = rhodecode.CONFIG
44 environ = request.environ
45 environ = request.environ
45 session = request.session
46 session = request.session
46 session_key = (config['pylons.environ_config']
47 session_key = (config['pylons.environ_config']
47 .get('session', 'beaker.session'))
48 .get('session', 'beaker.session'))
49
48
50 # Setup pylons globals.
49 # Setup pylons globals.
51 pylons.config._push_object(config)
50 pylons.config._push_object(config)
52 pylons.request._push_object(request)
51 pylons.request._push_object(request)
53 pylons.session._push_object(session)
52 pylons.session._push_object(session)
54 environ[session_key] = session
53 environ[session_key] = session
55 pylons.url._push_object(URLGenerator(config['routes.map'],
54 pylons.url._push_object(URLGenerator(config['routes.map'],
56 environ))
55 environ))
57
56
58 # TODO: Maybe we should use the language from pyramid.
57 # TODO: Maybe we should use the language from pyramid.
59 translator = _get_translator(config.get('lang'))
58 translator = _get_translator(config.get('lang'))
60 pylons.translator._push_object(translator)
59 pylons.translator._push_object(translator)
61
62 # Get the rhodecode auth user object and make it available.
63 auth_user = get_auth_user(environ)
64 request.user = auth_user
65 environ['rc_auth_user'] = auth_user
66
60
67 # Setup the pylons context object ('c')
61 # Get the rhodecode auth user object and make it available.
68 context = ContextObj()
62 auth_user = get_auth_user(environ)
69 context.rhodecode_user = auth_user
63 request.user = auth_user
70 attach_context_attributes(context, request)
64 environ['rc_auth_user'] = auth_user
71 pylons.tmpl_context._push_object(context)
65
72 return handler(request)
66 # Setup the pylons context object ('c')
73 finally:
67 context = ContextObj()
74 # Dispose current database session and rollback uncommitted
68 context.rhodecode_user = auth_user
75 # transactions.
69 attach_context_attributes(context, request)
76 meta.Session.remove()
70 pylons.tmpl_context._push_object(context)
71 return handler(request)
77
72
78 return pylons_compatibility_tween
73 return pylons_compatibility_tween
79
74
General Comments 0
You need to be logged in to leave comments. Login now