Go Back

Transactions using Microsoft Distributed Transaction Coordinator in .Net

   I was working with data access this morning at work, and was dealing with rewriting some very ugly try/catch blocks to make use of System.Transactions.TransactionScope. I had read about it on the MSDN forums at some point and finally got a chance to get down to it in my Service.

 

   One thing I wasn’t sure of was whether or not I should be doing my transaction scope within my business logic or the data access layer.  I’m still a bit uncertain of exactly where those lines of demarcation should really be. I generally like my assemblies to have a simple interface or a façade pattern and factories to get instances out of it. Since the Enterprise library data access layer had the Repository and identify factories making one simple entry point into that assembly and I felt I didn’t want to confuse that implementation, I went with putting it in my business logic. Plus that’s where my try/catch blocks were at anyhow…

 

Basically I had something that looked like this:

try

{

            InsertPerson(person);

foreach(WorkHistory history in historyCollection)

{

            Insert(history);

}

Foreach(Skill skill in skillCollection)

{

            Insert(skill);

}

            (…..)

}

Catch(Exception ex)

{

           /*roll back everything*/

            Throw ex;//re throw.

}

 

I rewrote that to be like this:

Using(TransactionScope scope = new TransactionScope())

{

            InsertPerson(person);

foreach(WorkHistory history in historyCollection)

{

            Insert(history);

}

foreach(Skill skill in skillCollection)

{

            Insert(skill);

}

            (…..)

            scope.Complete();

}

 

Really simple code! Unfortunately, it didn’t work and the error message I got was pretty descriptive, but at the same time not completely useful. I should have wrote it down but it was something like “MSDTC Network transactions are disabled, enable Use Network Transactions in MSDTC configuration”.

(Mental note to put the real error message in this text so it's web searchable)

 

Ok, simple enough… my first question was. “Where is MSDTC configuration?”

I don’t remember where I found it, but it’s “dcomcnfg” (which slightly annoyed me because theres bootcfg, ipconfig, pagefileconfig, msconfig… where is the naming scheme MS???)

Basically once you’re there you expand component services and the computers folder then right click and hit “properties” on “My Computer”

image001.jpg

image002.jpg

I saw the use local coordinator box, and then it asked me to choose a server. Well I didn’t want to remotely coordinate my transactions I just wanted to remotely RUN transactions on my remote sql server. So I checked use local coordinator back again. Did some googling, no luck. Opened security configuration:

 

image003.jpg

Now I finally got the stuff I wanted…

So I checked network dtc access

Checked allow remote clients

Checked allow inbound and allow outbound

Left mutual authentication required

And I enabled TIP and XA transactions for the heck of it. Turn it all on, I say!

I left the dtc logon account as it was, figured windows services are always suppose to be on local service and this is operating over the network so sure, network service sounded good.

Running my app worked just fine after that.

 

A good way to tell it was running was opening up the transaction statistics in dcomcnfg and watching the transactions come in and pass, then I modified the code to throw an exception in the using() block and it propertly aborted the transaction and I verified my data had been rolled back (and it joyously was).

image004.jpg

 

 

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Post a comment!
  1. Formatting options