开发者

Subsonic 3 transaction

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,


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();
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜