Ado.net Series Quiz 2 about distributed transactions

Source: Internet
Author: User
ado| Distributed issues:
For examples of distributed transactions:

Database A and database B each have a bank table (in fact, A and b on different machines, different types of database (Support DTC))
Table structure is as follows
BankAccount, Amount

A record in the database:
A 1000

B data in the database
B 1000

For easy testing, COM + is not used, but is tested directly in the Web service.
The code is as follows:

<webmethod (Transactionoption:=enterpriseservices.transactionoption.requiresnew) > _
Public Function dtstest () as String
Try
Contextutil.enablecommit ()

Drawmoneyfroma (100)
Depostmoneytob (100)
ContextUtil.SetComplete ()
Return "ok!"
Catch ex as Exception
ContextUtil.SetAbort ()
Return "Failed" + ex. Message
End Try


End Function


Public Sub Drawmoneyfroma (ByVal amount as Long)
' Extract a certain amount of money from A
Dim CNN as New SqlConnection ("Server=server1;database=a;uid=sa;pwd=password@1;enlist=false")
Cnn. Open ()
Dim cmd as New SqlCommand ("Update Bank set Amount=amount-" & Amount & "where bankaccount= ' A '", CNN)
Cmd. ExecuteNonQuery ()

End Sub

Public Sub Depostmoneytob (ByVal amount as Long)
' Add a certain amount of money to the B account
Dim CNN as New SqlConnection ("Server=server2;database=b;pwd=password@1;uid=sa;enlist=false")
Cnn. Open ()
Dim cmd as New SqlCommand ("Update Bank set Amount=amount +" & Amount & "where bankaccount= ' B", CNN)
Throw New Exception ("FF")
Cmd. ExecuteNonQuery ()
End Sub


The Web service was then invoked, and the balance of a was found to be 900, while B's account was still 1000, and the distributed transaction failed.

What are the possible causes?

The same code as above can only be modified slightly.



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.