{"id":187822,"date":"2024-11-11T17:28:18","date_gmt":"2024-11-11T09:28:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/187822\/"},"modified":"2024-11-11T17:28:19","modified_gmt":"2024-11-11T09:28:19","slug":"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e4%ba%8b%e5%8b%99%e8%99%95%e7%90%86%e7%b3%bb%e7%b5%b1%e6%87%89%e7%95%b6%e5%85%b7%e5%82%99%e7%9a%84%e5%9b%9b%e5%80%8b%e7%89%b9%e5%be%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/187822\/","title":{"rendered":"\u6578\u64da\u5eab\u4e2d\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\u61c9\u7576\u5177\u5099\u7684\u56db\u500b\u7279\u5fb5"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e4%ba%8b%e5%8b%99%e8%99%95%e7%90%86%e7%b3%bb%e7%b5%b1%e6%87%89%e7%95%b6%e5%85%b7%e5%82%99%e7%9a%84%e5%9b%9b%e5%80%8b%e7%89%b9%e5%be%b5-tpdjTbmReq\">\u6578\u64da\u5eab\u4e2d\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\u61c9\u7576\u5177\u5099\u7684\u56db\u500b\u7279\u5fb5<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\uff08Transaction Processing Systems, TPS\uff09\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\u3002\u9019\u4e9b\u7cfb\u7d71\u4e0d\u50c5\u652f\u6301\u65e5\u5e38\u696d\u52d9\u64cd\u4f5c\uff0c\u9084\u78ba\u4fdd\u6578\u64da\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6578\u64da\u5eab\u4e2d\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\u61c9\u7576\u5177\u5099\u7684\u56db\u500b\u95dc\u9375\u7279\u5fb5\u3002<\/p>\n<h2 id=\"1-%e5%8e%9f%e5%ad%90%e6%80%a7%ef%bc%88atomicity%ef%bc%89-tpdjTbmReq\">1. \u539f\u5b50\u6027\uff08Atomicity\uff09<\/h2>\n<p>\u539f\u5b50\u6027\u662f\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\u7684\u4e00\u500b\u57fa\u672c\u7279\u5fb5\uff0c\u6307\u7684\u662f\u4e8b\u52d9\u8981\u9ebc\u5b8c\u5168\u57f7\u884c\uff0c\u8981\u9ebc\u5b8c\u5168\u4e0d\u57f7\u884c\u3002\u9019\u610f\u5473\u8457\u5728\u4e8b\u52d9\u904e\u7a0b\u4e2d\uff0c\u4efb\u4f55\u4e00\u500b\u6b65\u9a5f\u7684\u5931\u6557\u90fd\u6703\u5c0e\u81f4\u6574\u500b\u4e8b\u52d9\u7684\u56de\u6efe\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u8655\u65bc\u4e0d\u4e00\u81f4\u7684\u72c0\u614b\u3002<\/p>\n<pre><code>-- \u4f8b\u5b50\uff1a\u5728\u9280\u884c\u8f49\u8cec\u4e2d\nBEGIN TRANSACTION;\nUPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 'A';\nUPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 'B';\nCOMMIT; -- \u5982\u679c\u6240\u6709\u6b65\u9a5f\u6210\u529f\uff0c\u5247\u63d0\u4ea4\nROLLBACK; -- \u5982\u679c\u4efb\u4f55\u6b65\u9a5f\u5931\u6557\uff0c\u5247\u56de\u6efe\n<\/code><\/pre>\n<h2 id=\"2-%e4%b8%80%e8%87%b4%e6%80%a7%ef%bc%88consistency%ef%bc%89-tpdjTbmReq\">2. \u4e00\u81f4\u6027\uff08Consistency\uff09<\/h2>\n<p>\u4e00\u81f4\u6027\u78ba\u4fdd\u4e8b\u52d9\u5728\u57f7\u884c\u524d\u5f8c\uff0c\u6578\u64da\u5eab\u7684\u72c0\u614b\u5fc5\u9808\u4fdd\u6301\u4e00\u81f4\u3002\u9019\u610f\u5473\u8457\u6240\u6709\u7684\u6578\u64da\u7d04\u675f\u548c\u898f\u5247\u5fc5\u9808\u5728\u4e8b\u52d9\u57f7\u884c\u5f8c\u5f97\u5230\u6eff\u8db3\u3002\u4f8b\u5982\uff0c\u5728\u9280\u884c\u7cfb\u7d71\u4e2d\uff0c\u8f49\u8cec\u5f8c\u5169\u500b\u8cec\u6236\u7684\u9918\u984d\u5fc5\u9808\u7b26\u5408\u9810\u5b9a\u7684\u898f\u5247\u3002<\/p>\n<pre><code>-- \u4f8b\u5b50\uff1a\u8f49\u8cec\u5f8c\u7684\u9918\u984d\u6aa2\u67e5\nIF (SELECT Balance FROM Accounts WHERE AccountID = 'A') &lt; 0 THEN\n    ROLLBACK; -- \u5982\u679c\u9918\u984d\u4e0d\u8db3\uff0c\u5247\u56de\u6efe\nEND IF;\n<\/code><\/pre>\n<h2 id=\"3-%e9%9a%94%e9%9b%a2%e6%80%a7%ef%bc%88isolation%ef%bc%89-tpdjTbmReq\">3. \u9694\u96e2\u6027\uff08Isolation\uff09<\/h2>\n<p>\u9694\u96e2\u6027\u78ba\u4fdd\u540c\u6642\u57f7\u884c\u7684\u4e8b\u52d9\u4e0d\u6703\u4e92\u76f8\u5e72\u64fe\u3002\u9019\u610f\u5473\u8457\u4e00\u500b\u4e8b\u52d9\u7684\u57f7\u884c\u4e0d\u61c9\u8a72\u5f71\u97ff\u5230\u5176\u4ed6\u4e8b\u52d9\u7684\u57f7\u884c\u7d50\u679c\u3002\u9694\u96e2\u6027\u901a\u5e38\u901a\u904e\u9396\u5b9a\u6a5f\u5236\u4f86\u5be6\u73fe\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u7af6\u722d\u689d\u4ef6\u7684\u767c\u751f\u3002<\/p>\n<pre><code>-- \u4f8b\u5b50\uff1a\u4f7f\u7528\u9396\u5b9a\u4f86\u5be6\u73fe\u9694\u96e2\nBEGIN TRANSACTION;\nLOCK TABLE Accounts IN EXCLUSIVE MODE; -- \u9396\u5b9a\u8868\u4ee5\u9632\u6b62\u5176\u4ed6\u4e8b\u52d9\u8a2a\u554f\n-- \u57f7\u884c\u4e8b\u52d9\u64cd\u4f5c\nCOMMIT;\n<\/code><\/pre>\n<h2 id=\"4-%e6%8c%81%e4%b9%85%e6%80%a7%ef%bc%88durability%ef%bc%89-tpdjTbmReq\">4. \u6301\u4e45\u6027\uff08Durability\uff09<\/h2>\n<p>\u6301\u4e45\u6027\u78ba\u4fdd\u4e00\u65e6\u4e8b\u52d9\u88ab\u63d0\u4ea4\uff0c\u5176\u7d50\u679c\u5c07\u6c38\u4e45\u4fdd\u5b58\uff0c\u5373\u4f7f\u7cfb\u7d71\u767c\u751f\u6545\u969c\u4e5f\u4e0d\u6703\u4e1f\u5931\u3002\u9019\u901a\u5e38\u901a\u904e\u5c07\u6578\u64da\u5beb\u5165\u6301\u4e45\u5b58\u5132\uff08\u5982\u786c\u789f\uff09\u4f86\u5be6\u73fe\uff0c\u4e26\u4f7f\u7528\u65e5\u8a8c\u8a18\u9304\u4f86\u4fdd\u8b77\u6578\u64da\u3002<\/p>\n<pre><code>-- \u4f8b\u5b50\uff1a\u4e8b\u52d9\u63d0\u4ea4\u5f8c\u7684\u65e5\u8a8c\u8a18\u9304\nCOMMIT;\nINSERT INTO TransactionLog (TransactionID, Status) VALUES (12345, 'COMMITTED');\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tpdjTbmReq\">\u7e3d\u7d50<\/h2>\n<p>\u6578\u64da\u5eab\u4e2d\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\u7684\u56db\u500b\u95dc\u9375\u7279\u5fb5\u2014\u2014\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u96e2\u6027\u548c\u6301\u4e45\u6027\u2014\u2014\u5171\u540c\u78ba\u4fdd\u4e86\u6578\u64da\u7684\u5b8c\u6574\u6027\u548c\u53ef\u9760\u6027\u3002\u9019\u4e9b\u7279\u5fb5\u4e0d\u50c5\u5c0d\u65bc\u91d1\u878d\u3001\u96fb\u5b50\u5546\u52d9\u7b49\u884c\u696d\u81f3\u95dc\u91cd\u8981\uff0c\u9084\u5c0d\u65bc\u4efb\u4f55\u9700\u8981\u9ad8\u53ef\u9760\u6027\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u7a0b\u5e8f\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u82e5\u60a8\u5c0d\u65bc\u5982\u4f55\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4ee5\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u8a0e\u6578\u64da\u5eab\u4e2d\u4e8b\u52d9\u8655\u7406\u7cfb\u7d71\u61c9\u5177\u5099\u7684\u56db\u500b\u95dc\u9375\u7279\u5fb5\uff0c\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u3001\u53ef\u9760\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-187822","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=187822"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187822\/revisions"}],"predecessor-version":[{"id":187823,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187822\/revisions\/187823"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=187822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=187822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=187822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}