Partilhar via


Note to self

A kind reader identified a bug (and a fix) for the DBFunctoids for BizTalk Server 2006. It is a heinous bug because it is a result of not completely reading the documentation. Shameful, really.

The DBFunctoid needs to keep some data around between calls, but it doesn't want to share that data across threads. It also wants to be lazy about it. ThreadStatic fits the bill and is used in the buggy code like this:

public class DBFunk

{

   [ThreadStatic]

   private static Dictionary<string, DBResult> _facts = new Dictionary<string, DBResult>();

   ...

}

The documentaton for ThreadStatic can be found under the ThreadStaticAttribute class. I'll wait here while you go take a look. I'd do a Sudoku puzzle, but I don't do Sudoku puzzles.

You back? Great. So what is the problem? I initialized the ThreadStatic variable. Heinous. The author of the topic went to the trouble to wave a note right there in front of my face and I ignored it. Was I sleeping? Was I thinking about the comics page?

Note Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to a null reference (Nothing in Visual Basic) if it is a reference type.

Heinous. The code should look like this (and does in the new version attached to the original DBFunktoids post):

public class DBFunk

{

[ThreadStatic]

private static Dictionary<string, DBResult> _facts;

private string RunStoredProcedureEx(string connectionString, string storedProcedureName, string parameterNames, params string[] parameters)
{
// Make sure we have an instance
if(_facts == null)
_facts = new Dictionary<string,DBResult>();

...

}

...

}

Note to self: RTFM.