Django - support for transaction.atomic
See original GitHub issueHey guys, thanks for a great library.
Context
Just to give you a context, I’d like to use it in Django project to reliably communicate with other microservices and implement Transactional outbox and Message Relay patterns.
Problem
The problem is that currently it’s not possible to save django models and defer a task in one transaction. Example code below:
import procrastinate
from django.db import transaction
from .models import *
from procrastinate.contrib.django import connector_params
from asgiref.sync import sync_to_async
app = procrastinate.App(
connector=procrastinate.Psycopg2Connector(**connector_params())
)
@app.task
def some_other_task():
print("Hello Word")
@app.task
@sync_to_async # Needed to be able to use Django ORM
@transaction.atomic
def send_order_placed_notification():
some_other_task.defer() # Task is deferred even when the next line throws IntegrityError (transaction rollback)
SomeModel.objects.create()
@transaction.atomic
def some_view(request):
send_order_placed_notification.defer() # Task is deferred even when the next line throws IntegrityError (transaction rollback)
Order.objects.create()
Root cause
Psycopg2Connector/AiopgConnector create separate database connection instead of using django.db.connection
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Database transactions | Django documentation
A transaction is an atomic set of database queries. Even if your program crashes, the database guarantees that either all the changes will...
Read more >Transaction Atomic With Django - Medium
A transaction is an atomic set of database queries. These functions take a using argument which should be the name of a database....
Read more >The trouble with transaction.atomic - David Seddon
How does it work? Under the hood, Django begins a database transaction when transaction.atomic is entered, and does not commit it until the ......
Read more >Django - Rollback save with transaction atomic - Stack Overflow
atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code...
Read more >Transaction Management with Django 1.6 - Real Python
“Django provides a single API to control database transactions. […] Atomicity is the defining property of database transactions. atomic allows us to create...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, I think so too.
I’ve created some draft which we can discuss