Sitecore Links Database Learning

Challenge:

While working on a project, we faced issue with item references which are stored in Links table of the database specified as LinkDatabase using defaultLinkDatabaseConnectionStringName variable in Sitecore.config.

Learning:

The project is built on Sitecore 9.0.2 and hosted on Azure PaaS as Web App with the setup of 1 CM and 3 CD instances. All instances share Master and Core databases while each instance has its own web database.

First, we rebuilt link databases but it did not work. Then we created a tool which retrieves and updates item referrers using Sitecore Links API:

public void GetItemReferrers(string itempath)
{
    try
    {
        var dbName = "web";
        var db = Sitecore.Configuration.Factory.GetDatabase(dbName);
        var item = db.GetItem(itempath);
        var links = Sitecore.Globals.LinkDatabase.GetReferrers(item);
        foreach (var aLink in links)
            System.Web.HttpContext.Current.Response.Write(aLink.TargetPath);
        var linkedItems = links.Select(i => i.GetSourceItem()).Where(i => i != null);
        foreach (var aItem in linkedItems)
            System.Web.HttpContext.Current.Response.Write(aItem.Paths.Path);
     }
     catch (Exception ex)
     {
         System.Web.HttpContext.Current.Response.Write("Print : " + ex.ToString());
     }
}
public void UpdateItemReferences(string itempath)
{
     try
     {
         var dbName = "web";
         var db = Sitecore.Configuration.Factory.GetDatabase(dbName);
         var item = db.GetItem(itempath);
         Sitecore.Globals.LinkDatabase.UpdateReferences(item);
     }
     catch (Exception ex)
     {
          System.Web.HttpContext.Current.Response.Write("Update : " + ex.ToString());
     }
}

So, we used this tool for one of the item and found that there were item referrers on CM but no referrer was found on CD instances.

Then we checked link database configuration using showconfig.aspx admin tool and found that web was the value of variable defaultLinkDatabaseConnectionStringName.

We found following configuration in Sitecore.config:

1
2
3
4
<sc.variable name="defaultLinkDatabaseConnectionStringName" value="core" />
<sc.variable name="defaultLinkDatabaseConnectionStringName" role:require="ContentDelivery or ContentManagement">
    <patch:attribute name="value">web</patch:attribute>
</sc.variable>

The default links database is the core database to store all entries, but for scaled environment web is specified.

What was causing issue in our case?

Sitecore maintains all item references in Links table. It does not matter how many databases are processed, all entries are stored in a single table in the database which is configured as Link Database.

As per above configuration web was the default link database in our case. So, when any reference was updated or we rebuilt link databases from CM Control Panel, all entries were stored in web database. As each instance has its own web database, we were not getting referrers on CD servers as links were stored in web database of CM instance.

Solution:

We changed value of defaultLinkDatabaseConnectionStringName to core again as it is shared by CM and CD instances. Then we rebuilt links database and resolved the issue.

Patch to change defaultLinkDatabaseConnectionStringName variable:

1
2
3
4
5
<sitecore>
    <sc.variable name="defaultLinkDatabaseConnectionStringName">
      <patch:attribute name="value">core</patch:attribute>
    </sc.variable>
</sitecore>

Also, Thanks to Kiran Patil and Sreekrishnan for helping to resolve this.

Reference to understand Links Database:

http://sitecore-masters.com/en/link-database/

Advertisements

Tagged: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: