coleifer / peewee

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

Connection won't be recycled after a while #2885

Closed dzpt closed 4 months ago

dzpt commented 4 months ago

I'm using bjoern to serve Flask app <-> peewee

I set the stale_timeout is 4 seconds. I close connection in both app.teardown_appcontext and app.teardown_request.

But the connection won't be recycled. Still got the message playhouse.pool.MaxConnectionsExceeded even leaving idle for 15 minutes straight.

Did i miss something and what should i do to recycle the connection?

coleifer commented 4 months ago

It's working fine for me.

from peewee import *
from playhouse.pool import *

app = Flask(__name__)
db = PooledSqliteDatabase('/tmp/test.db')

@app.before_request
def connect_db():
    db.connect()
@app.teardown_request
def close_db(exc=None):
    db.close()

import logging
log = logging.getLogger('peewee.pool')
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)

@app.route("/")
def hello_world():
    return "Hello, World!"

if __name__ == "__main__":
    import bjoern

    bjoern.run(app, "127.0.0.1", 8000)

Output:

No connection available in pool.
Created new connection 140657885258224.
Returning 140657885258224 to pool.
Returning 140657885258224 to pool.
Returning 140657885258224 to pool.
Returning 140657885258224 to pool.