tags:

views:

163

answers:

1

Hi, I have DTC configured as outlined on MS website to support for remote transaction. I have the following code always giving me error.

using (TransactionScope ts = new TransactionScope())
{
   Category c = new Category();
   c.Name = "Cat1";
   c.Save();

   Product p = Product.SingleOrDefault(x=>x.ProductID==1);
   p.Title = "new title";
   p.Save();

   ts.Close();
 }

However if I move the second block of code out of the suing block it works just fine. What I want to do is bind those two block of code into one trascation. What could be the readon? Thanks,

A: 

You don't specify what error the code is giving you but the only thing wrong I can see is that you're not calling Complete on your TransactionScope. Try the following:

using (TransactionScope ts = new TransactionScope())
{
   Category c = new Category();
   c.Name = "Cat1";
   c.Save();

   Product p = Product.SingleOrDefault(x=>x.ProductID==1);
   p.Title = "new title";
   p.Save();

   ts.Complete();
 }

You shouldn't actually need DTC enabled, you can wrap this code in a transaction using SubSonic's SharedDbConnectionScope. Try the following:

using (TransactionScope ts = new TransactionScope())
using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
{
   Category c = new Category();
   c.Name = "Cat1";
   c.Save();

   Product p = Product.SingleOrDefault(x=>x.ProductID==1);
   p.Title = "new title";
   p.Save();

   ts.Complete();
 }
Adam
Thanks for your answer. Here is the error I got and I actually have that line of code and still got the error.System.Transactions.TransactionManagerCommunicationException: Network access for Distributed Transaction Manager(MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using theComponent Services Administrative tool
Lewis
OK that error message means DTC isn't configured correctly. The following answer may help you http://stackoverflow.com/questions/7694/how-do-i-enable-mstdc-on-sqlserver however as I outlined above you should be able to avoid using DTC altogether.
Adam
I actually configure the DTC as described on several articles. However I still got that error. Since I can submit the transaction without the second code block, which means the DTC is working. I just can't figure out what cause the conflict if I added the second code block. I had tried your code yesterday and it gave me invalid object error message. The error looks to me is that the connection scope is using a wrong data set.
Lewis
Ok. I changed the sharedConnectionScope to use a specific connection string and it works now. It is really wired. Also I don't know if you can edit Subsonic web site. The tutorial for transaction on the web site is wrong. Maybe somebody can change it.
Lewis
Thanks Adam for your help.
Lewis
As a note DTC I believe is required for all non MS-SQL providers with SubSonic
runxc1 Bret Ferrier