Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
django-pgtransaction
Advanced tools
A context manager/decorator which extends Django's atomic function with the ability to set isolation level and retries for a given transaction.
django-pgtransaction
offers a drop-in replacement for the default django.db.transaction
module which, when used on top of a PostgreSQL database, extends the functionality of that module with Postgres-specific features.
At present, django-pgtransaction offers an extension of the django.db.transaction.atomic
context manager/decorator which allows one to dynamically set the isolation level when opening a transaction, as well as specifying a retry policy for when an operation in that transaction results in a Postgres locking exception. See the quickstart below or the docs for examples.
Set the isolation level of a transaction by using pgtransaction.atomic
:
import pgtransaction
with pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE):
# Do queries...
There are three isolation levels: pgtransaction.READ_COMMITTED
, pgtransaction.REPEATABLE_READ
, and pgtransaction.SERIALIZABLE
. By default it inherits the parent isolation level, which is Django's default of "READ COMMITTED".
When using stricter isolation levels like pgtransaction.SERIALIZABLE
, Postgres will throw serialization errors upon concurrent updates to rows. Use the retry
argument with the decorator to retry these failures:
@pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE, retry=3)
def do_queries():
# Do queries...
Note that the retry
argument will not work when used as a context manager. A RuntimeError
will be thrown.
By default, retries are only performed when psycopg.errors.SerializationError
or psycopg.errors.DeadlockDetected
errors are raised. Configure retried psycopg errors with settings.PGTRANSACTION_RETRY_EXCEPTIONS
. You can set a default retry amount with settings.PGTRANSACTION_RETRY
.
pgtransaction.atomic
can be nested, but keep the following in mind:
RuntimeError
is raised.django-pgtransaction
is compatible with Python 3.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.
Check out the Postgres docs to learn about transaction isolation in Postgres.
View the django-pgtransaction docs here
Install django-pgtransaction
with:
pip3 install django-pgtransaction
After this, add pgtransaction
to the INSTALLED_APPS
setting of your Django project.
For information on setting up django-pgtransaction for development and contributing changes, view CONTRIBUTING.md.
FAQs
A context manager/decorator which extends Django's atomic function with the ability to set isolation level and retries for a given transaction.
We found that django-pgtransaction demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.