In our Sitecore 8.1-3 solution I have created a custom xDB facet with a few different elements. When users log in to our site we call Tracker.Current.Session.Identify(emailAddress);
to properly identify them. I believe that what happens when we identify a user is that Sitecore gets a copy of their current Contact data and puts it in session. (I may be wrong about this)
Then later on our site if a user does certain things we have some custom code that updates some data in their xDB Contact record. The code that we use to update their xDB Contact record looks like this:
var manager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager;
var lockResult = manager.TryLoadContact(_contact.ContactId);
switch (lockResult.Status)
{
case LockAttemptStatus.Success:
var lockedContact = lockResult.Object;
lockedContact.ContactSaveMode = ContactSaveMode.AlwaysSave;
//Update the contact
//Marketing Info
var marketingInfo = lockedContact.GetFacet<IMarketingData>("Marketing Data");
marketingInfo.Organization = txtOrganization.Text;
marketingInfo.Industries = txtIndustries.Text;
marketingInfo.ProductTypes = txtProductTypes.Text;
manager.SaveAndReleaseContactToXdb(lockedContact);
break;
case LockAttemptStatus.NotFound:
Log.Info("LockAttempStatus.NotFound", this);
break;
default:
Log.Info("Contact could not be locked", this);
break;
}
So when that code runs it immediately updates the contact record in xDB. I have used RoboMongo to verify this. However what I have noticed is that if I let the user's session expire that the contact record in xDB seems to revert back to the data that was there before. I am guessing that Sitecore keeps a copy of the contact record in session and when the session expires it writes it back to xDB and overwrites the changes that my code made.
Is there a way to prevent this? I am thinking that maybe there is some way after the manager.SaveAndReleaseContactToXdb
line of code that I could update the contact data stored in session so that it now has the updated data.