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

  1. class MyProxy(ConnectionProxy):
  2. def execute(self, conn, execute, clauseelement,
  3. *multiparams, **params):
  4. print "compiled statement:", clauseelement
  5. return execute(clauseelement, *multiparams, **params)
  6.  
  7. def cursor_execute(self, execute, cursor, statement,
  8. parameters, context, executemany):
  9. print "raw statement:", statement
  10. 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:

  1. e = create_engine('someurl://', proxy=MyProxy())
  • begin(conn, begin)
  • Intercept begin() events.

  • begintwophase(_conn, begin_twophase, xid)

  • Intercept begin_twophase() events.

  • commit(conn, commit)

  • Intercept commit() events.

  • committwophase(_conn, commit_twophase, xid, is_prepared)

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

  • preparetwophase(_conn, prepare_twophase, xid)

  • Intercept prepare_twophase() events.

  • releasesavepoint(_conn, release_savepoint, name, context)

  • Intercept release_savepoint() events.

  • rollback(conn, rollback)

  • Intercept rollback() events.

  • rollbacksavepoint(_conn, rollback_savepoint, name, context)

  • Intercept rollback_savepoint() events.

  • rollbacktwophase(_conn, rollback_twophase, xid, is_prepared)

  • Intercept rollback_twophase() events.

  • savepoint(conn, savepoint, name=None)

  • 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:

  1. class MyListener(PoolListener):
  2. def connect(self, dbapi_con, con_record):
  3. '''perform connect operations'''
  4. # etc.
  5.  
  6. # create a new pool with a listener
  7. p = QueuePool(..., listeners=[MyListener()])
  8.  
  9. # add a listener after the fact
  10. p.add_listener(MyListener())
  11.  
  12. # usage with create_engine()
  13. 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 connectevent, 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 ClauseElementexecution. 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.

  • checkin(dbapi_con, con_record)
  • Called when a connection returns to the pool.

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

  1. - dbapi_con
  2. -

A raw DB-API connection

  1. - con_record
  2. -

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 connection

    • con_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 SQLAlchemyConnection wrapper).

    • con_record

    • The _ConnectionRecord that persistently manages the connection
  • firstconnect(_dbapi_con, con_record)

  • Called exactly once for the first DB-API connection.

    • dbapi_con
    • A newly connected raw DB-API connection (not a SQLAlchemyConnection wrapper).

    • con_record

    • The _ConnectionRecord that persistently manages the connection