Call WebService in a transaction to implement data synchronization to a certain extent

Source: Internet
Author: User

Error Method:
Using (Transactionscope scope Scope = New Transactionscope ())
{
Try
{
WS. webservice1 Webs = New Distributed Transaction _ demo. ws. webservice1 ();
Webs. helloworld (); // The execution is successful. Data has been inserted to the remote database. Rollback upon failure

Exesql (); // The execution fails. rollback does not roll back the data in the remote database. If yes, the transaction is committed.

Scope. Complete ();
}
Catch (Exception ex)
{
// Thronew exception ("Sending Message exception, cause:" + ex. Message );
}
Finally
{
// Release resources
Scope. Dispose ();
}
}

Correct method:
Using(Transactionscope scope Scope= NewTransactionscope ())
{
Try
{

Exesql (); // If the execution fails, roll back directly. If the execution succeeds, continue.

WS. webservice1 Webs = New Distributed Transaction _ demo. ws. webservice1 ();
Webs. helloworld (); // If the execution is successful, the remote database has inserted data and committed the transaction.

// If the transaction fails, the transaction is also rolled back.

Scope. Complete ();
}
Catch (Exception ex)
{
// Throw new exception ("sending information exception, cause:" + ex. Message );
}
Finally
{
// Release resources
Scope. Dispose ();
}
}

The above method achieves data synchronization to a certain extent. But it is not really a distributed transaction. Because in scope. Complete (); disconnection or any other fault has ended. At this time, the remote database already has data, but it does not exist locally. Of course, this situation is extreme.

I also quoted Daniel "sp1234" as an example: "For general applications, you do not have to consider the concept of distributed transactions that is too technical. You can access the remote web service in the local database transaction. If an error occurs, it will be rolled back (that is, the method implemented above !).
If the performance of this practice is really poor (the so-called theoretical performance of distributed transactions is also very poor), you can change to the business idea, a background thread for data synchronization at, when there is no time in the foreground, this is enough."

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.