Deprecated Event Interfaces
This section describes the class-based core event interface introduced inSQLAlchemy 0.5. The ORM analogue is described at Deprecated ORM Event Interfaces.
Deprecated since version 0.7: The new event system described in Events replacesthe extension/proxy/listener system, providing a consistent interfaceto all events without the need for subclassing.
Execution, Connection and Cursor Events
- class
sqlalchemy.interfaces.
ConnectionProxy
- Allows interception of statement execution by Connections.
Deprecated since version 0.7: ConnectionProxy
is deprecated and will be removed in a futurerelease. Please refer to event.listen()
in conjunction withthe ConnectionEvents
listener interface.
Either or both of the execute()
and cursor_execute()
may be implemented to intercept compiled statement andcursor level executions, e.g.:
- 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)
The execute
argument is a function that will fulfill the defaultexecution behavior for the operation. The signature illustratedin the example should be used.
The proxy is installed into an Engine
viathe proxy
argument:
- e = create_engine('someurl://', proxy=MyProxy())
begin
(conn, begin)Intercept begin() events.
Intercept begin_twophase() events.
Intercept commit() events.
Intercept commit_twophase() events.
cursorexecute
(_execute, cursor, statement, parameters, context, executemany)Intercept low-level cursor execute() events.
execute
(conn, execute, clauseelement, *multiparams, **params)Intercept high level execute() events.
Intercept prepare_twophase() events.
Intercept release_savepoint() events.
Intercept rollback() events.
Intercept rollback_savepoint() events.
rollbacktwophase
(_conn, rollback_twophase, xid, is_prepared)Intercept rollback_twophase() events.
- Intercept savepoint() events.
Connection Pool Events
- class
sqlalchemy.interfaces.
PoolListener
- Hooks into the lifecycle of connections in a
Pool
.
Deprecated since version 0.7: PoolListener
is deprecated and will be removed in a futurerelease. Please refer to event.listen()
in conjunction withthe PoolEvents
listener interface.
Usage:
- 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()])
All of the standard connection Pool
types canaccept event listeners for key connection lifecycle events:creation, pool check-out and check-in. There are no events firedwhen a connection closes.
For any given DB-API connection, there will be one connect
event, n number of checkout
events, and either n or n - 1checkin
events. (If a Connection
is detached from itspool via the detach()
method, it won’t be checked back in.)
These are low-level events for low-level objects: raw PythonDB-API connections, without the conveniences of the SQLAlchemyConnection
wrapper, Dialect
services or ClauseElement
execution. If you execute SQL through the connection, explicitlyclosing all cursors and other resources is recommended.
Events also receive a _ConnectionRecord
, a long-lived internalPool
object that basically represents a “slot” in theconnection pool. _ConnectionRecord
objects have one publicattribute of note: info
, a dictionary whose contents arescoped to the lifetime of the DB-API connection managed by therecord. You can use this shared storage area however you like.
There is no need to subclass PoolListener
to handle events.Any class that implements one or more of these methods can be usedas a pool listener. The Pool
will inspect the methodsprovided by a listener object and add the listener to one or moreinternal event queues based on its capabilities. In terms ofefficiency and function call overhead, you’re much better off onlyproviding implementations for the hooks you’ll be using.
Note that the connection may be closed, and may be None if theconnection has been invalidated. checkin
will not be calledfor detached connections. (They do not return to the pool.)
- dbapi_con
-
A raw DB-API connection
- con_record
-
The _ConnectionRecord
that persistently manages the connection
checkout
(dbapi_con, con_record, con_proxy)Called when a connection is retrieved from the Pool.
- dbapi_con
A raw DB-API connection
con_record
The
_ConnectionRecord
that persistently manages the connectioncon_proxy
- The
_ConnectionFairy
which manages the connection for the span ofthe current checkout.
If you raise an exc.DisconnectionError
, the currentconnection will be disposed and a fresh connection retrieved.Processing of all checkout listeners will abort and restartusing the new connection.
connect
(dbapi_con, con_record)Called once for each new DB-API connection or Pool’s
creator()
.- dbapi_con
A newly connected raw DB-API connection (not a SQLAlchemy
Connection
wrapper).con_record
- The
_ConnectionRecord
that persistently manages the connection
Called exactly once for the first DB-API connection.
- dbapi_con
A newly connected raw DB-API connection (not a SQLAlchemy
Connection
wrapper).con_record
- The
_ConnectionRecord
that persistently manages the connection