if you want to go that route I'd use the connection events to hit every DBAPI 
connection as it's created, and store them in a weak-referencing collection:

import weakref
from sqlalchemy import event
from sqlalchemy.engine import Engine

connections = weakref.WeakKeyDictionary()

@event.listens_for(Engine, "connect")
def connect(dbapi_conn, conn_rec):
    connections[dbapi_conn] = True


# this would run in a worker thread or other asynchronous system
def refresh():
   for conn in connections:
        cursor = conn.cursor()
        cursor.execute("select 1")
        cursor.close()

there's a slight chance of unsupported concurrent access with the above 
pattern, esp. if you're using a non-threadsafe DBAPI like MySQLdb.   If so, you 
might want to also keep track of connections as they are checked out and 
checked in using the "checkin", "checkout" events, and only do the "ping" if a 
connection is not in "checkout" state.







On Dec 23, 2012, at 11:03 AM, Alexey Vihorev wrote:

> Hi, all!
> I’ve got a problem in my GUI app – if a connection stayes open for too long, 
> and is inactive (user opened an editing window and went away for a cofee 
> brake etc), firewall/other network machinery will close the connection and 
> the session will fail with ’connection abnormally terminated’. So I’m trying 
> to come up with a best way to handle  that, and for now I’ve settled with 
> using timer and executing session.connection().execute('select now()') every 
> minute or so, but that requires handling of each session\connection 
> individually. Can I get a list of all open connections for an engine, iterate 
> over them and do execute('select now()')? Thanks.
> 
> -- 
> 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.

-- 
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.

Reply via email to