In this document, we will talk about Django transactions. We will explain why transactions are a problem in a multi-db scenario and propose a way to solve this problem.
Django has a useful library to handle database transactions called transcation. From this library, Synnefo uses two decorators: commit_on_success and commit_manually.
Using these decorators as-is is fine when the Cyclades/Astakos nodes have to deal with one database. In the case of two or more databases however, the user needs to add the using argument in these decorators to define the database that will be used to open a transaction. Else, they use the ‘default’ database.
The most straight-forward solution is to add in all decorators a using argument with cyclades or astakos as database names, and enforce that all settings from now on will have these two entries.
The only issue with this approach is that the core problem, that is, how to chose between multiple databases in the application level, will be solved with two different ways. The one way is with the using argument in transactions, while the other one is with database routers.
Therefore, we propose a solution for the transaction problem that converges with database routers.
The solution is the following:
To sum up, the only changes we need to do is to add a transaction.py in Astakos and Cyclades, write the wrappers and replace this import:
from django.db import transaction
with the Astakos/Cyclades transaction.py file, in any code that uses the commit_on_success/commit_manually decorators.