On Mon, Nov 13, 2017 at 3:58 AM, Ahmad Javed <ahmadjaved...@gmail.com> wrote: > Hi, > > We have implemented web applications using Django and SQLAlchemy framework. > We have distributed system in the company and some API used frequently. > > We are facing a problem where MySQL connections reached a maximum limit. > > In Django applications, we are using SQLAlchemy with pool engine and > scoped_session. Also using pool recycle option as well. > > There is custom Django middleware placed in application to remove the > session at the end and dispose the engine as well. > > But still, it leaves MySQL open connections. > > Today I have reproduced and investigated problem locally and dig into > library code. I found that instead of disposing engine if I dispose engine's > pool then it actually remove connections. > > I was expecting that disposing engine only will take care of connections. > > I need some guidance from experts. Is this approach good? Should I go with > this solution? Or is there better solution that can help me? > > P.S, we are using 0.7.8 version of SQLAlchemy. Unfortunately due to some > issues I can not update the version. > > Thanks in advance, > Ahmad Javed >
It sounds like you are recreating the engine for each request, which is not the way it is supposed to be used. You should only have a single engine instance, typically created or configured when your application starts up. Disposing the engine (or the pool) should never be necessary. If all your per-request DB access goes through the scoped session, and you are calling session.remove() at the end of each request, connections should be returned to the pool properly. Simon -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.