Django transactions and multiple database issue


Here is my multi-tenant django setup, with

  1. One database per client,
  2. Database routing implemented using thread local variables which tells the client database to use, which is set on middleware for every request.
  3. Each django specific models are also present on these isolated databases (Like user database, content type model

The main issue comes with using django transactions, and decorator @transaction.atomic()

The using parameter on atomic decorator, if not set, won’t roll back if database operations specific to client X are being executed.

These are the things im hoping to achieve.

  1. I want transaction to be opened specific to each client on the request, and must look at thread local variable for opening transaction without modifying django itself Or does django provide an easy option for this kind of usecase?.

  2. I want transactions to use thread local or database router when 3rd party libraries use @transaction.atomic decorator

I know i could use context based transaction opening, getting thread local variable and putting them as using argument, but this doesn’t work on 3rd party packages which already use @transaction.atomic decorator