On Wed, Mar 17, 2010 at 2:31 PM, Michael Bayer <[email protected]> wrote: > > On Mar 16, 2010, at 8:09 PM, Julian Scheid wrote: > >> I'm looking for a way to close a pooled connection client-side after a >> certain period, say when it wasn't used in 30 seconds. I don't think >> any of the Pool implementations that come with SQLAlchemy supports >> this. Does anybody know of a third-party implementation with this >> functionality? I guess it wouldn't be difficult to create one from >> scratch but I was hoping that there is an existing solution. > > > *All* of the pool implementations support this, with the caveat that its > recycled on next checkout. See "pool_recycle" at > http://www.sqlalchemy.org/docs/reference/sqlalchemy/connections.html?highlight=create_engine#sqlalchemy.create_engine > . > > You can also force any connection to recycle immediately using > connection.invalidate(): > > http://www.sqlalchemy.org/docs/reference/sqlalchemy/connections.html?highlight=create_engine#sqlalchemy.create_engine > > if you truly wanted a checked out connection to automatically invalidate > itself while checked out, you definitely need to forego using any > transactions with it (i.e. autocommit for everything), and you can create a > ConnectionProxy that calls invalidate() when an execute() call is received > more than 30 seconds since the last one. ConnectionProxy is at > http://www.sqlalchemy.org/docs/reference/sqlalchemy/interfaces.html#sqlalchemy.interfaces.ConnectionProxy > .
Thanks for your reply. To elaborate a bit, this is meant to reduce the number of open database connections held by long-lived clients that do not continuously access the database (while still avoiding having to open a new physical connection for every session when there are multiple sessions in quick succession.) I do not need to invalidate checked out connections - I would like to close connections that have been returned to the pool and have been sitting there for a while without being re-requested. If I understand recycling correctly, it doesn't really help with this as it only closes the connection right before opening another one. I'm not sure if Connection.invalidate helps a lot either, as invalidation/expiry should depend on when the connection was returned to the pool so it seems that putting this functionality into a custom Pool implementation is more appropriate. Does this make sense, am I missing something? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
