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

Add support for MySQL connection recycling #239

Closed
arnuschky opened this issue Oct 17, 2013 · 12 comments
Closed

Add support for MySQL connection recycling #239

arnuschky opened this issue Oct 17, 2013 · 12 comments

Comments

@arnuschky
Copy link

We're getting the following error on our flask project:

  SQLError: (OperationalError) (2006, ‘MySQL server has gone away’)

This problem was implicitly mentioned in #222 for example.

SQLalchemy addresses the problem as follows: pallets-eco/flask-sqlalchemy@05c5193
Associated issue: pallets-eco/flask-sqlalchemy#2

Would it be possible to implement this in PeeWee as well?

@arnuschky
Copy link
Author

Now that I think of it, this issue maybe more appropriate for flask-peewee, but I am not sure.

@coleifer
Copy link
Owner

I'm a little unclear on how your particular issue is arising. If you're using flask-peewee's Database object, it should be opening and closing the connection on each request. If you're using a multi-threaded wsgi server have you tried configuring your database with threadlocals=True? That would eliminate the possibility that a second thread is closing the conn opened by the first thread.

@arnuschky
Copy link
Author

Yes, we are using gunicorn with multiple workers and without threadlocals=True. Must have missed it in the docs (now I even found another old issue regarding this)! I guess that his issue is invalid then, but I will test first.

@beanyoung
Copy link

You could use sql_error_handler to handle this problem. Anyway, this problem is caused by the connection is idle for a long time(longer than timeout seconds, default 28800 seconds)

@coleifer
Copy link
Owner

@arnuschky let me know if threadlocals resolved it!

@arnuschky
Copy link
Author

It seems to resolve the problem. Thanks!

@erigo
Copy link

erigo commented Aug 1, 2014

threadlocals=True can not resolved this issue~
after 28800 seconds, Error Msg ‘MySQL server has gone away’ appear agian!


read peewee doc again, use mysql_db.connect() & mysql_db.close() for each request in CherryPy.

@coleifer
Copy link
Owner

coleifer commented Aug 1, 2014

You can also check out http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#pool for connection pooling and recycling.

@erigo
Copy link

erigo commented Aug 2, 2014

Thanks for your big help. playhouse.pool is the final solution which I needed~

@serkandaglioglu
Copy link

I use peewee with flask.

Peewee database connection :

db = PooledMySQLDatabase(database,**{
            "passwd": password, "user": user,
            "max_connections":None,"stale_timeout":None,
            "threadlocals" : True
        })

@app.before_request
def before_request():
    db.connect()

@app.teardown_request
def teardown_request(exception):
    db.close()

After mysql error that "MySQL server has gone away (error(32, 'Broken pipe'))", select queries works without problem, but insert,update,delete queries don't work.

On insert,update,delete queries works behind(in mysql) but peewee throw this errors.

(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")

@coleifer
Copy link
Owner

That's odd, not sure what that might mean.

@serkandaglioglu
Copy link

@coleifer I have two different applications with peewee and flask. They have same problem. Select queries has no problem but insert,update,delete always throws error which above. I will do test with an basic peewee and flask app and I will write the result of the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants