using pymysql connect to mysql, leave the program running for a long time,for example, leave the office at night and come back next morning.during this period,no any operation on this application.now doing a database commit would give this error.
File "/usr/local/lib/python3.3/site-packages/pymysql/cursors.py", line 117, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python3.3/site-packages/pymysql/connections.py", line 189, in defaulterrorhandler
raise errorclass(errorvalue)
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
restart the web server (tornado),it's ok.why if leave it long time would get this error?
Open the MySQL Workbench Preferences. Check if the SSH Timeout and DBMS Timeout value is set to only a few seconds. Try to increase the default value of the connection timeouts. Save the settings, close the MySQL Workbench and reopen the connection to see if you are able to connect to the database.
PyMySQL fetchAll The fetchall function gets all records. It returns a result set. Technically, it is a tuple of tuples. Each of the inner tuples represent a row in the table.
The client library is thread-safe per connection. Two threads can share the same connection with the following caveats: Unless you are using the asynchronous C API functions mentioned previously, multiple threads cannot send a query to the MySQL server at the same time on the same connection.
class pymysql.cursors. DictCursor (connection) A cursor which returns results as a dictionary.
The wait_timeout exists for a reason: long-time idle connections are wasteful of limited server resources. You are correct: increasing it is not the right approach.
Fortunately, this is Python which has a robust exception mechanism. I'm not familiar with pymysql
but presumably you've got an "open_connection" method somewhere which allows
try:
cursor.do_something()
except pymysql.err.OperationalError as e:
if e[0] == 2013: # Lost connection to server
# redo open_connection and do_something
else:
raise
Since you didn't post any calling code, I can't structure this example to match your application. There things worth noting about the except
clause: the first is that they should always be as narrow as possible, in this case there are (presumably) many OperationalErrors and you only know how to deal with 'Lost connection'.
In case it isn't a lost connection exception, you should re-raise it so it doesn't get swallowed. Unless you know how to handle other OperationalErrors, this will get passed up the stack and result in an informative error message which is a reasonable thing to do since your cursor is probably useless anyway by then.
Restarting the web server only fixes the lost connection as an accidental side effect of reinitializing everything; handling the exception within the code is a much more gentle way of accomplishing your goal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With