版本:1.1.0b2 |发布日期:2016年7月1日

SQLAlchemy 1.1文档

弃用的事件接口

本节介绍SQLAlchemy 0.5中引入的基于类的核心事件接口。ORM模拟在Deprecated ORM Event Interfaces中描述。

从版本0.7开始弃用: Events中描述的新事件系统取代了扩展/代理/侦听器系统,为所有事件提供一致的接口,而无需子类化。

执行,连接和光标事件

class sqlalchemy.interfaces。 ConnectionProxy

允许通过Connections拦截语句执行。

注意

ConnectionProxy已弃用。请参阅ConnectionEvents

可以实现execute()cursor_execute()中的任何一个或两个来拦截已编译的语句和游标级别执行,例如:

class MyProxy(ConnectionProxy):
    def execute(self, conn, execute, clauseelement,
                *multiparams, **params):
        print "compiled statement:", clauseelement
        return execute(clauseelement, *multiparams, **params)

    def cursor_execute(self, execute, cursor, statement,
                       parameters, context, executemany):
        print "raw statement:", statement
        return execute(cursor, statement, parameters, context)

execute参数是一个函数,它将完成操作的默认执行行为。应该使用示例中所示的签名。

代理通过proxy参数安装到Engine中:

e = create_engine('someurl://', proxy=MyProxy())
begin connbegin

拦截begin()事件。

begin_twophase(conn, begin_twophase, xid)

拦截begin_twophase()事件。

commit conncommit

拦截commit()事件。

commit_twophase tt> conncommit_twophasexidis_prepared t5 > ) T6> ¶ T7>

拦截commit_twophase()事件。

cursor_execute execute游标语句参数 t5 >,上下文executemany

拦截低级别的游标execute()事件。

execute(conn, execute, clauseelement, *multiparams, **params)

拦截高级别的execute()事件。

prepare_twophase(conn, prepare_twophase, xid)

拦截prepare_twophase()事件。

release_savepoint(conn, release_savepoint, name, context)

拦截release_savepoint()事件。

回滚 conn回滚

拦截回滚()事件。

rollback_savepoint(conn, rollback_savepoint, name, context)

拦截rollback_savepoint()事件。

rollback_twophase connrollback_twophasexidis_prepared t5 > ) T6> ¶ T7>

拦截rollback_twophase()事件。

savepoint(conn, savepoint, name=None)

截取保存点()事件。

连接池事件

class sqlalchemy.interfaces。 PoolListener

钩入Pool中连接的生命周期。

注意

PoolListener is deprecated. 请参阅PoolEvents

用法:

class MyListener(PoolListener):
    def connect(self, dbapi_con, con_record):
        '''perform connect operations'''
    # etc.

# create a new pool with a listener
p = QueuePool(..., listeners=[MyListener()])

# add a listener after the fact
p.add_listener(MyListener())

# usage with create_engine()
e = create_engine("url://", listeners=[MyListener()])

所有标准连接Pool类型都可以接受关键连接生命周期事件的事件侦听器:创建,池检出和检入。连接关闭时不会触发事件。

For any given DB-API connection, there will be one connect event, n number of checkout events, and either n or n - 1 checkin events. (如果通过detach()方法将Connection从其池中分离出来,它将不会被重新签入。)

这些是低级对象的低级事件:原始Python DB-API连接,没有SQLAlchemy Connection包装,Dialect服务或ClauseElementNone

事件还会收到一个_ConnectionRecord,这是一个长期存在的内部Pool对象,基本上表示连接池中的“插槽”。_ConnectionRecord对象具有一个值得注意的公共属性:info,这是一个字典,其内容的范围为记录管理的DB-API连接的生存期。不过,您可以使用此共享存储区域。

没有必要继承PoolListener来处理事件。任何实现一个或多个这些方法的类都可以用作池监听器。Pool将检查侦听器对象提供的方法,并根据侦听器的能力将侦听器添加到一个或多个内部事件队列中。就效率和函数调用开销而言,仅仅为您将要使用的钩子提供实现会更好。

checkin dbapi_concon_record

连接返回到池时调用。

请注意,连接可能已关闭,如果连接已失效,则可能为无。checkin will not be called for detached connections. (他们不回到游泳池。)

dbapi_con
原始的DB-API连接
con_record
持续管理连接的_ConnectionRecord
结帐 dbapi_concon_recordcon_proxy ¶ T6>

从池中检索连接时调用。

dbapi_con
原始的DB-API连接
con_record
持续管理连接的_ConnectionRecord
con_proxy
_ConnectionFairy管理当前结帐范围的连接。

如果您引发exc.DisconnectionError,则将丢弃当前连接并检索新的连接。处理所有结帐侦听器将中止并使用新连接重新启动。

连接 tt> dbapi_concon_record

为每个新的DB-API连接或池的creator()调用一次。

dbapi_con
新连接的原始DB-API连接(不是SQLAlchemy Connection包装器)。
con_record
持续管理连接的_ConnectionRecord
first_connect(dbapi_con, con_record)

第一次为DB-API连接调用一次。

dbapi_con
新连接的原始DB-API连接(不是SQLAlchemy Connection包装器)。
con_record
持续管理连接的_ConnectionRecord