0

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,

Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
Lewis
  • 23
  • 1
  • 4

1 Answers1

0

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 Cooper
  • 8,077
  • 2
  • 33
  • 51
  • 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 the Component Services Administrative tool – Lewis Dec 03 '09 at 14:36
  • 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 Cooper Dec 03 '09 at 15:06
  • 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 Dec 03 '09 at 17:41
  • 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 Dec 03 '09 at 17:56
  • As a note DTC I believe is required for all non MS-SQL providers with SubSonic – runxc1 Bret Ferrier Dec 07 '09 at 23:24