Skip to content

wemamawe/psycopg2-managed-connection

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

psycopg2-managed-connection

Latest version released on PyPi

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.

Installation

This package is released on PyPI as psycopg2-managed-connection and can be installed via pip:

$ pip install psycopg2-managed-connection

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. This will run the test suite using the version used by your local python command. You can force usage of python3 by running make test3.

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

About

Thread-safe connection manager for psycopg2 connections.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 98.9%
  • Makefile 1.1%