From 64d4c4c09f3af3bc2e8c78babe32000e8e4e4c09 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 23 Nov 2009 16:43:17 +0000 Subject: [PATCH] [soc2009/multidb] Updated db-backed session to be multi-db compatible. Patch from Russell Keith-Magee. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11766 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- TODO | 7 ++++--- django/contrib/sessions/backends/db.py | 9 +++++++-- docs/ref/settings.txt | 12 ++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 23ce75c8d6..d7aaa82dfe 100644 --- a/TODO +++ b/TODO @@ -7,12 +7,13 @@ Required for v1.2 * Finalize the sql.Query internals * Clean up the use of db.backend.query_class() * Verify it still works with GeoDjango - * Resolve internal uses of multidb interface - * Update database backend for session store to use Multidb - * Check default Site creation behavior * Resolve the public facing UI issues around using multi-db * Should we take the opportunity to modify DB backends to use fully qualified paths? + * Should we clean up DATABASES['DATABASE_NAME'] to DATABASES['NAME'] etc? * Meta.using? Is is still required/desirable? + * Fix the regressiontests/multiple_database test failures + * Give instances knowledge of the database from which they were loaded. + * Cascade instance using to m2m queries * Cleanup of new API entry points * validate() on a field * name/purpose clash with Honza? diff --git a/django/contrib/sessions/backends/db.py b/django/contrib/sessions/backends/db.py index f31c292d36..4ece18ca37 100644 --- a/django/contrib/sessions/backends/db.py +++ b/django/contrib/sessions/backends/db.py @@ -1,4 +1,5 @@ import datetime +from django.conf import settings from django.contrib.sessions.models import Session from django.contrib.sessions.backends.base import SessionBase, CreateError from django.core.exceptions import SuspiciousOperation @@ -9,6 +10,10 @@ class SessionStore(SessionBase): """ Implements database session store. """ + def __init__(self, session_key=None): + self.using = getattr(settings, "SESSION_DB_ALIAS", DEFAULT_DB_ALIAS) + super(SessionStore, self).__init__(session_key) + def load(self): try: s = Session.objects.get( @@ -54,12 +59,12 @@ class SessionStore(SessionBase): expire_date = self.get_expiry_date() ) # TODO update for multidb - sid = transaction.savepoint(using=DEFAULT_DB_ALIAS) + sid = transaction.savepoint(using=self.using) try: obj.save(force_insert=must_create) except IntegrityError: if must_create: - transaction.savepoint_rollback(sid, using=DEFAULT_DB_ALIAS) + transaction.savepoint_rollback(sid, using=self.using) raise CreateError raise diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 6205777087..c93a0d067e 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -1056,6 +1056,18 @@ See the :ref:`topics-http-sessions`. .. setting:: SESSION_EXPIRE_AT_BROWSER_CLOSE +SESSION_DB_ALIAS +---------------- + +.. versionadded:: 1.2 + +Default: ``None`` + +If you're using database-backed session storage, this selects the database +alias that will be used to store session data. By default, Django will use +the ``default`` database, but you can store session data on any database +you choose. + SESSION_EXPIRE_AT_BROWSER_CLOSE -------------------------------