Thanks Russ. I too thought it might be the loop to allocate a new session 
key, but after studying the code and trying some forced debugs (print x) I 
thought it could be due to the super __init__ spinning when it couldnt find 
the session key in the db. I can access and insert into the DB table during 
the lock, and the lock never seems to end (I've run it for atleast an hour 
with no stop). We've built out our stuff exclusively on 1.5 so shouldnt be 
any 1.4 migration issues. Here is the session middleware:


from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.exceptions import SuspiciousOperation
from django.db import IntegrityError, transaction, router

"""
 startlm/backend.py
 Custom session backend for django.
 Talks to our lm_sessions table.

 Started November 22, 2012
 By Wade Williams
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 """


class SessionStore(SessionBase):
    """
    Implements database session store on lm_sessions table.
    Subtle differences:
    lm_sessions model is referred to as LM_Session, not Session.
    lm_session primary key field is session_id, not session_key
    So some potentially confusing details here.

    As of Now 11/24/12 the lm_sessions table does not support sessions
    expiring on time, so expire_date information is commented out.
    """
    def __init__(self, session_key=None, request=None):
        super(SessionStore, self).__init__(session_key)
        self.request = request

    def load(self):
        try:
            s = LM_Session.objects.get(
                session_id=self.session_key,
                #expire_date__gt=timezone.now()
            )
        except:
            self.create()
            return {}
        else:
            return self.decode(s.session_data)


    def exists(self, session_key):
        return LM_Session.objects.filter(session_id=session_key).exists()

    def create(self):
        while True:
            self._session_key = self._get_new_session_key()
            try:
                # Save immediately to ensure we have a unique entry in the
                # database.
                self.save(must_create=True)
            except CreateError:
                # Key wasn't unique. Try again.
                continue
            self.modified = True
            self._session_cache = {}
            return

    def save(self, must_create=False):
        """
        Saves the current session data to the database. If 'must_create' is
        True, a database error will be raised if the saving operation 
doesn't
        create a *new* entry (as opposed to possibly updating an existing
        entry).
        """

        # Because session middelware runs before authentication,
        # request.user isn't set until the view has been processed.
        uid = self.request.user.id

        if uid is None:
            uid = 0


        obj = LM_Session(
            session_id=self._get_or_create_session_key(),
            
session_data=self.encode(self._get_session(no_load=must_create)),
            session_ip=self.request.CLIENT_IP,
            session_user_agent=self.request.META['HTTP_USER_AGENT'],
            session_user_id=uid,

            #expire_date=self.get_expiry_date()
        )
        using = router.db_for_write(LM_Session, instance=obj)
        sid = transaction.savepoint(using=using)
        try:
            obj.save(force_insert=must_create, using=using)
        except IntegrityError:
            if must_create:
                transaction.savepoint_rollback(sid, using=using)
                raise CreateError
            raise

    def delete(self, session_key=None):
        if session_key is None:
            if self.session_key is None:
                return
            session_key = self.session_key
        try:
            LM_Session.objects.get(session_id=session_key).delete()
        except LM_Session.DoesNotExist:
            pass

    @classmethod
    def clear_expired(cls):
        #Session.objects.filter(expire_date__lt=timezone.now()).delete()
        #transaction.commit_unless_managed()
        pass

# At bottom to avoid circular import
from startlm.models import LM_Session


I can add the LM_Session model and the sessionbackend we're using as well 
but my gut is they are not the issue at hand. 


Thanks for your help.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ISZRfG8ztIcJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to