<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 ,分布式事务失败。