question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django - support for transaction.atomic

See original GitHub issue

Hey 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:open
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ewjoachimcommented, Jan 31, 2022

I think the easiest way to make it work for Django is to rewrite the procrastinate worker in a synchronous way

Yes, I think so too.

1reaction
michaszczcommented, Dec 21, 2021

I’ve created some draft which we can discuss

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found