Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top