#21463 closed Uncategorized (fixed)
django mysql connection pinging
Reported by: | Brian May | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.5 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Every time django makes a mysql query, it pings the mysql server. django/db/backends/mysql/base.py:
def _valid_connection(self): if self.connection is not None: try: self.connection.ping() return True except DatabaseError: self.connection.close() self.connection = None return False
Not only is this inefficient[1], it is not needed.
I believe this kludge is in case the server connection is closed, e.g. server restarted, or server closed connection as it was idle, or ...
However I have talked to mysql experts, and have been told: "The proper way is as follows: If the connection was lost, a query call will return a specific error, and the client library will reconnect. The app then just needs to re-issue the same query - or in case it was in the middle of a transaction, restart that transaction (obviously)."
Doing a websearch it also seems to be insufficient, there are a number of user's reporting that they get "OperationalError: (2006, 'MySQL server has gone away')" type errors, which suggests Django isn't reconnecting automatically when it should."
I have just received this error myself, from Django 1.5, however still investigating.
I have a vague recollection that this was discussed in a ticket from years ago, however I can't find it now.
Notes:
[1] https://groups.google.com/forum/#!topic/django-developers/oIkjJbdWVEo
My reading of the 1.6.x and master versions suggest that this has been already fixed. 1.5.x is now in security fixes mode, so we aren't going to backport fixes there.