Use of distributed transactions in ASP. NET

Source: Internet
Author: User

Use of distributed transactions in ASP. NET
I have previously published a transaction storage process. I recently encountered distributed transactions in my project. I would like to summarize all of them and share some experiences with you. First, why do we need distributed transactions? Let's talk about where I encountered distributed transactions in the project. When I was developing the website background, I usually stored the image path in the database when there was an image table design, images are stored in the folder of the website. Therefore, when we operate the product table, when I want to delete the image path of the database product, I also need to delete the images under the website directory, to achieve this function, I used distributed transactions. Idea: 1. System must be referenced in the project. transactions assembly 2. Code method for transaction control: System. transactions. transactionScope scop = new System. transactions. transactionScope () 3. You must start the Distributed Transaction Coordinator service to run Distributed transactions. The following is an example of the code I wrote: 1 // 3. delete the image of the database and folder by id 2 3 // 3.0 get object 4 ProductEntity entity = Product_BLLSub.Get_ProductEntity (int. parse (id); 5 // 3.1 create a transaction 6 using (System. transactions. transactionScope SC Op = new System. transactions. transactionScope () 7 {8 // 3.2 Delete the data of the database image 9 Product_BLLSub.Create_ProductDelete (int. parse (id); 10 12 // 3.3 get the image path 13 string thumphyPath = context. server. mapPath ("/upload/thum/") + entity. img_url; 14 string imgPhyPath = context. server. mapPath ("/upload/img/") + entity. img_url; 15 // 3.4 Delete the thumbnail 16 if (System. IO. file. exists (thumphyPath) 17 {18 System. IO. file. delete (thumphyPath); 1 9} 20 // 3.5 Delete the source image 21 if (System. IO. file. exists (imgPhyPath) 22 {23 System. IO. file. delete (imgPhyPath); 24} 25 // 3.6 submit the transaction 26 scop. complete (); 27} 28 35 // 36 Response deleted successfully. write ("deleted successfully"); Note: The method for operating the database is to extract the database data and convert it into an object, and then operate the database by operating the object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.