Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#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


Every time django makes a mysql query, it pings the mysql server. django/db/backends/mysql/

    def _valid_connection(self):
        if self.connection is not None:
                return True
            except DatabaseError:
                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.


Change History (2)

comment:1 Changed 5 years ago by Anssi Kääriäinen

Resolution: fixed
Status: newclosed

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.

comment:2 Changed 5 years ago by Aymeric Augustin

I cleaned this up when I added persistent connections.

Note: See TracTickets for help on using tickets.
Back to Top