Thursday, June 5, 2014

CallContext ThreadStatic and SQL transactions

Recently we changed all our DB calls to async/await, as they are much more "thread" efficient.

And most of our old code, the SQL transactions runs on Thread local.  This won't work on async/awaits since now they are all interrupt like and can run on any thread. (we are not running with coordinated transactions for other reasons)

One of our guys implemented the new way initially with a static context, and we have to pass the context name to all calls.  When it is time for me to fix some of the code, it just feels so unnatural, no only there are a lot of code changes, if you miss one they won't be in the transaction.

And during the research, someone found CallContext, it works very much like Thread Statistic, and I think Microsoft copies it to new threads, so with simple fixes everything is working again (of course anything in a transaction is synchronized again without the DTC, that we can live with for now).