{"id":426,"date":"2007-02-18T09:56:32","date_gmt":"2007-02-18T01:56:32","guid":{"rendered":"http:\/\/blog.axqd.net\/?p=426"},"modified":"2007-02-18T09:56:32","modified_gmt":"2007-02-18T01:56:32","slug":"systemtransactions%e5%ae%9e%e7%8e%b0%e4%bd%a0%e8%87%aa%e5%b7%b1%e7%9a%84resource-manager","status":"publish","type":"post","link":"https:\/\/blog.axqd.net\/?p=426","title":{"rendered":"System.Transactions:\u5b9e\u73b0\u4f60\u81ea\u5df1\u7684Resource Manager"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>System.Transactions:\u5b9e\u73b0\u4f60\u81ea\u5df1\u7684Resource Manager<\/strong><\/p>\n<p style=\"text-align: right;\">By Sahil Malik<\/p>\n<p>.net 2.0\u6240\u5e26\u6765\u6700\u5927\u7684\u53d8\u5316\u4e4b\u4e00\u4e5f\u8bb8\u5c31\u662fSystem.Transactions\u547d\u540d\u7a7a\u95f4\u7684\u5f15\u5165\u3002\u5728\u6211\u4ee5\u524d\u5173\u4e8eSQLCLR\u7684\u6587\u7ae0\u4e2d\uff0c\u4e5f\u7b80\u7565\u7684\u63d0\u5230\u8fc7\u8fd9\u4e2a\u547d\u540d\u7a7a\u95f4\u3002\u4f46\u662f\u8c01\u8bf4\u4e8b\u52a1\u7684\u6982\u5ff5\u53ea\u80fd\u5c40\u9650\u4e8e\u6570\u636e\u5e93\uff1f\u96be\u9053\u6211\u4eec\u5728\u6570\u636e\u5e93\u4ee5\u5916\u7684\u9886\u57df\u5c31\u4e0d\u9700\u8981\u53ef\u9760\u7684\u4ee3\u7801\u4e86\u4e48?\u7b54\u6848\u662f\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981! \u8fd9\u6b63\u662f\u4f60\u4e0d\u80fd\u5ffd\u89c6System.Transactions\u547d\u540d\u7a7a\u95f4\u7684\u539f\u56e0\uff0c\u4e5f\u662f\u5979\u5c06\u5728\u4e0b\u4e00\u4ee3\u5fae\u8f6f\u5e73\u53f0\u5f15\u8d77\u6df1\u523b\u53d8\u9769\u7684\u539f\u56e0\u3002\u8ba9\u6211\u4eec\u9996\u5148\u6765\u770b\u770b System.Transactions\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n<p><strong>System.Transactions\u5982\u4f55\u5de5\u4f5c?<\/strong><\/p>\n<p>System.Transactions\u4e3a\u60a8\u63d0\u4f9b\u6700\u7b80\u6d01\u660e\u4e86\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u4e8b\u52a1\u65b9\u5f0f\u7684\u5404\u79cd\u64cd\u4f5c\u3002\u4f60\u5f53\u7136\u53ef\u4ee5\u7528\u5404\u79cd\u5404\u6837\u7684\u65b9\u5f0f\u4f7f\u7528\u5979\uff0c\u4f46\u4e5f\u8bb8\u6700\u5178\u578b\u7684\u4e00\u4e2a\u5b9e\u73b0\u5c31\u662f\u5982\u4e0b\uff0c\u5c06\u4e8b\u52a1\u64cd\u4f5c\u5305\u88f9\u5728TransactionScope(\u7528\u6765\u6784\u5efa\u4ee3\u7801\u6bb5\u4e8b\u52a1\u7684\u4e00\u4e2a\u5b9e\u4f8b\u53d8\u91cf)\u91cc\u9762\uff1a<\/p>\n<p><code>using (TransactionScope ts = new TransactionScope())<br \/>\n{<br \/>\n\/\/ \u4e8b\u52a1\u4ee3\u7801\u653e\u8fd9\u91cc<br \/>\nts.Complete();<br \/>\n}<\/code><\/p>\n<p>\u5728\u4e0a\u9762\u6ce8\u91ca\u7684\u90e8\u5206\u91cc\u9762\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6301\u6709\u6216\u5b9e\u73b0\u4e86resource manager(RM)\u7684\u7c7b\u3002<\/p>\n<p>\u4e8b\u52a1\u5de5\u4f5c\u4e8e\u5404\u79cd\u88abRM\u7ba1\u7406\u7740\u7684\u8d44\u6e90\u4e0a\u3002RM\u540c\u5176\u4ed6\u5b9e\u4f53(\u5178\u578b\u7684\u5305\u62ec\uff1a\u5176\u4ed6\u8fdb\u7a0b\u6216\u8005\u88ab\u79f0\u4e3atransaction managers(TMs)\u7684\u670d\u52a1)\u534f\u540c\u5de5\u4f5c\u3002<\/p>\n<p>RM\u4e0eTM\u5728\u4e8b\u52a1\u4e2d\u534f\u540c\u5de5\u4f5c\u7684\u65b9\u5f0f\u5e38\u88ab\u79f0\u4e3a&#8221;\u4e24\u6bb5\u63d0\u4ea4\u8fc7\u7a0b&#8221;\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u5178\u578b\u7684\u201c\u4e24\u6bb5\u63d0\u4ea4\u8fc7\u7a0b\u201d\u5de5\u4f5c\u6d41\uff1a<\/p>\n<p>1.RM\u52a0\u5165\u4e8b\u52a1\uff1b<br \/>\n2.RM\u505a\u5b8c\u51c6\u5907\u5de5\u4f5c\u540e\uff0c\u5411TM\u53d1\u9001\u7b2c\u4e00\u9636\u6bb5\u5b8c\u6210\u4fe1\u53f7\u3002\u8fd9\u4e2a\u9636\u6bb5\u4e5f\u88ab\u79f0\u4e3a&#8221;\u51c6\u5907\u9636\u6bb5&#8221;\uff1b<br \/>\n3.TM\u5728\u6240\u6709RMs\u6210\u529f\u6267\u884c\u5b8c\u51c6\u5907\u5de5\u4f5c\u540e\uff0c\u5411\u6240\u6709RMs\u53d1\u9001\u7eff\u706f\u4fe1\u53f7\uff1b<br \/>\n4.RMs\u83b7\u5f97\u7eff\u706f\u4fe1\u53f7\u540e\u5b9e\u9645\u5f00\u59cb\u63d0\u4ea4\u5de5\u4f5c(\u5982\u679c\u6536\u5230\u7ea2\u706f\u4fe1\u53f7\uff0c\u5219\u56de\u6eda\u4ed6\u4eec\u81ea\u5df1\u7684\u5de5\u4f5c)\u3002\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u9636\u6bb5&#8211;&#8220;\u63d0\u4ea4\u9636\u6bb5&#8221;\uff1b<br \/>\n5.\u5426\u5219\uff0c\u4e8b\u52a1\u534f\u8c03\u5668\u540c\u6240\u6709RMs\u8fdb\u884c\u534f\u8c03\u4ee5\u4fdd\u8bc1\u4ed6\u4eec\u8981\u4e48\u6210\u529f\uff0c\u8981\u4e48\u4e00\u8d77\u56de\u6eda\uff1b<\/p>\n<p>\u4e0a\u9762\u63d0\u5230\u7684\u6574\u4e2a\u8fc7\u7a0b\u90fd\u7531System.Transactions\u6846\u67b6\u63d0\u4f9b\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528MSDTC(Microsoft\u5206\u5e03\u5f0f\u4e8b\u52a1\u534f\u8c03\u5668)\u4f5c\u4e3aTMs\u3002\u4e3a\u4e86\u4f7f\u7528System.Transactions\u6765\u7ba1\u7406\u53c2\u4e0e\u4e8b\u52a1\u64cd\u4f5c\u7684\u8d44\u6e90\uff0c\u4f60\u5fc5\u987b\u8981\u4e48\u4f7f\u7528\u73b0\u6709\u7684RM\u6216\u8005\u5b9e\u73b0\u4f60\u81ea\u5df1\u7684RM\u3002<\/p>\n<p>\u5c31\u76ee\u524d\u800c\u8bba\uff0c System.Transactions\u5df2\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u5b9e\u9645\u80fd\u5e94\u7528\u7684RMs\u5374\u5f88\u5c11\uff0c\u6240\u4ee5\u5f88\u591a\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u53d1\u73b0\u5b9e\u73b0\u81ea\u5df1\u7684RM\u975e\u5e38\u6709\u7528\u3002\u5728\u8fd9\u4e4b\u540e\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e9bRMs\u540c\u672a\u6765.net\u6846\u67b6\u4e2d\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684TM\u534f\u540c\u5de5\u4f5c\u3002\u53e6\u5916\uff0c\u540c.net 2.0\u4e00\u540c\u53d1\u5e03\u7684\u4e00\u4e2a\u5f88\u5e38\u7528\u7684RM\u5c31\u662fSqlConnection\u7c7b\u3002\u672c\u6587\u4e2d\u4f1a\u4ee5\u6b64\u7c7b\u548c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684RM\u4e3a\u4f8b\uff0c\u63cf\u8ff0\u5b83\u4eec\u7a76\u7adf\u5982\u4f55\u540cTM\u4e00\u8d77\u5de5\u4f5c\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u786e\u5b9e\u4e0d\u5f97\u4e0d\u5b9e\u73b0\u81ea\u5df1\u7684RM\uff0c\u6700\u91cd\u8981\u7684\u95ee\u9898\u6050\u6015\u5c31\u662f&#8211;&#8220;RM\u7a76\u7adf\u8981\u505a\u4e9b\u4ec0\u4e48?&#8221;<\/p>\n<p><strong>RM\u7a76\u7adf\u8981\u505a\u4e9b\u4ec0\u4e48?<\/strong><\/p>\n<p>\u5f71\u54cd\u5b9e\u73b0\u4f60\u81ea\u5df1\u7684RM\u7684\u4e00\u4e2a\u5173\u952e\u95ee\u9898\u5c31\u662f\uff1a\u8fd9\u4e2aRM\u7ba1\u7406\u7684\u8d44\u6e90\u672c\u8eab\u7684\u7279\u6027\u662f\u6301\u4e45\u7684\u8fd8\u662f\u6613\u53d8\u7684?<\/p>\n<p>\u5728\u5178\u578b\u7684&#8221;\u4e24\u6bb5\u63d0\u4ea4\u8fc7\u7a0b&#8221;\u4e2d\uff0c\u6301\u4e45\u7684\u8d44\u6e90\u9700\u8981&#8221;\u5931\u8d25\u6062\u590d&#8221;\u3002\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u5c31\u662f\u4e8b\u52a1\u6587\u4ef6\u62f7\u8d1d\u3002\u5982\u679c\u4f60\u4e0d\u5f97\u4e0d\u5b9e\u73b0\u4e00\u4e2aRM\u6765\u5c01\u88c5\u62f7\u8d1d\u6587\u4ef6\u7684\u64cd\u4f5c\u5230\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u6587\u4ef6\u53ef\u4ee5\u5728\u7b2c\u4e00\u4e2a\u9636\u6bb5\u88ab\u5b9e\u9645\u62f7\u8d1d\u3002\u5982\u679cRM\u5931\u8d25\uff0c\u6062\u590d\u7b56\u7565\u5c06\u4fdd\u8bc1\u5728\u56de\u6eda\u8fc7\u7a0b\u4e2d\uff0cTM\u6709\u8db3\u591f\u7684\u4fe1\u606f\u6765\u6062\u590d\u539f\u6765\u7684\u72b6\u6001\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u80fd\u5c31\u662f\u6307\u5220\u9664\u6587\u4ef6\u6216\u8005\u7528\u4ee5\u524d\u5b58\u5728\u7684\u6587\u4ef6\u6765\u4ee3\u66ff\u3002\u8fd9\u79cd\u8d44\u6e90\u7684\u6536\u96c6\u64cd\u4f5c\uff0c\u4f7f\u7528System.Transactions.Transaction.EnlistDurable\u65b9\u6cd5\u3002<\/p>\n<p>\u53cd\u4e4b\uff0c\u6613\u53d8\u8d44\u6e90\u662f\u4e0d\u9700\u8981&#8221;\u5931\u8d25\u6062\u590d&#8221;\u7684\u3002\u540c\u6837\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u5c31\u662f\u6301\u6709\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002\u5c01\u88c5\u8fd9\u7c7b\u64cd\u4f5c\u5230\u4e8b\u52a1\u4e2d\u7684RM\u4f7f\u7528System.Transactions.Transaction.EnlistVolatile\u65b9\u6cd5\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u79cd\u6536\u96c6\u8d44\u6e90\u7684\u65b9\u5f0f\u662fPSPE(\u53ef\u63d0\u5347\u7684\u5355\u6bb5\u5f0f\u6536\u96c6)\u3002\u8fd9\u79cd\u65b9\u5f0f\u7531\u6301\u4e45\u7684RM\u62e5\u6709\u4e8b\u52a1\uff0c\u5f53\u6761\u4ef6\u6539\u53d8\u65f6\uff0c\u53ef\u4ee5\u9010\u6b65\u5347\u7ea7\u4e3a\u7531TM\u7ba1\u7406\u3002\u56e0\u4e3a\u6301\u4e45\u7684\u8d44\u6e90\u6536\u96c6\u6bd4\u6613\u53d8\u7684\u8d44\u6e90\u6536\u96c6\u4ee3\u4ef7\u66f4\u5927\uff0cPSPE\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u6709\u5438\u5f15\u529b\u7684\u6301\u4e45\u8d44\u6e90\u6536\u96c6\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u5728\u8bb8\u591a\u5730\u65b9\u662f\u4ee5\u727a\u7272\u6027\u80fd\u4e3a\u4ee3\u4ef7\u7684\u3002<\/p>\n<p>\u5728\u9009\u597d\u8d44\u6e90\u6536\u96c6\u7684\u65b9\u5f0f\u540e\uff0c\u63a5\u4e0b\u6765\u4f60\u9700\u8981\u5b9e\u73b0IEnlistmentNotification\u63a5\u53e3\uff0c\u4ee5\u4f7f\u4f60\u4eceTM\u83b7\u5f97\u5fc5\u8981\u7684\u56de\u8c03\u6765\u8fdb\u884c\u6070\u5f53\u7684\u63d0\u4ea4\u6216\u56de\u6eda\u3002 Windows\u5e73\u53f0\u7684TMs\u7684\u4f8b\u5b50\u662f\u9002\u5408\u6613\u53d8\u8d44\u6e90\u6536\u96c6\u7684LTM(\u8f7b\u91cf\u7ea7\u7684\u4e8b\u52a1\u7ba1\u7406\u5668)\u4ee5\u53ca\u9002\u5408\u5206\u5e03\u5f0f\u6216\u8005\u6301\u4e45\u8d44\u6e90\u6536\u96c6\u7684MSDTC\u3002<\/p>\n<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u5411\u4f60\u793a\u8303\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6982\u5ff5\u3002<\/p>\n<p>\u4f7f\u7528\u6613\u53d8\u8d44\u6e90\u6536\u96c6\u65b9\u5f0f\u5b9e\u73b0RM<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u793a\u8303\u4e86\u5728\u4e8b\u52a1\u4e2d\u5199\u6210\u5458\u53d8\u91cf\u7684RM\uff1a<\/p>\n<p><code>public class VolatileRM : IEnlistmentNotification<br \/>\n{<br \/>\nprivate int memberValue = 0;<br \/>\nprivate int oldMemberValue = 0;<br \/>\npublic int MemberValue<br \/>\n{<br \/>\nget { return memberValue; }<br \/>\n}<br \/>\npublic void SetMemberValue(int newMemberValue)<br \/>\n{<br \/>\nTransaction currentTx = Transaction.Current;<br \/>\nif (currentTx != null)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: SetMemberValue - EnlistVolatile\");<br \/>\ncurrentTx.EnlistVolatile(this, EnlistmentOptions.None);<br \/>\n}<br \/>\noldMemberValue = memberValue;<br \/>\nmemberValue = newMemberValue;<br \/>\n}<br \/>\nIEnlistmentNotification Members#region IEnlistmentNotification Members<br \/>\npublic void Commit(Enlistment enlistment)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: Commit\");<br \/>\n\/\/ Clear out oldMemberValue<br \/>\noldMemberValue = 0;<br \/>\n}<br \/>\npublic void InDoubt(Enlistment enlistment)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: InDoubt\");<br \/>\n}<br \/>\npublic void Prepare(PreparingEnlistment preparingEnlistment)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: Prepare\");<br \/>\npreparingEnlistment.Prepared();<br \/>\n}<br \/>\npublic void Rollback(Enlistment enlistment)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: Rollback\");<br \/>\n\/\/ Restore previous state<br \/>\nmemberValue = oldMemberValue;<br \/>\noldMemberValue = 0;<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684\u90a3\u6837\uff0c\u6211\u4eec\u5728SetMemberValue\u4e2d\u9996\u5148\u68c0\u67e5\u4e86\u73b0\u5728\u662f\u5426\u5728\u4e8b\u52a1\u4e2d\u3002\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5b83\u5982\u4e0b\u6240\u793a\uff0c\u5c06\u81ea\u5df1\u6536\u96c6\u8fdb\u6613\u53d8\u4e8b\u52a1\u4e2d\uff0c\u5e76\u4e14\u4e3a\u53ef\u80fd\u7684\u56de\u6eda\u505a\u4e00\u4e9b\u5fc5\u8981\u7684\u64cd\u4f5c\uff1a<\/p>\n<p><code>public void SetMemberValue(int newMemberValue)<br \/>\n{<br \/>\nTransaction currentTx = Transaction.Current;<br \/>\nif (currentTx != null)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: SetMemberValue - EnlistVolatile\");<br \/>\ncurrentTx.EnlistVolatile(this, EnlistmentOptions.None);<br \/>\n}<br \/>\noldMemberValue = memberValue;<br \/>\nmemberValue = newMemberValue;<br \/>\n}<\/code><\/p>\n<p>\u793a\u4f8b\u4e2d\u4e5f\u5b9e\u73b0\u4e86\u4e00\u7cfb\u5217\u5176\u4ed6\u7684\u65b9\u6cd5\u3002\u63d0\u4ea4(Commit)\u3001\u56de\u6eda(RollBack)\uff0c\u987e\u540d\u601d\u4e49\uff0c\u8981\u4e48\u63d0\u4ea4\u5de5\u4f5c\uff0c\u8981\u4e48\u56de\u6eda\u5de5\u4f5c\u3002TM\u8c03\u7528Prepare\u65b9\u6cd5\u6765\u901a\u77e5RM\u5b83\u6b63\u8bf7\u6c42\u7b2c\u4e00\u9636\u6bb5\u7684\u4e8b\u52a1\u63d0\u4ea4\u5de5\u4f5c\uff0c\u5e76\u4e14\u6d4b\u8bd5\u4f60\u7684RM\u662f\u5426\u4e3a\u5de5\u4f5c\u505a\u597d\u4e86\u51c6\u5907\u3002\u8fd9\u91cc\uff0c\u4f60\u6709\u4e0b\u9762\u4e09\u79cd\u9009\u62e9\uff1a<\/p>\n<p>1.\u8c03\u7528preparingEnlistment.Prepare()\u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u901a\u77e5TM\u4f60\u5df2\u505a\u597d\u51c6\u5907\uff0c\u5e76\u4e14\u6b63\u7b49\u5f85\u4e0b\u4e00\u6b65\u63d0\u4ea4\u7684\u901a\u77e5\u3002<\/p>\n<p>2.\u8c03\u7528preparingEnlistment.ForceRollBack()\u65b9\u6cd5\u6216\u8005\u6307\u5b9apreparingEnlistment.RecoveryInformation\u3002<\/p>\n<p>\u5728\u91cd\u65b0\u6536\u96c6\u8d44\u6e90\u7684\u65f6\u5019\uff0c\u8fd9\u5c06\u63d0\u4f9b\u65b0\u7684RM\u5b9e\u4f8b\u548c\u5b8c\u6210\u6062\u590d\u5de5\u4f5c\u6240\u9700\u8981\u7684\u5145\u8db3\u7684\u4fe1\u606f\u3002\u4f46\u662f\u73b0\u5728\u4f60\u8fd8\u4e0d\u5fc5\u4e3a\u6b64\u62c5\u5fc3\u3002<\/p>\n<p>3.\u8c03\u7528preparingEnlistment.Done()\u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u5c06RM\u4f5c\u4e3a\u65c1\u89c2\u8005\u3002\u53ea\u89c2\u5bdf\u4e8b\u52a1\uff0c\u4f46\u5e76\u4e0d\u53c2\u4e0e\u5176\u4e2d\u3002\u901a\u8fc7\u8c03\u7528Done\u65b9\u6cd5\uff0cTM\u4e0d\u4f1a\u7ed9\u4f60\u7b2c\u4e8c\u9636\u6bb5\u7684\u901a\u77e5\u3002<\/p>\n<p>\u65e2\u7136\u4f60\u5df2\u7ecf\u5efa\u597d\u4e86RM\uff0c\u4f60\u73b0\u5728\u80fd\u591f\u4f9d\u8d56\u6846\u67b6\u6765\u7f16\u5199\u7b80\u5355\u53ef\u9760\u7684\u4ee3\u7801\u3002\u4f7f\u7528\u4e0a\u9762\u7684RM\u662f\u76f8\u5f53\u8f7b\u677e\u60ec\u610f\u7684\u4e00\u4ef6\u4e8b\u60c5\u3002\u4f8b\u5982\uff0c\u952e\u5165\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<p><code>VolatileRM vrm = null ;<br \/>\nusing (TransactionScope txSc = new TransactionScope())<br \/>\n{<br \/>\nvrm = new VolatileRM();<br \/>\nvrm.SetMemberValue(3);<br \/>\ntxSc.Complete();<br \/>\n}<br \/>\nConsole.WriteLine(\"Member Value:\" + vrm.MemberValue);<\/code><\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u4ea7\u751f\u4e0b\u9762\u7684\u8f93\u51fa\uff1a<\/p>\n<p><code>VolatileRM: SetMemberValue - EnlistVolatile<br \/>\nVolatileRM: Prepare<br \/>\nVolatileRM: Commit<br \/>\nMember Value:3<\/code><\/p>\n<p>\u5982\u4f60\u6240\u89c1\uff0c\u6210\u5458\u503c\u88ab\u66f4\u65b0\u4e86\uff0c\u5e76\u4e14RM\u5728\u51c6\u5907\u548c\u63d0\u4ea4\u9636\u6bb5\u5f97\u5230\u4e86\u76f8\u5e94\u7684\u901a\u77e5\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u901a\u8fc7RM\u5b9e\u73b0\u5f3a\u5236\u56de\u6eda\uff0c\u4f60\u53ef\u4ee5\u5982\u4e0b\u7b80\u5355\u7684\u4fee\u6539RM\u7684Prepare\u65b9\u6cd5\uff1a<\/p>\n<p><code>public void Prepare(PreparingEnlistment preparingEnlistment)<br \/>\n{<br \/>\nConsole.WriteLine(\"VolatileRM: Prepare\");<br \/>\npreparingEnlistment.ForceRollback();<br \/>\n}<\/code><\/p>\n<p>\u8fd9\u5c06\u5f15\u8d77\u5982\u4e0b\u5f02\u5e38:<\/p>\n<p><code>The Transaction Has Aborted.<\/code><\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u9002\u5f53\u7684preparingEnlistment.RollBack\u91cd\u8f7d\u65b9\u6cd5\u5f15\u8d77\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u79cd\u5f15\u8d77\u56de\u6eda\u7684\u65b9\u6cd5\u662f\u7b80\u5355\u7684\u5220\u6389TransactionScope.Complete\u8bed\u53e5\u6216\u8005\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u6536\u96c6\u5176\u4ed6\u5f15\u8d77\u56de\u6eda\u7684RM\u3002<\/p>\n<p><code>VolatileRM vrm = null ;<br \/>\nusing (TransactionScope txSc = new TransactionScope())<br \/>\n{<br \/>\nvrm = new VolatileRM();<br \/>\nvrm.SetMemberValue(3);<br \/>\n\/\/ txSc.Complete();<br \/>\n}<br \/>\nConsole.WriteLine(\"Member Value:\" + vrm.MemberValue);<\/code><\/p>\n<p>\u5f53\u4f60\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5c06\u4ea7\u751f\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<p><code>VolatileRM: SetMemberValue - EnlistVolatile<br \/>\nVolatileRM: Rollback<br \/>\nMember Value: 0<\/code><\/p>\n<p>\u4f60\u53ef\u80fd\u89c9\u5f97\u8fd9\u4e2a\u8303\u4f8b\u6709\u70b9\u4e0d\u77e5\u6240\u4e91\u3002\u5176\u5b9e\uff0c\u4f60\u901a\u8fc7\u8fd9\u4e2a\u8303\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u4f60\u80fd\u591f\u5982\u6b64\u6709\u6548\u7684\u521b\u5efa\u4e0e\u5176\u4ed6RM\u534f\u540c\u5de5\u4f5c\u7684\u4e8b\u52a1\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4f60\u80fd\u591f\u5c06\u8fd9\u4e2a\u975e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u4fee\u6539\u6210\u5458\u53d8\u91cf\u7684\u64cd\u4f5c\u540c\u6570\u636e\u5e93\u7684\u4e8b\u52a1\u64cd\u4f5c\u653e\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u3002\u4f60\u80fd\u591f\u4f7f\u7528\u5982\u4e0b\u7684T-SQL\u4ee3\u7801\u8f7b\u677e\u5efa\u7acb\u6570\u636e\u5e93\uff1a<\/p>\n<p><code>Create Database Test<br \/>\nGo<br \/>\nCreate Table Demo<br \/>\n(<br \/>\nDemoValue varchar(5)<br \/>\n)<\/code><\/p>\n<p>\u7136\u540e\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u5c06VolatileRM\u548c\u4e24\u4e2aSqlConnection\u5b9e\u4f8b\u6536\u5165\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff1a<\/p>\n<p><code>private static string connStr = \"Data Source=(local);Initial Catalog=Test;Integrated Security=True\";<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\nVolatileRM vrm = null ;<br \/>\nusing (TransactionScope txSc = new TransactionScope())<br \/>\n{<br \/>\nvrm = new VolatileRM();<br \/>\nvrm.SetMemberValue(3);<br \/>\nusing (SqlConnection cn = new SqlConnection(connStr))<br \/>\n{<br \/>\nSqlCommand cmd = cn.CreateCommand();<br \/>\ncmd.CommandText = \"Insert into Demo(DemoValue) Values ('XXX')\";<br \/>\ncn.Open();<br \/>\ncmd.ExecuteNonQuery();<br \/>\ncn.Close();<br \/>\n}<br \/>\nusing (SqlConnection cn = new SqlConnection(connStr))<br \/>\n{<br \/>\nSqlCommand cmd = cn.CreateCommand();<br \/>\ncmd.CommandText = \"Insert into Demo(DemoValue) Values ('YYY')\";<br \/>\ncn.Open();<br \/>\ncmd.ExecuteNonQuery();<br \/>\ncn.Close();<br \/>\n}<br \/>\nConsole.WriteLine( \"Transaction identifier:\" + Transaction.Current.TransactionInformation.DistributedIdentifier);<br \/>\ntxSc.Complete();<br \/>\n}<br \/>\nConsole.WriteLine(\"Member Value:\" + vrm.MemberValue);<br \/>\n}<\/code><\/p>\n<p>\u6846\u67b6\u80fd\u591f\u901a\u8fc7PSPE\u81ea\u52a8\u8bc6\u522b\u51fa\u4f60\u7684SqlConnection\u5b9e\u4f8b\u5df2\u7ecf\u53c2\u4e0e\u5230\u4e8b\u52a1\u4e2d\u3002\u5f53\u7b2c\u4e8c\u4e2aSqlConnection\u5b9e\u4f8b\u51fa\u73b0\u65f6\uff0c\u5b83\u5c06\u5728\u7b2c\u4e8c\u4e2a cn.Open\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\u81ea\u52a8\u5c06\u4e8b\u52a1\u63d0\u5347\u81f3MSDTC\u3002\u4f60\u80fd\u591f\u901a\u8fc7&#8221;\u63a7\u5236\u9762\u677f-&gt;\u7ba1\u7406\u5de5\u5177-&gt;\u7ec4\u4ef6\u670d\u52a1&#8221;\u4e2d\u89c2\u5bdf\u8fd9\u4e9b\u64cd\u4f5c:<\/p>\n<p>\u63d0\u793a: \u8fde\u63a5\u81f3SQL Server 2005\u7684SqlConnection\u91c7\u7528PSPE\u65b9\u5f0f\uff0c\u800c\u8fde\u63a5\u81f3SQL Server 2000\u6216\u66f4\u4f4e\u7248\u672c\u7684SqlConnection\u5c06\u91c7\u7528\u6301\u4e45\u65b9\u5f0f\uff0c\u5373\u4f7f\u5728\u5f53\u524d\u4e8b\u52a1\u4e2d\u4ec5\u6709\u4e00\u4e2aRM\u3002<\/p>\n<p>\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u4ea7\u751f\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<p><code>VolatileRM: SetMemberValue - EnlistVolatile<br \/>\nTransaction identifier:c40015f6-5086-4688-b565-c65db1cbc8e7<br \/>\nVolatileRM: Prepare<br \/>\nVolatileRM: Commit<br \/>\nMember Value:3<\/code><\/p>\n<p>\u5982\u4f60\u6240\u89c1\uff0c\u4f60\u4f9d\u7136\u5728\u6536\u96c6\u6613\u53d8\u8d44\u6e90\u3002\u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\uff0c\u4f60\u7684\u4e8b\u52a1\u5c06\u81ea\u52a8\u63d0\u5347\u81f3MSDTC\uff0c\u5e76\u4e14\u83b7\u5f97\u5206\u5e03\u5f0fID\u3002\u5982\u679c\u4f60\u662f\u67b6\u6784\u5e08\uff0c\u8fd9\u5c06\u7ed9\u4e88\u4f60\u4e24\u4e2a\u6700\u597d\u7684\u4e16\u754c\uff1a\u4e8b\u52a1\u96c6\u6210\u548c\u53ef\u80fd\u6700\u597d\u7684\u6027\u80fd\u3002\u4f60\u83b7\u5f97\u8fd9\u4e9b\u4ec5\u4ec5\u9700\u8981\u5199\u5982\u4e0b\u7684\u7b80\u5355\u7684\u4ee3\u7801\uff1a<\/p>\n<p><code>Transaction<br \/>\n{<br \/>\nOperation A ;<br \/>\nOperation B ;<br \/>\n....<br \/>\n....<br \/>\nCommit();<br \/>\n}<\/code><\/p>\n<p>\u5b9e\u73b0\u57fa\u4e8eSystem.Transactions\u7684\u4e8b\u52a1<\/p>\n<p>\u73b0\u5728\u4f60\u5df2\u7ecf\u5177\u5907\u4e86\u57fa\u4e8eSystem.Transactions\u5b9e\u73b0\u4e8b\u52a1\u5904\u7406\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u4f60\u5b66\u5230\u4e86\u5404\u79cd\u5404\u6837\u7684\u5c06\u8d44\u6e90\u6536\u96c6\u5230\u5f53\u524d\u4e8b\u52a1\u4e2d\u7684\u65b9\u6cd5\uff0c\u5e76\u4e14\u770b\u5230\u4e86\u81ea\u5b9a\u4e49RM\u6536\u96c6\u6613\u53d8\u8d44\u6e90\u662f\u591a\u4e48\u7684\u7b80\u5355\u3002<\/p>\n<p><strong>\u4e0b\u8f7d\u4ee3\u7801<\/strong><\/p>\n<p>\u4e0b\u8f7d\u76f8\u5173\u4ee3\u7801\uff0c\u70b9\u51fb<a href=\"http:\/\/www.developer.com\/img\/2005\/11\/ResourceMgr.zip\">\u8fd9\u91cc<\/a>\u3002<\/p>\n<p><strong>\u5173\u4e8e\u4f5c\u8005<\/strong><\/p>\n<p>Sahil Malik\u719f\u6089\u4eceDos\u81f3.Net\u7b49\u8bb8\u591a\u5c16\u7aef\u7684\u5fae\u8f6f\u79d1\u6280\u3002\u4ed6\u662fPro ADO.NET 2.0\u7684\u4f5c\u8005\u4ee5\u53caPro ADO.NET with VB.NET 1.1\u7684\u5408\u8457\u8005\u3002Sahil\u76ee\u524d\u4e5f\u5728\u57fa\u4e8eADO.NET 2.0\u7684\u591a\u5a92\u4f53\u9886\u57df\u6709\u6240\u5efa\u6811\u3002\u4ed6\u66fe\u88ab\u6388\u4e88\u5fae\u8f6fMVP\u79f0\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>System.Transactions:\u5b9e\u73b0\u4f60\u81ea\u5df1\u7684Resource Manager By Sahil Mal &hellip; <a href=\"https:\/\/blog.axqd.net\/?p=426\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cSystem.Transactions:\u5b9e\u73b0\u4f60\u81ea\u5df1\u7684Resource Manager\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-426","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"https:\/\/blog.axqd.net\/index.php?rest_route=\/wp\/v2\/posts\/426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.axqd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.axqd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.axqd.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.axqd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=426"}],"version-history":[{"count":0,"href":"https:\/\/blog.axqd.net\/index.php?rest_route=\/wp\/v2\/posts\/426\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.axqd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.axqd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.axqd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}