DBConnectionPool

Manages connections, recycles them if requested to and monitors the connection state.

Class Documenation

class gdbpool.connection_pool.DBConnectionPool(dsn, db_module='psycopg2', pool_size=10, conn_lifetime=600, do_log=False)[source]

Bases: object

The Connection Pool

“Classic” pool of connections with connection lifecycle management

Parameters:
  • dsn (string) – DSN for the default class:DBConnectionPool
  • db_module (string) – name of the DB-API module to use
  • pool_size (int) – Poolsize of the first/default class:DBConnectionPool
  • conn_lifetime (int) – Number of seconds after which a connection will be recycled when put() back
  • do_log (bool) – Log to the console or not
create_connection()[source]

Try to open a new connection to the database and put it on the pool

resize(new_size)

Resize the pool (nr. of connections on the pool)

Parameters:new_size (int) – nr ob connections the pool should be resized to
get(timeout=None, iso_level=2)[source]

Get a connection from the pool

Parameters:
  • timeout (int) – seconds to wait for a connection or None
  • iso_level – transaction isolation level to be set on the connection. Must be one of psycopg2.extensions ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_UNCOMMITTED, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_REPEATABLE_READ, ISOLATION_LEVEL_SERIALIZABLE
Returns:

– a PoolConnection

put(conn, timeout=1, force_recycle=False)[source]

Put a connection back onto the pool

Parameters:
  • conn – The PoolConnection object to be put back onto the pool
  • timeout (int) – timeout in seconds to to put the connection onto the pool
  • force_recycle (bool) – Force connection recycling independent from the pool wide connection lifecycle

Transaction Isolation Level constants

When getting a connection from the pool one can set the transaction isolation level for that connection (the default being the same as postres: ISOLATION_LEVEL_READ_COMMITTED). These are the levelse that can be requested. (See also: 13.2. Transaction Isolation and Isolation level constants)

The isolation level is being reset when the connection is put back onto the pool.

The constants can be imported from psycopg:

from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, \
    ISOLATION_LEVEL_READ_UNCOMMITTED, \
    ISOLATION_LEVEL_READ_COMMITTED, \
    ISOLATION_LEVEL_REPEATABLE_READ, \
    ISOLATION_LEVEL_SERIALIZABLE
ISOLATION_LEVEL_AUTOCOMMIT
ISOLATION_LEVEL_READ_UNCOMMITTED
ISOLATION_LEVEL_READ_COMMITTED
ISOLATION_LEVEL_REPEATABLE_READ
ISOLATION_LEVEL_SERIALIZABLE

Table Of Contents

Previous topic

PGChannelListener

Next topic

DBInteractionPool

This Page