<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) '從 a 中提取一定的錢 dim cnn as new sqlconnection("server=server1;database=a;uid=sa;[email protected];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) '往 b 帳戶加入一定的錢 dim cnn as new sqlconnection("server=server2;database=b;[email protected];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
然后調用web service, 發現a 的余額是 900, 而 b 的帳戶仍舊是 1000 ,分布式事務失敗。