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."