Skip to main content

Thread-safe connection manager for psycopg2 connections.

Project description

This package includes a managed connection for psycopg2 that provides thread-safe exclusive access to an underlying psycopg2.connection object.

This allows many threads to share the same connection instance (avoiding the TCP and process startup cost associated with establishing a new PostgreSQL connection) and ensures that threads do not release the connection with a transaction still in progress – either due to developer error, or an unhandled exception while a thread is interacting with the database.

ManagedConnection also will ensure that a connection that is closed upon entering the managed context will be opened.

Usage

Creating a Managed Connection

>>> from pgmanagedconnection import ManagedConnection
>>>
>>> dsn = 'postgres:///postgres'  # a libpq connection string
>>> manager = ManagedConnection(dsn)
>>> manager
<ManagedConnection: postgres:///postgres (closed)>

Making Queries

>>> with manager() as connection:
...     cursor = connection.cursor()
...     cursor.execute('SELECT 1')
...     cursor.fetchone()
...     connection.commit()
(1,)

Dealing with Uncommitted Transactions

Leaving a transaction open when exiting the context manager will lead to a RuntimeError. This also results in any open transactions implicitly being rolled back.

>>> with manager() as connection:
...    cursor = connection.cursor()
...    cursor.execute('SELECT 1')
Traceback (most recent call last):
    ...
RuntimeError: Did not commit or rollback open transaction before releasing connection.

Dealing with Errors

The currently open transaction will be rolled back if an unhandled exception is encountered.

>>> import psycopg2
>>> with manager() as connection:
...    cursor = connection.cursor()
...    cursor.execute('SELECT 1')
...    assert manager.status is psycopg2.extensions.TRANSACTION_STATUS_INTRANS
...    raise NotImplementedError()
Traceback (most recent call last):
    ...
NotImplementedError
>>> manager.status is psycopg2.extensions.TRANSACTION_STATUS_IDLE
True

Development

Testing

The test suite can be run with make test.

It assumes a running and accessible PostgreSQL server. The connection details are deferred to the underlying libpq implementation, and default values can be specified using libpq environment variables.

tox is also supported as a test runner (if installed.)

Testing with Docker

$ export PGPORT=5432
$ docker run -dp $PGPORT:5432 postgres
$ PGUSER=postgres make test

If using boot2docker, the PGHOST environment variable will also need to be set to the virtual machine IP address.

$ PGUSER=postgres PGHOST=$(boot2docker ip) make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psycopg2-managed-connection-1.0.0.tar.gz (3.4 kB view details)

Uploaded Source

File details

Details for the file psycopg2-managed-connection-1.0.0.tar.gz.

File metadata

File hashes

Hashes for psycopg2-managed-connection-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5159e4ffaf9bb102c5d7d0e2a7bfff8e7e422aa9a06c282558db5b456d6a1215
MD5 8874d826b1fe51981829e997ff7fa550
BLAKE2b-256 517e23a9b380daeda1ebc1fe127cc4d1fe49eba87aaae853e904f05ede820972

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page