Multi-DB support within transaction middleware

166 views
Skip to first unread message

David Winterbottom

unread,
Oct 18, 2011, 4:22:20 PM10/18/11
to django-d...@googlegroups.com
The current implementation of django.middleware.TransactionMiddleware does not create a transaction for each database, only the default one defined by DEFAULT_DB_ALIAS - https://code.djangoproject.com/browser/django/trunk/django/db/utils.py.  Shouldn't the middleware manage a transaction for each database defined in the DATABASES setting?

I discovered this while debugging some caching issues with the johnnycache library, which saves cached querysets when committing.  In my case, all requests to databases other than default were not being cached.

Happy to supply a patch and tests if this is sensible.
--
Dr. David Winterbottom
Head of Programming

Tangent Labs
84-86 Great Portland Street
London W1W 7NR
England, UK


Calvin Spealman

unread,
Nov 12, 2011, 12:46:49 PM11/12/11
to django-d...@googlegroups.com
I'm not sure this is so cut and dry. For two reasons:

1) If a project has a lot of databases configured, created and ending a transaction in all of them for every request/response cycle could get expensive, and following that:

2) This is enough of a change in behavior that it shouldn't just be fixed in the existing middleware without some kind of control over it. Maybe some flag in settings as to which databases the transaction middleware affects, with the default database being the default?

Yo-Yo Ma

unread,
Nov 13, 2011, 2:05:37 PM11/13/11
to Django developers
Before voting for more magic to the transaction middleware, I'd vote
to remove it altogether. Explicit is surely better than implicit in
this case. The admin already uses commit_on_success, etc.

On Oct 18, 1:22 pm, David Winterbottom
<david.winterbot...@tangentlabs.co.uk> wrote:
> The current implementation of django.middleware.TransactionMiddleware does
> not create a transaction for each database, only the default one
> defined by DEFAULT_DB_ALIAS
> -https://code.djangoproject.com/browser/django/trunk/django/db/utils.py.
>  Shouldn't the middleware manage a transaction for each database defined in
> the DATABASES setting?
>
> I discovered this while debugging some caching issues with the johnnycache
> library, which saves cached querysets when committing.  In my case, all
> requests to databases other than default were not being cached.
>
> Happy to supply a patch and tests if this is sensible.
> --
> *Dr. David Winterbottom*

David Winterbottom

unread,
Nov 14, 2011, 4:30:03 PM11/14/11
to django-d...@googlegroups.com
There's merit in both the responses.  

Since it's trivial to write your own transaction middleware, I would say the most important thing is to document that the middleware only affects your 'default' database.  Then it's up to the developer to decide if they need anything further.

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.





--
Dr. David Winterbottom
Reply all
Reply to author
Forward
0 new messages