coleifer / flask-peewee

flask integration for peewee, including admin, authentication, rest api and more
http://flask-peewee.readthedocs.org/
MIT License
776 stars 181 forks source link

db.py: Database.connect_db(): do nothing if already connected #166

Closed michael-db closed 7 years ago

michael-db commented 7 years ago

With SQLite, this fixes peewee.OperationalError raise OperationalError('Connection already open') when following the Flask-Security "Basic Peewee Application" example https://pythonhosted.org/Flask-Security/quickstart.html#id3

Test environment: pip install flask-security flask-peewee Successfully installed Flask-0.12 Flask-Login-0.3.2 Flask-Mail-0.9.1 Flask-Principal-0.4.0 Flask-WTF-0.14.2 MarkupSafe-0.23 blinker-1.4 click-6.7 flask-peewee-0.6.7 flask-security-1.7.5 itsdangerous-0.24 jinja2-2.9.5 passlib-1.7.1 peewee-2.8.8 werkzeug-0.11.15 wtf-peewee-0.2.6 wtforms-2.1

coleifer commented 7 years ago

The problem in that example code is that in the app.before_first_request hook, a connection is opened implicitly but never then closed, hence leaking a connection. I'd suggest fixing the example code.

michael-db commented 7 years ago

Thanks, Charles. You're right, but it had seemed to me that it would be convenient to expose a 'get connection if not already connected' method, which would simultaneously solve that issue. (Provided the connection was ultimately closed somewhere.) Of course, it's not a bug in db.py as such.