-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eb16ed0 removed (among a lot of other things) a call to close_old_connections(), mostly because I hadn't understood what that was doing exactly. Since then I've been kept awake at night with the sense of dread that old database connections were piling up unclosed on Django's server. So I wrote this wsgi middleware that should hopefully restore balance in the universe, or at least close db connections.
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.core.signals import request_finished, request_started | ||
|
||
|
||
class DjangoDBManagementMiddleware: | ||
""" | ||
A simple WSGI middleware that manually manages opening/closing db connections. | ||
Django normally does that as part of its own middleware chain, but we're using Trac's middleware | ||
so we must do this by hand. | ||
This hopefully prevents open connections from piling up. | ||
""" | ||
|
||
def __init__(self, application): | ||
self.application = application | ||
|
||
def __call__(self, environ, start_response): | ||
request_started.send(sender=self.__class__) | ||
try: | ||
for data in self.application(environ, start_response): | ||
yield data | ||
finally: | ||
request_finished.send(sender=self.__class__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters