# SQLAlchemy 0.6.0 Documentation

Search:
Version: 0.6.0 Last Updated: 04/18/2010 19:16:18

# Connection Pooling

SQLAlchemy ships with a connection pooling framework that integrates with the Engine system and can also be used on its own to manage plain DB-API connections.

At the base of any database helper library is a system for efficiently acquiring connections to the database. Since the establishment of a database connection is typically a somewhat expensive operation, an application needs a way to get at database connections repeatedly without incurring the full overhead each time. Particularly for server-side web applications, a connection pool is the standard way to maintain a group or “pool” of active database connections which are reused from request to request in a single server process.

## Connection Pool Configuration

The `Engine` returned by the `create_engine()` function in most cases has a `QueuePool` integrated, pre-configured with reasonable pooling defaults. If you’re reading this section to simply enable pooling- congratulations! You’re already done.

The most common `QueuePool` tuning parameters can be passed directly to `create_engine()` as keyword arguments: `pool_size`, `max_overflow`, `pool_recycle` and `pool_timeout`. For example:

    engine = create_engine('postgresql://me@localhost/mydb',
                           pool_size=20, max_overflow=0)

In the case of SQLite, a `SingletonThreadPool` is provided instead, to provide compatibility with SQLite’s restricted threading model.

## Custom Pool Construction

`Pool` instances may be created directly for your own use or to supply to `sqlalchemy.create_engine()` via the `pool=` keyword argument.

Constructing your own pool requires supplying a callable function the Pool can use to create new connections. The function will be called with no arguments.

Through this method, custom connection schemes can be made, such as a using connections from another library’s pool, or making a new connection that automatically executes some initialization commands:

    import sqlalchemy.pool as pool
    import psycopg2

    def getconn():
        c = psycopg2.connect(username='ed', host='127.0.0.1', dbname='test')
        # execute an initialization function on the connection before returning
        c.cursor.execute("setup_encodings()")
        return c

    p = pool.QueuePool(getconn, max_overflow=10, pool_size=5)

Or with SingletonThreadPool:

    import sqlalchemy.pool as pool
    import sqlite

    p = pool.SingletonThreadPool(lambda: sqlite.connect(filename='myfile.db'))

## Builtin Pool Implementations

class `sqlalchemy.pool.``AssertionPool`(*args, **kw)

  Bases: `sqlalchemy.pool.Pool`

  A Pool that allows at most one checked out connection at any given time.

  This will raise an exception if more than one connection is checked out at a time. Useful for debugging code that is using more connections than desired.

  `__init__`(*args, **kw)

class `sqlalchemy.pool.``NullPool`(creator, recycle=-1, echo=None, use_threadlocal=False, logging_name=None, reset_on_return=True, listeners=None)

  Bases: `sqlalchemy.pool.Pool`

  A Pool which does not pool connections.

  Instead it literally opens and closes the underlying DB-API connection per each connection open/close.

  Reconnect-related functions such as `recycle` and connection invalidation are not supported by this Pool implementation, since no connections are held persistently.

class `sqlalchemy.pool.``Pool`(creator, recycle=-1, echo=None, use_threadlocal=False, logging_name=None, reset_on_return=True, listeners=None)

  Bases: `sqlalchemy.log.Identified`

  Abstract base class for connection pools.

  `__init__`(creator, recycle=-1, echo=None, use_threadlocal=False, logging_name=None, reset_on_return=True, listeners=None)

    Construct a Pool.

    Parameters:

    - creator – a callable function that returns a DB-API connection object. The function will be called with parameters.
    - recycle – If set to non -1, number of seconds between connection recycling, which means upon checkout, if this timeout is surpassed the connection will be closed and replaced with a newly opened connection. Defaults to -1.
    - logging_name – String identifier which will be used within the “name” field of logging records generated within the “sqlalchemy.pool” logger. Defaults to a hexstring of the object’s id.
    - echo – If True, connections being pulled and retrieved from the pool will be logged to the standard output, as well as pool sizing information. Echoing can also be achieved by enabling logging for the “sqlalchemy.pool” namespace. Defaults to False.
    - use_threadlocal – If set to True, repeated calls to `connect()` within the same application thread will be guaranteed to return the same connection object, if one has already been retrieved from the pool and has not been returned yet. Offers a slight performance advantage at the cost of individual transactions by default. The `unique_connection()` method is provided to bypass the threadlocal behavior installed into `connect()`.
    - reset_on_return – If true, reset the database state of connections returned to the pool. This is typically a ROLLBACK to release locks and transaction resources. Disable at your own peril. Defaults to True.
    - listeners – A list of `PoolListener`-like objects or dictionaries of callables that receive events when DB-API connections are created, checked out and checked in to the pool.

  `add_listener`(listener)

    Add a `PoolListener`-like object to this pool.

    `listener` may be an object that implements some or all of PoolListener, or a dictionary of callables containing implementations of some or all of the named methods in PoolListener.

  `connect`()
  `create_connection`()
  `dispose`()

    Dispose of this pool.

    This method leaves the possibility of checked-out connections remaining open, It is advised to not reuse the pool once dispose() is called, and to instead use a new pool constructed by the recreate() method.

  `do_get`()
  `do_return_conn`(conn)
  `get`()
  `recreate`()
    Return a new instance with identical creation arguments.
  `return_conn`(record)
  `status`()
  `unique_connection`()

class `sqlalchemy.pool.``QueuePool`(creator, pool_size=5, max_overflow=10, timeout=30, **kw)

  Bases: `sqlalchemy.pool.Pool`

  A Pool that imposes a limit on the number of open connections.

  `__init__`(creator, pool_size=5, max_overflow=10, timeout=30, **kw)

    Construct a QueuePool.

    Parameters:

    - creator – a callable function that returns a DB-API connection object. The function will be called with parameters.
    - pool_size – The size of the pool to be maintained. This is the largest number of connections that will be kept persistently in the pool. Note that the pool begins with no connections; once this number of connections is requested, that number of connections will remain. Defaults to 5.
    - max_overflow – The maximum overflow size of the pool. When the number of checked-out connections reaches the size set in pool_size, additional connections will be returned up to this limit. When those additional connections are returned to the pool, they are disconnected and discarded. It follows then that the total number of simultaneous connections the pool will allow is pool_size + max_overflow, and the total number of “sleeping” connections the pool will allow is pool_size. max_overflow can be set to -1 to indicate no overflow limit; no limit will be placed on the total number of concurrent connections. Defaults to 10.
    - timeout – The number of seconds to wait before giving up on returning a connection. Defaults to 30.
    - recycle – If set to non -1, number of seconds between connection recycling, which means upon checkout, if this timeout is surpassed the connection will be closed and replaced with a newly opened connection. Defaults to -1.
    - echo – If True, connections being pulled and retrieved from the pool will be logged to the standard output, as well as pool sizing information. Echoing can also be achieved by enabling logging for the “sqlalchemy.pool” namespace. Defaults to False.
    - use_threadlocal – If set to True, repeated calls to `connect()` within the same application thread will be guaranteed to return the same connection object, if one has already been retrieved from the pool and has not been returned yet. Offers a slight performance advantage at the cost of individual transactions by default. The `unique_connection()` method is provided to bypass the threadlocal behavior installed into `connect()`.
    - reset_on_return – If true, reset the database state of connections returned to the pool. This is typically a ROLLBACK to release locks and transaction resources. Disable at your own peril. Defaults to True.
    - listeners – A list of `PoolListener`-like objects or dictionaries of callables that receive events when DB-API connections are created, checked out and checked in to the pool.

class `sqlalchemy.pool.``SingletonThreadPool`(creator, pool_size=5, **kw)

  Bases: `sqlalchemy.pool.Pool`

  A Pool that maintains one connection per thread.

  Maintains one connection per each thread, never moving a connection to a thread other than the one which it was created in.

  This is used for SQLite, which both does not handle multithreading by default, and also requires a singleton connection if a :memory: database is being used.

  Options are the same as those of `Pool`, as well as:

  Parameter: | pool_size – The number of threads in which to maintain connections at once. Defaults to five.

  `__init__`(creator, pool_size=5, **kw)
  `dispose`()
    Dispose of this pool.

class `sqlalchemy.pool.``StaticPool`(creator, recycle=-1, echo=None, use_threadlocal=False, logging_name=None, reset_on_return=True, listeners=None)

  Bases: `sqlalchemy.pool.Pool`

  A Pool of exactly one connection, used for all requests.

  Reconnect-related functions such as `recycle` and connection invalidation (which is also used to support auto-reconnect) are not currently supported by this Pool implementation but may be implemented in a future release.

## Pooling Plain DB-API Connections

Any PEP 249 DB-API module can be “proxied” through the connection pool transparently. Usage of the DB-API is exactly as before, except the `connect()` method will consult the pool. Below we illustrate this with `psycopg2`:

    import sqlalchemy.pool as pool
    import psycopg2 as psycopg

    psycopg = pool.manage(psycopg)

    # then connect normally
    connection = psycopg.connect(database='test', username='scott',
                                 password='tiger')

This produces a `_DBProxy` object which supports the same `connect()` function as the original DB-API module. Upon connection, a connection proxy object is returned, which delegates its calls to a real DB-API connection object. This connection object is stored persistently within a connection pool (an instance of `Pool`) that corresponds to the exact connection arguments sent to the `connect()` function.

The connection proxy supports all of the methods on the original connection object, most of which are proxied via `__getattr__()`. The `close()` method will return the connection to the pool, and the `cursor()` method will return a proxied cursor object. Both the connection proxy and the cursor proxy will also return the underlying connection to the pool after they have both been garbage collected, which is detected via weakref callbacks (`__del__` is not used).

Additionally, when connections are returned to the pool, a `rollback()` is issued on the connection unconditionally. This is to release any locks still held by the connection that may have resulted from normal activity.

By default, the `connect()` method will return the same connection that is already checked out in the current thread. This allows a particular connection to be used in a given thread without needing to pass it around between functions. To disable this behavior, specify `use_threadlocal=False` to the `manage()` function.

`sqlalchemy.pool.``manage`(module, **params)

  Return a proxy for a DB-API module that automatically pools connections.

  Given a DB-API 2.0 module and pool management parameters, returns a proxy for the module that will automatically pool connections, creating new connection pools for each distinct set of connection arguments sent to the decorated module’s connect() function.

  Parameters:

  - module – a DB-API 2.0 database module
  - poolclass – the class used by the pool module to provide pooling. Defaults to `QueuePool`.
  - **params – will be passed through to poolclass

`sqlalchemy.pool.``clear_managers`()

  Remove all current DB-API 2.0 managers.

  All pools and connections are disposed.