Skip to main content
2 of 4
edited title

Thread scope of switchers, disablers and contexts

A while back I surprised to find that objects in Sitecore context are specific to the current thread. So, for example, if I run a parallel loop I can't rely on Sitecore context from inside that loop.

That got me thinking about my understanding of many the Sitecore features I use regularly. I've always been a slightly unclear on the thread scope of many of the 'context' type objects. You know the kind of thing:

  • DatabaseSwitcher
  • LanguageSwitcher
  • BulkUpdateContext
  • SecurityDisabler
  • EventDisabler
  • DatabaseCacheDisabler

Let's take the example of a bulk import. I know the advice is to disable as much of the stuff that might slow things down as possible. But in pratice I'm always concerned about the side effects of doing so. Is it safe to use disablers etc. while other users are in the system or while jobs might be running? Or do they act only on the thread that created them?

If they are thread specific, then what advice is there for using them in a multi-threaded scenario? In a parallel loop, should I create the switchers/disablers inside each 'iteration'?

My head hurts...