Here is my multi-tenant django setup, with
- One database per client,
- Database routing implemented using thread local variables which tells the client database to use, which is set on middleware for every request.
- 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.
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?.
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