psycopg2-managed-connection
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
Metadata
Release files for psycopg2-managed-connection 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| psycopg2-managed-connection-1.0.0.tar.gz | 3.4 kB | Without provenance | Details for psycopg2-managed-connection-1.0.0.tar.gz |
Release files / psycopg2-managed-connection-1.0.0.tar.gz
| Download URL | psycopg2-managed-connection-1.0.0.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5159e4ffaf9bb102c5d7d0e2a7bfff8e7e422aa9a06c282558db5b456d6a1215
|
|
BLAKE2b-256 checksum How to use checksums |
517e23a9b380daeda1ebc1fe127cc4d1fe49eba87aaae853e904f05ede820972
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for psycopg2-managed-connection 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.