Multi-step transaction rollback

Source: Internet
Author: User

msdn link: http://msdn.microsoft.com/zh-cn/library/w97s6fw4 (V = vs.80). aspx

Class program {static void main (string [] ARGs) {try {using (transactionscope = new transactionscope () {customenlistment cstomenlistment = new customenlistment (); cstomenlistment. preparing + = new customenlistment. preparingeventhandler () => {testclass. num1 = 1; testclass. num2 = 2; testclass. num3 = 3; throw new exception ("Throw test error! ") ;}); Cstomenlistment. rollbacking + = new customenlistment. rollbackingeventhandler () => {testclass. num1 = 0; testclass. num2 = 0; testclass. num3 = 0 ;}); transaction. current. enlistvolatile (cstomenlistment, enlistmentoptions. none); guid = new GUID ("{3456789a-7654-2345-abcd-098765434567}"); transaction. current. enlistdurable (guid, cstomenlistment, enlistmentoptions. none); transactionscope. compl Ete () ;}} catch {console. writeline ("Show ERROR infomation! ");} Thread. sleep (1000); // The transaction starts asynchronous thread execution independently to stop the main thread and is used to test the effect console. writeline (testclass. num1.tostring (); console. writeline (testclass. num2.tostring (); console. writeline (testclass. num3.tostring (); console. read () ;}}// two-phase commit (2 PC) public class customenlistment: ienlistmentnotification {Public Delegate void preparingeventhandler (); public event preparingeventhandler preparing; Public Delegate void rollbacki Ngeventhandler (); public event rollbackingeventhandler rollbacking; // check whether each group of transactions executes public void prepare (preparingenlistment) {try {preparing (); preparingenlistment. prepared (); // call commit} catch {preparingenlistment. forcerollback (); // call rollback} // submit public void commit (enlistment) {enlistment. done ();} // roll back public void rollback (enlistment) {rollbackin G (); enlistment. Done () ;}// the description of the method comes from msdn // The indoubt method should be implemented for the volatile resource manager. // Call this method when the transaction manager loses contact with one or more participants (so its status is unknown. // If this happens, you should record this fact whether or not any transaction participants are in an inconsistent state for future investigations. Public void indoubt (enlistment) {enlistment. done () ;}//< summary> /// test class /// </Summary> public class testclass {Private Static int num1 = 0; public static int num1 {get {return testclass. num1;} set {testclass. num1 = value ;}} Private Static int num2 = 0; public static int num2 {get {return testclass. num2;} set {testclass. num2 = value ;}} Private Static int num3 = 0; public static int num3 {get {return testclass. num3;} set {testclass. num3 = value ;}}}

if you have any doubts or errors, you are welcome to correct and discuss them.

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.