Hi Everyone,

Our use case is that we have a writer primary database and read-only 
replica databases. The replicas can sometimes be a bit behind the primary
We have written a Router like this

class CustomRouter:
    def db_for_read(self, model, **hints):
        return 'reader'

    def db_for_write(self, model, **hints):
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return None

So this is all fine for normal queries. But we observed that even within a 
transaction.atomic() block, the read queries go to a different database, 
and write queries go to a different database

So I was hoping *db_for_transaction* proposal may improve this behavior.
On Tuesday, June 1, 2021 at 6:32:44 PM UTC+5:30 f.apo...@gmail.com wrote:

> On Tuesday, June 1, 2021 at 2:35:17 PM UTC+2 gojeta...@gmail.com wrote:
>
>> I don't see any reason for why providing a hook seems so difficult. 
>>
>
> It is more code to maintain, needs tests etc and increases complexity. 
> Just because something is easy on the surface, doesn't mean it will be easy 
> in the longrun.
>
> A simple implementation can be: (From message-3 of this conversation):
>>
>>
>> > #settings.py
>>
>> TRANSACTION_DB_SELECTOR = "path_to_some_callable"
>>
>> #transaction.py
>> ...
>> transaction_db_selector = import_string(settings.TRANSACTION_DB_SELECTOR)
>> def get_connection():
>>     if transaction_db_selector:
>>         using = transaction_db_selector()
>>     if using is None:
>>         using = DEFAULT_DB_ALIAS
>>     return connections[using]
>>
>
> I do not think that completely ignoring the `using` that was passed in by 
> the user would be a very good idea (for the case when there is also a 
> `TRANSACTION_DB_SELECTOR` set).
>
> Cheers,
> Florian
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/d8bd0f76-a2c7-4b66-b66d-c7e1f042e60en%40googlegroups.com.
  • Re:... N Aditya
    • ... N Aditya
      • ... charettes
      • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... Florian Apolloner
      • ... N Aditya
      • ... Aymeric Augustin
      • ... N Aditya
      • ... Florian Apolloner
      • ... N Aditya
      • ... Lokesh Dokara
      • ... N Aditya
      • ... Aymeric Augustin
      • ... N Aditya
      • ... Shai Berger
      • ... Aymeric Augustin
      • ... N Aditya
      • ... Aymeric Augustin
      • ... AJAY
      • ... Florian Apolloner

Reply via email to