[Sqlalchemy-tickets] Issue #3379: allow external APIs control over invalidation (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2015-04-22 21:57:46
|
New issue 3379: allow external APIs control over invalidation https://bitbucket.org/zzzeek/sqlalchemy/issue/3379/allow-external-apis-control-over Mike Bayer: so that pool events can be used for clustering and other special cases where we want to invalidate only a subset of connections and have control over whether connections are bumped immediately, or only on next checkout, e.g.: ``` #!python from sqlalchemy import create_engine from sqlalchemy import event import collections import random e = create_engine("mysql://scott:tiger@localhost/test") hosts = ["host1", "host2", "host3"] conn_recs = collections.defaultdict(set) @event.listens_for(e, "do_connect") def do_connect(dialect, conn_rec, carg, cparam): host = random.choice(hosts) cparam['host'] = host conn_rec.info['host'] = host conn_recs['host'].add(conn_rec) @event.listens_for(e, "handle_error") def handle_error(ctx): if ctx.is_disconnect: host = ctx.connection.info['host'] ctx.invalidate_pool_on_disconnect = False ctx.connection.invalidate(soft=True) for conn_rec in conn_recs[host]: conn_rec.invalidate(soft=True) ``` |