Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #21597 -- Recover from faulty connections. #2740

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Fixed #21597 -- Recover from faulty connections.

This implements a way of recovering from faulty MySQL Server connections when an error #2006 is issued from the MySQL Connection Adapter.
It renews the (already broken) MySQL Connection and also resets the faulty cursor by replacing its current instance with a brand new one.
  • Loading branch information...
victorgama committed May 29, 2014
commit 38f58aa4d751ad83f1dc76d5b945a1036239584f
@@ -114,9 +114,11 @@ class CursorWrapper(object):
to the particular underlying representation returned by Connection.cursor().
"""
codes_for_integrityerror = (1048,)
codes_for_goneaway = (2006,)

def __init__(self, cursor):
def __init__(self, cursor, db=None):
self.cursor = cursor
self.db = db

def execute(self, query, args=None):
try:
@@ -127,6 +129,8 @@ def execute(self, query, args=None):
# misclassified and Django would prefer the more logical place.
if e.args[0] in self.codes_for_integrityerror:
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
if e.args[0] in self.codes_for_goneaway:
return self._reissue_query(self.execute, query, args)
raise

def executemany(self, query, args):
@@ -137,8 +141,16 @@ def executemany(self, query, args):
# misclassified and Django would prefer the more logical place.
if e.args[0] in self.codes_for_integrityerror:
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
if e.args[0] in self.codes_for_goneaway:
return self._reissue_query(self.executemany, query, args)
raise

def _reissue_query(self, method, query, args):
self.cursor.close()
self.db.connect()
self.cursor = self.db.create_cursor()
return method(query, args)

def __getattr__(self, attr):
if attr in self.__dict__:
return self.__dict__[attr]
@@ -448,7 +460,7 @@ def init_connection_state(self):

def create_cursor(self):
cursor = self.connection.cursor()
return CursorWrapper(cursor)
return CursorWrapper(cursor, self)

def _rollback(self):
try:
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.