CommittableTransaction Distributed Transaction instance

Source: Internet
Author: User
Tags connectionstrings

String constr = ConfigurationManager. ConnectionStrings ["myCon"]. ConnectionString;
String constr2 = ConfigurationManager. ConnectionStrings ["myCon2"]. ConnectionString;
String SQL = "insert into [Tuser] (username, pwd, age) values ('committabletransaction', 'bbb', 10 )";
String sq2 = "insert into [Tuser] (username, pwd, age) values ('committabletransaction', 'bbb ', 'ssssss ')";

 

# Region CommittableTransaction distributed
Using (SqlConnection con = new SqlConnection (constr), con2 = new SqlConnection (constr2 ))
{
Con. Open ();
Con2.Open ();
CommittableTransaction ct = new CommittableTransaction (); // CommittableTransaction is created after the connection is opened
// The transaction performance is not good.
Con. EnlistTransaction (ct); // register distributed transactions in CommittableTransaction
Con2.EnlistTransaction (ct );
Try
{
Using (SqlCommand cmd = new SqlCommand (SQL, con ))
{

Int a = cmd. ExecuteNonQuery ();
Console. WriteLine ();

}
Using (SqlCommand cmd = new SqlCommand (sq2, con2 ))
{

Int B = cmd. ExecuteNonQuery ();
Console. WriteLine (B );

}
Ct. Commit (); // submit the transaction
}
Catch (Exception ess)
{
Console. WriteLine (ess. Message );
Ct. Rollback (); // roll back the transaction

}
// The connection is used elsewhere. Distributed transactions must be cleared.
Con. EnlistTransaction (null );
Con2.EnlistTransaction (null );
}

# Endregion

 

Although distributed transactions are implemented. However, the performance is really poor. Therefore, distributed transactions are not required. It is best not to use it. Especially when multiple transactions are concurrently executed.

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.