Does anyone know what the "Contact could not be locked in XDB" error is and how to fix it?

I am using Sitecore 7.5 with MongoDB.  We have been building this site for a long time with no issues.  Suddenly today on our development machine we are getting the following error:

Contact 76c1c7c9-4535-4ae6-bd7d-8e22456cf870 could not be locked in the XDB.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Sitecore.Analytics.Exceptions.ContactLockException: Contact 76c1c7c9-4535-4ae6-bd7d-8e22456cf870 could not be locked in the XDB.

I have not been able to find much with a few Google searches.  Does anyone know what this means and how I can fix it?  The error is intermittent.  It comes and goes.

  • Hey Corey,

    exception is throwing from Identify method from class :  Sitecore.Analytics.Tracking.StandardSession

    I can see is trying to load a contact but the result is not a successfully

      LockAttemptResult<Contact> result = manager.TryLoadContact(contactId);

       

       is checking if result is

              LockAttemptStatus.Success

          and if is not success then is throwing the error. 

         

       Can you check if you have contact with that id ?   

       Please check bellow code to understand more why is happening.  I hope my tips will help you a little bit. 

        

    public override void Identify(string userName)
    {
        Assert.ArgumentNotNull(userName, "userName");
        Assert.IsNotNull(this.Contact, "this.Contact should not be null");
        Assert.IsNotNull(this.Interaction, "this.Interaction should not be null");
        Assert.IsNotNull(this.Device, "this.Device should not be null");
        ContactManager manager = Factory.CreateObject("tracking/contactManager", true) as ContactManager;
        ContactRepositoryBase base2 = Factory.CreateObject("contactRepository", true) as ContactRepositoryBase;
        Assert.IsNotNull(base2, "contactRepository");
        Contact contact = base2.LoadContactReadOnly(userName);
        if ((contact == null) || (contact.ContactId == Guid.Empty))
        {
            bool flag = false;
            if (this.Contact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
            {
                if (((this.Contact.ContactSaveMode != ContactSaveMode.NeverSave) && !this.Settings.IsTransient) && (ContactClassification.IsHuman(this.Contact.System.Classification) || ContactClassification.IsAutoDetectedRobot(this.Contact.System.Classification)))
                {
                    if (this.Contact.System.VisitCount > 1)
                    {
                        contact = base2.LoadContactReadOnly(this.Contact.ContactId);
                    }
                    if (contact == null)
                    {
                        contact = base2.CreateContact(this.Contact.ContactId);
                        contact.IsTemporaryInstance = true;
                    }
                    flag = true;
                }
                else
                {
                    contact = this.Contact;
                }
            }
            else
            {
                flag = true;
                ID id = string.Equals(this.Contact.Identifiers.Identifier, userName, StringComparison.InvariantCultureIgnoreCase) ? this.Contact.ContactId.ToID() : ID.NewID;
                contact = base2.CreateContact(id);
            }
            contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known;
            contact.Identifiers.Identifier = userName;
            if (flag)
            {
                manager.FlushContactToXdb(contact);
            }
        }
        if (this.Contact.ContactId == contact.ContactId)
        {
            if (ContactClassification.IsAutoDetectedRobot(this.Contact.System.Classification))
            {
                this.SetClassification(0, this.Contact.System.OverrideClassification, true);
            }
            this.Contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known;
            this.Contact.Identifiers.Identifier = userName;
            Log.Debug("StandardSession.Identify - contact is already loaded: " + this.Contact.ContactId);
        }
        else
        {
            Guid contactId = contact.ContactId;
            LockAttemptResult<Contact> result = manager.TryLoadContact(contactId);
            if ((result.Status == LockAttemptStatus.AlreadyLocked) && (result.LockedBy.Type == LeaseOwnerType.WebCluster))
            {
                Log.Debug("StandardSession.Identify - contact is locked, transferring: " + contactId);
                TransferSessionPipeline.Run(new TransferSessionArgs(new Guid?(contactId), this, result.LockedBy.Identifier, HttpContext.Current.Request.Url));
            }
            else
            {
                if (result.Status != LockAttemptStatus.Success)
                {
                    throw new ContactLockException(string.Format("Contact {0} could not be locked in the XDB.", contactId));
                }
                if (this.Contact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
                {
                    Contact dyingContact = this.Contact;
                    this.Contact = result.Object;
                    base2.MergeContacts(this.Contact, dyingContact);
                    manager.ReleaseContact(dyingContact.ContactId);
                    manager.RemoveFromSession(dyingContact.ContactId);
                    this.Interaction.ContactId = this.Contact.ContactId;
                    this.Interaction.ContactVisitIndex = this.Contact.System.VisitCount;
                    Log.Debug("StandardSession.Identify - merged: " + this.Contact.ContactId);
                }
                else
                {
                    this.Interaction.AcceptModifications();
                    Guid channelId = this.Interaction.ChannelId;
                    CommitSessionPipelineArgs args = new CommitSessionPipelineArgs {
                        Session = this
                    };
                    CommitSessionPipeline.Run(args);
                    this.Contact = result.Object;
                    ICurrentPageContext currentPage = this.Interaction.CurrentPage;
                    HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);
                    this.CreateInteraction(httpContext);
                    Assert.IsNotNull(this.Device, "StandardSession.Device is null.");
                    Assert.IsNotNull(this.Interaction, "StandardSession.Interaction is null.");
                    this.Interaction.DeviceId = this.Device.DeviceId;
                    this.Interaction.ChannelId = channelId;
                    this.Interaction.CreatePage();
                    this.InitializePage(this.Interaction.CurrentPage, currentPage);
                    Assert.IsNotNull(this.Contact, "StandardSession.Contact is null.");
                    Log.Debug("StandardSession.Identify - started new logical visit: " + this.Contact.ContactId);
                }
                this.Device.LastKnownContactId = new Guid?(this.Contact.ContactId);
            }
        }
    }
    
     
    
     
      public override void Identify(string userName)
    {
        Assert.ArgumentNotNull(userName, "userName");
        Assert.IsNotNull(this.Contact, "this.Contact should not be null");
        Assert.IsNotNull(this.Interaction, "this.Interaction should not be null");
        Assert.IsNotNull(this.Device, "this.Device should not be null");
        ContactManager manager = Factory.CreateObject("tracking/contactManager", true) as ContactManager;
        ContactRepositoryBase base2 = Factory.CreateObject("contactRepository", true) as ContactRepositoryBase;
        Assert.IsNotNull(base2, "contactRepository");
        Contact contact = base2.LoadContactReadOnly(userName);
        if ((contact == null) || (contact.ContactId == Guid.Empty))
        {
            bool flag = false;
            if (this.Contact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
            {
                if (((this.Contact.ContactSaveMode != ContactSaveMode.NeverSave) && !this.Settings.IsTransient) && (ContactClassification.IsHuman(this.Contact.System.Classification) || ContactClassification.IsAutoDetectedRobot(this.Contact.System.Classification)))
                {
                    if (this.Contact.System.VisitCount > 1)
                    {
                        contact = base2.LoadContactReadOnly(this.Contact.ContactId);
                    }
                    if (contact == null)
                    {
                        contact = base2.CreateContact(this.Contact.ContactId);
                        contact.IsTemporaryInstance = true;
                    }
                    flag = true;
                }
                else
                {
                    contact = this.Contact;
                }
            }
            else
            {
                flag = true;
                ID id = string.Equals(this.Contact.Identifiers.Identifier, userName, StringComparison.InvariantCultureIgnoreCase) ? this.Contact.ContactId.ToID() : ID.NewID;
                contact = base2.CreateContact(id);
            }
            contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known;
            contact.Identifiers.Identifier = userName;
            if (flag)
            {
                manager.FlushContactToXdb(contact);
            }
        }
        if (this.Contact.ContactId == contact.ContactId)
        {
            if (ContactClassification.IsAutoDetectedRobot(this.Contact.System.Classification))
            {
                this.SetClassification(0, this.Contact.System.OverrideClassification, true);
            }
            this.Contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known;
            this.Contact.Identifiers.Identifier = userName;
            Log.Debug("StandardSession.Identify - contact is already loaded: " + this.Contact.ContactId);
        }
        else
        {
            Guid contactId = contact.ContactId;
            LockAttemptResult<Contact> result = manager.TryLoadContact(contactId);
            if ((result.Status == LockAttemptStatus.AlreadyLocked) && (result.LockedBy.Type == LeaseOwnerType.WebCluster))
            {
                Log.Debug("StandardSession.Identify - contact is locked, transferring: " + contactId);
                TransferSessionPipeline.Run(new TransferSessionArgs(new Guid?(contactId), this, result.LockedBy.Identifier, HttpContext.Current.Request.Url));
            }
            else
            {
                if (result.Status != LockAttemptStatus.Success)
                {
                    throw new ContactLockException(string.Format("Contact {0} could not be locked in the XDB.", contactId));
                }
                if (this.Contact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
                {
                    Contact dyingContact = this.Contact;
                    this.Contact = result.Object;
                    base2.MergeContacts(this.Contact, dyingContact);
                    manager.ReleaseContact(dyingContact.ContactId);
                    manager.RemoveFromSession(dyingContact.ContactId);
                    this.Interaction.ContactId = this.Contact.ContactId;
                    this.Interaction.ContactVisitIndex = this.Contact.System.VisitCount;
                    Log.Debug("StandardSession.Identify - merged: " + this.Contact.ContactId);
                }
                else
                {
                    this.Interaction.AcceptModifications();
                    Guid channelId = this.Interaction.ChannelId;
                    CommitSessionPipelineArgs args = new CommitSessionPipelineArgs {
                        Session = this
                    };
                    CommitSessionPipeline.Run(args);
                    this.Contact = result.Object;
                    ICurrentPageContext currentPage = this.Interaction.CurrentPage;
                    HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);
                    this.CreateInteraction(httpContext);
                    Assert.IsNotNull(this.Device, "StandardSession.Device is null.");
                    Assert.IsNotNull(this.Interaction, "StandardSession.Interaction is null.");
                    this.Interaction.DeviceId = this.Device.DeviceId;
                    this.Interaction.ChannelId = channelId;
                    this.Interaction.CreatePage();
                    this.InitializePage(this.Interaction.CurrentPage, currentPage);
                    Assert.IsNotNull(this.Contact, "StandardSession.Contact is null.");
                    Log.Debug("StandardSession.Identify - started new logical visit: " + this.Contact.ContactId);
                }
                this.Device.LastKnownContactId = new Guid?(this.Contact.ContactId);
            }
        }
    }
    
     
    
  • Check out for MongoDB service. Is it up and running? Try restarting it and check for the presence of any old mongo.lock or mongod.lock file in data directory specified by dbPath. If yes, you might need to repair database. This link will be helpful for repairing database docs.mongodb.org/.../recover-data-following-unexpected-shutdown
  • In reply to Amrita Shukla:

    We are using the cloud based MongoDB provided by Rackspace (ObjectRocket). And I only get this error intermittently. Sometimes things work, sometimes they don't.
  • In reply to Sitecore Climber:

    Ok. Thanks for the info. But do you have any idea what I can do to try and fix this?
  • In reply to Corey Burnett:

    Hi Corey. Did you ever get a fix for this? I'm having the same issue and also using ObjectRocket.
  • In reply to GregCoffman_49567709:

    I too am experiencing the same issue with Object Rocket. These errors sporadically appear in our logs and sometimes cause CPU usage to spike. Is there a way to tell Sitecore to gracefully not try to read/write data to and from MongoDB if it encounters a problem getting responses from the service?