Saturday, February 28, 2009

Error: "MSDTC on server 'servername' is unavailable" when using nested transaction

I got this error when I used nested transaction: I opened an ASP.NET transaction

using (TransactionScope scope = new TransactionScope())
{

Inside the block I called a function, that started another transaction the same way.
Inside the second transaction, I got this exception when I tried to open a connection:

using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
cn.Open();

When I googled the error message, the advice that I found was to start the transaction manager on the server, or to enable remote access if the server is on a remote machine. It didn't seem like this was the problem since I was running everything locally, and transactions worked just fine when I wasn't trying to use nested transactions.
I looked for info on nested transaction, and found out there is no problem using them in ASP.NET - the implementation just joins all the actions of the nested transaction to the previous transaction. So I figured it is not the problem.
I tried closing VS and starting it again to no avail, then I restarted my machine, again, without solving the problem. I even tried running the code on another machine, thinking maybe something went wrong with the database installation on my main machine, but exactly the same thing happened on the other computer.

Eventually I did what all the posts that I found advised to do, and started the transaction manager on my machine, using the following procedure:

Start->All Programs->Administrative Tools->Services

Right click on "Distributed Transaction Manager" and "start".
I right clicked this service, clicked "properties", and set the startup type to "Automatic", to make sure it starts every time Windows starts.

Now everything works fine.
I still don't know why only nested transactions need this service, or found anything about it in the documentation, but at least it works, and I hope this post will save other programmers all the time I wasted on this problem.

No comments:

Post a Comment