Walkthrough: Configuring a shared session state database using the SQL Server provider

Last updated Friday, July 7, 2017 in Sitecore Experience Platform for Administrator, Developer

In shared session-state, all data that can be shared across multiple sessions, for example, the data related to contacts and devices, is collected and saved to the session state database.

You must always configure shared session state whatever your server configuration. For example, you can have a single standalone content delivery server, multiple content delivery servers, or a clustered environment, but you always need to configure session state.

Note

Shared session state is not supported on content management servers.

A contact can make multiple parallel visits to a website in which case each visit has its own private session state. However, some data can be shared between visits, such as device and contact related information.

Information that is shared between parallel visits by the same contact is stored in the shared session-state store. This data is still private to the contact but it is accessible from all current sessions made by the same contact.

You can use either the Sitecore MongoDB or Sitecore SQL Server providers to configure your shared session-state store. Both these providers support the SessionEnd event that the xDB needs to track website visits.

Note

The standard OutOfProcSessionStateStore that is shipped with ASP.NET does not support the SessionEnd event and therefore cannot be used with shared session state.

You can configure shared session state to use any session-state store provider that extends the abstract class SessionStateStoreProviderBase (shipped with ASP.NET). The only additional requirement is that the session-state store provider can invoke the SessionEnd event via SessionStateItemExpireCallback.

Note

If you are using the xDB Cloud Edition, using the shared session state provider for MongoDB is not included as part of this service.

Follow the steps in this section to use a SQL Server database as your shared session state store using the Sitecore ASP.NET Session State Provider for SQL Server:

Deploy a SQL Server session database

The Sitecore ASP.NET Session State Provider for SQL Server enables you to use SQL Server as your session state store. This provider supports the SessionEnd event that the xDB needs to track website visits.

To deploy the SQL Server session database:

  1. Start Microsoft SQL Server Management Studio 2012 or later.

    Important

    If you are using SQL Server 2008, you first need to download Microsoft SQL Server Management Studio Express before you can deploy the Session database.

  2. Connect to the server node that you want to install the Session database on.
  3. Expand the server node, right-click Databases, and then click Attach.
  4. In the Attach Databases dialog box, click Add.
  5. Browse to the Databases folder in your website root folder, select the Sitecore.Sessions.mdf database and click OK.
  6. In the Attach Databases dialog box, click OK. The session database now appears in your list of attached databases.
  7. Add the following connection string to the ConnectionStrings.config file:
    <add name="sharedsession" connectionString="user id=_sql_server_user_;password=_user_password_;Data 
    Source=_sqlserver_;Database = _sharedSession_database_name_"/>
  8. The add name value can be session or sharedsession depending on whether you are configuring private or shared session state.

Optimize SQL Server performance

For each web request, the session-state store database is accessed multiple times. This can have a significant impact on the performance of your website. Therefore, you should install enough RAM to allow Microsoft SQL Server to keep the session state database in memory. You should also put the database files on an SSD drive.

To achieve optimal performance, you can install an extension to the Sessions database.

To install the performance enhancements:

  1. In Microsoft SQL Server Management Studio 2012, open the Sessions db performance boost.sql file.

    This file is stored in the \Databases\Scripts folder of your Sitecore installation.

  2. In the first line of the Sessions db performance boost.sql file, replace USE [Sitecore_Session] with the name of your session database.
  3. After you have updated the USE statement to point to your session database, press F5 to execute the file.

Note

These performance enhancements move the session-state store to SQL Server tempDB which is the standard practice recommended by Microsoft. However, this is not supported on Windows Azure.

Every user must therefore have access to tempDB. However, every time that SQL Server is restarted, it recreates tempDB and resets the access rights. For information about how to ensure that users always have access to tempDB, see this KB article.

For more information see Session-State Modes on MSDN.

Configure Sitecore

To configure Sitecore to use the shared session state provider for SQL Server:

  1. In your website root folder, navigate to: Website\App_Config\Include
  2. Open the Sitecore.Analytics.Tracking.config file.
  3. Locate the line where you can define the default shared session state provider using the following path: sitecore/tracking/sharedSessionState.
  4. The default shared-session store uses inProc provider (storing data in memory and implemented in the internal ASP.NET class InProcSessionStateStore):
    <sharedSessionState defaultProvider="inProc">
      <providers>
        <clear/>
        <add name="inProc" type="System.Web.SessionState.InProcSessionStateStore" />
      </providers>
  5. To configure SQL Server as your shared session state store provider change the defaultProvider from inProc to mssql. Also, change the name attribute value to mssql.
    <sharedSessionState defaultProvider="mssql">
      <providers>
         <clear/>
         <add 
    name="mssql" 
    type="Sitecore.SessionProvider.Sql.SqlSessionStateProvider,Sitecore.SessionProvider.Sql"
    connectionStringName="sharedsession" 
    pollingInterval="2" 
    compression="true" 
    sessionType="shared"/>
            </providers>

Adjust session state settings

In Sitecore, when you configure a session state, you have the following configuration options:

Setting

Description

connectionStringName

Contains the connection string that Sitecore uses to connect to the session database.

Edit to specify the session state database that you want to use. In the xDB, this database is called session.

For example:

connectionStringName="session"

pollingInterval

Specifies the time interval in seconds that the session-state provider uses to check if any sessions have expired.

For example:

pollingInterval="2"

compression

Indicates that you want session-state data to be compressed.

The default value is true. Compressing session state data reduces the amount of data that you need to transfer between the database and the Sitecore instance. This may cause some additional CPU overhead.

For example:

compression="true" />

sessionType

Indicates whether the type of session state is private or shared.

For example:

sessionType="shared"