{"id":1400,"date":"2023-03-19T11:17:49","date_gmt":"2023-03-19T03:17:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1400"},"modified":"2023-04-28T21:09:42","modified_gmt":"2023-04-28T13:09:42","slug":"principles-and-usage-scenarios-of-pessimistic-and-optimistic-locks","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/principles-and-usage-scenarios-of-pessimistic-and-optimistic-locks\/","title":{"rendered":"\u60b2\u89c2\u9501\u548c\u4e50\u89c2\u9501\u7684\u539f\u7406\u53ca\u4f7f\u7528\u573a\u666f"},"content":{"rendered":"<h2>\u60b2\u89c2\u9501\u548c\u4e50\u89c2\u9501\u7684\u533a\u522b<\/h2>\n<h3>\u60b2\u89c2\u9501<\/h3>\n<p>\u6bcf\u6b21\u62ff\u6570\u636e\u90fd\u4ee5\u4e3a\u522b\u4eba\u4f1a\u4fee\u6539\uff0c\u6240\u4ee5\u6bcf\u6b21\u62ff\u6570\u636e\u65f6\u90fd\u4f1a\u4e0a\u9501\u3002<br \/>\n\u5b9e\u73b0\uff1a\u5f00\u542f\u4e8b\u52a1\uff0c\u542f\u7528\u9501\u673a\u5236<\/p>\n<p><!-- more --><\/p>\n<h3>\u4e50\u89c2\u9501<\/h3>\n<p>\u6bcf\u6b21\u62ff\u6570\u636e\u65f6\u5019\u90fd\u8ba4\u4e3a\u522b\u4eba\u4e0d\u4f1a\u4fee\u6539\uff0c\u6240\u4ee5\u4e0d\u4f1a\u4e0a\u9501\uff0c\u4f46\u662f\u5728\u66f4\u65b0\u6570\u636e\u65f6\u5019\u4f1a\u5224\u65ad\u5728\u6b64\u671f\u95f4\u662f\u5426\u6709\u4eba\u66f4\u65b0\u8fc7\u3002<br \/>\n\u5b9e\u73b0\uff1a1.\u4f7f\u7528\u7248\u672c\u53f7 2.\u4f7f\u7528\u65f6\u95f4\u6233<\/p>\n<h3>\u5bf9\u6bd4<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u672f\u8bed<\/th>\n<th style=\"text-align: left;\">\u63cf\u8ff0<\/th>\n<th style=\"text-align: left;\">\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">\u4e50\u89c2\u9501<\/td>\n<td style=\"text-align: left;\">\u6bcf\u6b21\u53bb\u62ff\u6570\u636e\u7684\u65f6\u5019\u90fd\u8ba4\u4e3a\u522b\u4eba\u4e0d\u4f1a\u4fee\u6539\uff0c<br \/>\u6240\u4ee5\u4e0d\u4f1a\u4e0a\u9501\uff0c\u4f46\u662f\u5728\u66f4\u65b0\u7684\u65f6\u5019<br \/>\u4f1a\u5224\u65ad\u4e00\u4e0b\u5728\u6b64\u671f\u95f4\u522b\u4eba\u6709\u6ca1\u6709\u53bb\u66f4\u65b0\u8fd9\u4e2a\u6570\u636e<\/td>\n<td style=\"text-align: left;\">\u7248\u672c\u53f7\u6216\u65f6\u95f4\u6233\u63a7\u5236\uff0c<br \/>\u9002\u7528\u4e8e\u591a\u8bfb\u5c11\u5199\u7684\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u60b2\u89c2\u9501<\/td>\n<td style=\"text-align: left;\">\u6bcf\u6b21\u53bb\u62ff\u6570\u636e\u7684\u65f6\u5019\u90fd\u8ba4\u4e3a\u522b\u4eba\u4f1a\u4fee\u6539\uff0c<br \/>\u6240\u4ee5\u6bcf\u6b21\u5728\u62ff\u6570\u636e\u7684\u65f6\u5019\u90fd\u4f1a\u4e0a\u9501\uff0c<br \/>\u8fd9\u6837\u522b\u4eba\u60f3\u62ff\u8fd9\u4e2a\u6570\u636e\u5c31\u4f1ablock\u76f4\u5230\u5b83\u62ff\u5230\u9501<\/td>\n<td style=\"text-align: left;\">DB\u7684\u884c\u9501\u3001\u8868\u9501\u7b49\uff0c<br \/>\u9002\u7528\u4e8e\u6570\u636e\u4e00\u81f4\u6027\u6bd4\u8f83\u9ad8\u7684\u573a\u666f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<h3>\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u4e50\u89c2\u9501<\/h3>\n<p>\u8d44\u6e90\u63d0\u4ea4\u51b2\u7a81\uff0c\u5176\u4ed6\u4f7f\u7528\u65b9\u9700\u8981\u91cd\u65b0\u8bfb\u53d6\u8d44\u6e90\uff0c\u4f1a\u589e\u52a0\u8bfb\u7684\u6b21\u6570\uff0c\u4f46\u662f\u53ef\u4ee5\u9762\u5bf9\u9ad8\u5e76\u53d1\u573a\u666f\uff0c\u524d\u63d0\u662f\u5982\u679c\u51fa\u73b0\u63d0\u4ea4\u5931\u8d25\uff0c\u7528\u6237\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u56e0\u6b64\u4e00\u822c\u4e50\u89c2\u9501\u53ea\u7528\u5728\u9ad8\u5e76\u53d1\u3001\u591a\u8bfb\u5c11\u5199\u7684\u573a\u666f\u3002<br \/>\n\u5176\u4e2d\uff1aGIT,SVN,CVS\u7b49\u4ee3\u7801\u7248\u672c\u63a7\u5236\u7ba1\u7406\u5668\uff0c\u5c31\u662f\u4e00\u4e2a\u4e50\u89c2\u9501\u4f7f\u7528\u5f88\u597d\u7684\u573a\u666f\uff0c\u4f8b\u5982\uff1aA\u3001B\u7a0b\u5e8f\u5458\uff0c\u540c\u65f6\u4eceSVN\u670d\u52a1\u5668\u4e0a\u4e0b\u8f7d\u4e86code.html\u6587\u4ef6\uff0c\u5f53A\u5b8c\u6210\u63d0\u4ea4\u540e\uff0c\u6b64\u65f6B\u518d\u63d0\u4ea4\uff0c\u90a3\u4e48\u4f1a\u62a5\u7248\u672c\u51b2\u7a81\uff0c\u6b64\u65f6\u9700\u8981B\u8fdb\u884c\u7248\u672c\u5904\u7406\u5408\u5e76\u540e\uff0c\u518d\u63d0\u4ea4\u5230\u670d\u52a1\u5668\u3002\u8fd9\u5176\u5b9e\u5c31\u662f\u4e50\u89c2\u9501\u7684\u5b9e\u73b0\u5168\u8fc7\u7a0b\u3002\u5982\u679c\u6b64\u65f6\u4f7f\u7528\u7684\u662f\u60b2\u89c2\u9501\uff0c\u90a3\u4e48\u610f\u5473\u8005\u6240\u6709\u7a0b\u5e8f\u5458\u90fd\u5fc5\u987b\u4e00\u4e2a\u4e00\u4e2a\u7b49\u5f85\u64cd\u4f5c\u63d0\u4ea4\u5b8c\uff0c\u624d\u80fd\u8bbf\u95ee\u6587\u4ef6\uff0c\u8fd9\u662f\u96be\u4ee5\u63a5\u53d7\u7684\u3002<\/p>\n<h3>\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u60b2\u89c2\u9501<\/h3>\n<p>\u4e00\u65e6\u901a\u8fc7\u60b2\u89c2\u9501\u9501\u5b9a\u4e00\u4e2a\u8d44\u6e90\uff0c\u90a3\u4e48\u5176\u4ed6\u9700\u8981\u64cd\u4f5c\u8be5\u8d44\u6e90\u7684\u4f7f\u7528\u65b9\uff0c\u53ea\u80fd\u7b49\u5f85\u76f4\u5230\u9501\u88ab\u91ca\u653e\uff0c\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u51cf\u5c11\u5e76\u53d1\uff0c\u4f46\u662f\u5f53\u5e76\u53d1\u91cf\u975e\u5e38\u5927\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u9501\u6d88\u8017\u8d44\u6e90\uff0c\u5e76\u4e14\u53ef\u80fd\u9501\u5b9a\u65f6\u95f4\u8fc7\u957f\uff0c\u5bb9\u6613\u5bfc\u81f4\u7cfb\u7edf\u6027\u80fd\u4e0b\u964d\uff0c\u8d44\u6e90\u6d88\u8017\u4e25\u91cd\u3002\u56e0\u6b64\u4e00\u822c\u6211\u4eec\u53ef\u4ee5\u5728\u5e76\u53d1\u91cf\u4e0d\u662f\u5f88\u5927\uff0c\u5e76\u4e14\u51fa\u73b0\u5e76\u53d1\u60c5\u51b5\u5bfc\u81f4\u7684\u5f02\u5e38\u7528\u6237\u548c\u7cfb\u7edf\u90fd\u5f88\u96be\u4ee5\u63a5\u53d7\u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u9009\u62e9\u60b2\u89c2\u9501\u8fdb\u884c\u3002<\/p>\n<h2>\u793a\u4f8b<\/h2>\n<h3>\u4e50\u89c2\u9501<\/h3>\n<h4>\u7248\u672c\u53f7\u63a7\u5236<\/h4>\n<pre><code>1, start transaction\n2, first_version = get_cur_version() \/\/ \u83b7\u53d6\u5f53\u524d\u6570\u636e\u7248\u672c\n3, update_data(version+=1)           \/\/ \u66f4\u65b0\u64cd\u4f5c\u7248\u672c\u53f7+1\n4, cur_version = get_cur_version()   \/\/ \u63d0\u4ea4\u66f4\u65b0\u65f6\uff0c\u83b7\u53d6\u7248\u672c\u53f7\n5, if first_version == cur_version   \/\/ \u6bd4\u8f83\u63d0\u4ea4\u65f6\u7684\u7248\u672c\u53f7\u4e0e\u7b2c\u4e00\u6b21\u83b7\u53d6\u7684\u7248\u672c\u53f7\uff0c\u5982\u679c\u4e00\u81f4\uff0c\u90a3\u4e48\u8ba4\u4e3a\u8d44\u6e90\u662f\u6700\u65b0\u7684\uff0c\u53ef\u4ee5\u66f4\u65b0\n     then commit \n  else rollback or raise exception  \/\/ \u5426\u5219\u56de\u6eda\u6216\u8005\u629b\u51fa\u5f02\u5e38<\/code><\/pre>\n<h4>\u65f6\u95f4\u6233\u63a7\u5236<\/h4>\n<pre><code>1, start transaction\n2, first_timestamp = get_cur_timestamp()\n3, update_data(timestamp=get_sys_cur_timestamp)\n4, if first_timestamp = get_cur_timestamp()\n      then commit\n   else rollback or raise Exception<\/code><\/pre>\n<h3>\u60b2\u89c2\u9501<\/h3>\n<p>\u60b2\u89c2\u9501\u7684\u5b9e\u73b0\u4e00\u822c\u90fd\u662f\u901a\u8fc7\u9501\u673a\u5236\u6765\u5b9e\u73b0\u7684\uff0c\u9501\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u8bbf\u95ee\u7684\u5165\u53e3\u3002\u5982\u679c\u8981\u5bf9\u4e00\u4e2a\u5177\u6709\u9501\u5c5e\u6027\u7684\u8d44\u6e90\u6267\u884c\u8bbf\u95ee\u65f6\uff0c\u5728\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u6301\u9501\u6743\u624d\u80fd\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f46\u662f\u5f80\u5f80\u8fd9\u79cd\u64cd\u4f5c\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u4f8b\u5982\u6570\u636e\u5e93\u8868\u7684\u884c\u9501\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60b2\u89c2\u9501\u548c\u4e50\u89c2\u9501\u7684\u533a\u522b \u60b2\u89c2\u9501 \u6bcf\u6b21\u62ff\u6570\u636e\u90fd\u4ee5\u4e3a\u522b\u4eba\u4f1a\u4fee\u6539\uff0c\u6240\u4ee5\u6bcf\u6b21\u62ff\u6570\u636e\u65f6\u90fd\u4f1a\u4e0a\u9501\u3002 \u5b9e\u73b0\uff1a\u5f00\u542f\u4e8b\u52a1\uff0c\u542f\u7528\u9501\u673a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[52],"class_list":["post-1400","post","type-post","status-publish","format-standard","hentry","category-database","tag-lock"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1400"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1400\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}