问题
I getting a django.db.utils.OpertionalError: (2013, Lost connection to MySQL Server)
. The reason is because the MAX_TIMEOUT
is set to 90 seconds. I'm attempting to catch the error, close the bade connection, and make a fresh query. I would think the following try/except block would catch the error, close the connection and try the same query a second time:
try:
sku = SkuLight.objects.filter(expansion=card_set).filter(name=card_name)
except OperationalError as e:
print(e)
connection.close()
sku = SkuLight.objects.filter(expansion=card_set).filter(name=card_name)
Am I not closing the connection correctly? If not, how can I close and reopen the connection in the correct way. Thanks.
Here is the full traceback:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\fbchat\client.py", line 2295, in _parseMessage
thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m)
File "C:\Users\Jup\Alpha\customer\facebook_listen.py", line 54, in onMessage
if not sku.exists():
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py", line 715, in exists
return self.query.has_results(using=self.db)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 509, in has_results
return compiler.has_results()
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 1032, in has_results
return bool(self.execute_sql(SINGLE))
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 1063, in execute_sql
cursor.execute(sql, params)
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Program Files\Python36\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Program Files\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Program Files\Python36\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')
来源:https://stackoverflow.com/questions/52228881/trouble-closing-and-reopening-mysql-connection-after-catching-handling-django-db