On Tue, Nov 28, 2017 at 8:12 PM, Colton Allen <[email protected]> wrote: > I'm receiving this error: > > sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, > connection timed out, timeout 30 > > > I'm curious how SQLAlchemy manages database connections. I've pasted below > some psuedo-code to simplify the application. > > # Create an engine > engine = create_engine('whatever') > > # Create a scoped session factory > factory = sqlalchemy.orm.sessionmaker(bind=engine) > factory = sqlalchemy.orm.scoped_session(factory, > scopefunc=_app_ctx_stack.__ident_func__) > > # Handle websocket connection > def my_websocket(ws): > while True: > # yield after arbitrary amount of time > model_id = yield_from_redis() > > # Use the session to query the model. > session = factory() > model = session.query(SomeModel).get(model_id) > ws.send(model.seralized) > > # breakout or whatever > factory.remove() > > > > The engine and factory are being created once on a singleton. The session > is being created each time its needed. Anytime I need a session, I call > factory(). This hasn't been a problem on the HTTP server (I call remove() > after each request) but since websockets are so long lived, I assume that > creating all these session instances are somehow causing the overflow. > > Should I be closing that session sooner? I assume thats the case but I'm > asking because I want to make sure the code is as stable as possible.
if the websocket is long lived then yes, you should be releasing the transaction held by the session each time you do something, you dont need to remove the whole thing. calling session.close() before you wait for a long time would be sufficient (or commit() or rollback()). > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
