coleifer / peewee

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
http://docs.peewee-orm.com/
MIT License
11.11k stars 1.37k forks source link

pymysql.err.OperationalError: (2014, 'Command Out of Sync') #1236

Closed aiportal closed 7 years ago

aiportal commented 7 years ago

My code in flask is like this:

db_connections = [db_fetch, db_zb123]

class Database:
    @staticmethod
    def connect():
        for conn in db_connections:
            if conn.is_closed():
                conn.connect()

    @staticmethod
    def close():
        for conn in db_connections:
            if not conn.is_closed():
                conn.close()

@app.before_request
def before_req():
    Database.connect()

@app.teardown_request
def teardown_req(exc):
    Database.close()

And my web app is under uwsgi.

I found exception log in uwsgi log file.


Traceback (most recent call last):
  File "/env/flask/lib/python3.5/site-packages/peewee.py", line 3754, in commit
    self.get_conn().commit()
  File "/env/flask/lib/python3.5/site-packages/pymysql/connections.py", line 784, in commit
    self._read_ok_packet()
  File "/env/flask/lib/python3.5/site-packages/pymysql/connections.py", line 765, in _read_ok_packet
    raise err.OperationalError(2014, "Command Out of Sync")
pymysql.err.OperationalError: (2014, 'Command Out of Sync')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/env/flask/lib/python3.5/site-packages/peewee.py", line 3746, in execute_sql
    self.commit()
  File "/env/flask/lib/python3.5/site-packages/peewee.py", line 3754, in commit
    self.get_conn().commit()
  File "/env/flask/lib/python3.5/site-packages/peewee.py", line 3569, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/env/flask/lib/python3.5/site-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/env/flask/lib/python3.5/site-packages/peewee.py", line 3754, in commit
    self.get_conn().commit()
  File "/env/flask/lib/python3.5/site-packages/pymysql/connections.py", line 784, in commit
    self._read_ok_packet()
  File "/env/flask/lib/python3.5/site-packages/pymysql/connections.py", line 765, in _read_ok_packet
    raise err.OperationalError(2014, "Command Out of Sync")
peewee.OperationalError: (2014, 'Command Out of Sync')

During handling of the above exception, another exception occurred:
...

Why this error happens and how to fix it?

coleifer commented 7 years ago

Not sure looks like an issue in pymysql, the database driver, as opposed to peewee.

nanoric commented 5 years ago

I have a same problem. with the same env: peewee + pymysql + uwsgi(multi processes. every process have only 1 thread running)

at the beginning, errors comes about 8 hours since uwsgi starts(about 10k queries). With there is more and more user, it crashes faster and faster. Now it will crash every hour.

I tried to use PooledMySQLConnection, but havn't solve this problem. But why a PooledConnection can still suffer a "connection problem"? Keep every connection alive and usable is the duty for a ConnectionPool, isn't it?

coleifer commented 5 years ago

Keep every connection alive and usable is the duty for a ConnectionPool, isn't it?

If you read the code in the playhouse.pool module you'll see that Peewee does this when a connection is requested. It verifies that the connection is usable before returning it to the user, using an implementation-specific method for each db driver. MySQL uses the "ping()" method.