ASP.NET中分散式交易的使用

來源:互聯網
上載者:User

標籤:asp.net 分散式交易

最近在做項目的時候遇到分散式交易,所有總結一下,跟大家分享和交流一下經驗。首先說明為什麼要分散式交易呢?先說說我在項目的哪裡遇到分散式交易吧,我是在做網站後台開發的時候,一般涉及到有圖片表的設計時,資料庫存放的是圖片的路徑,圖片是存放在網站的檔案夾下面,所以我們操作產品表時,當我要刪除資料庫產品圖片路徑,同時要把存在網站目錄下的圖片也刪掉,為了實現這功能,我就使用了分散式交易。

 

思路:

 1、在項目中必須引用 System.Transactions 程式集

 2、在需要進行事務管控的代碼方法:System.Transactions.TransactionScope scop = new System.Transactions.TransactionScope()

 3、必須啟動服務 Distributed Transaction Coordinator才能進行分散式交易的正常運行

下面是我寫的一個例子主要代碼:

 

//根據id將資料庫和檔案夾的圖片一起刪掉            //根據id得到實體物件            ProductEntity entity = Product_BLLSub.Get_ProductEntity(int.Parse(id));           //建立一個事務            using (System.Transactions.TransactionScope scop = new System.Transactions.TransactionScope())            {                 //刪除資料庫圖片的資料                 Product_BLLSub.Create_ProductDelete(int.Parse(id));                //得到圖片的路徑                    string thumphyPath = context.Server.MapPath("/upload/thum/") + entity.img_url;                    string imgPhyPath = context.Server.MapPath("/upload/img/") + entity.img_url;                    //刪除縮圖                    if (System.IO.File.Exists(thumphyPath))                    {                        System.IO.File.Delete(thumphyPath);                    }                    //刪除原圖                    if (System.IO.File.Exists(imgPhyPath))                    {                        System.IO.File.Delete(imgPhyPath);                    }                    //提交事務                    scop.Complete();                }              //刪除成功         Response.Write("刪除成功");

 

說明:我操作資料庫的方法是將資料庫資料取出來轉換成實體物件,然後通過操作實體物件來操作資料庫。

本文出自 “靈雨飄零部落格” 部落格,請務必保留此出處http://101779.blog.51cto.com/91779/1547382

ASP.NET中分散式交易的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.