{"id":322792,"date":"2021-02-06T00:48:20","date_gmt":"2021-02-05T16:48:20","guid":{"rendered":"http:\/\/4563.org\/?p=322792"},"modified":"2021-02-06T00:48:20","modified_gmt":"2021-02-05T16:48:20","slug":"%e8%a2%ab-mysql-%e7%9a%84%e4%ba%8b%e5%8a%a1-%e5%92%8c%e9%94%81-%e6%90%9e%e6%87%b5%e4%ba%86","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=322792","title":{"rendered":"\u88ab mysql \u7684\u4e8b\u52a1 \u548c\u9501 \u641e\u61f5\u4e86"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u88ab mysql \u7684\u4e8b\u52a1 \u548c\u9501 \u641e\u61f5\u4e86               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5199\u4e86\u4e00\u4e2a\u63a5\u53e3\u3002<\/p>\n<p>update order_a u set u.product_price=?2 where u.id=?1 <\/p>\n<p>\u5f00\u542f 50 \u4e2a\u7ebf\u7a0b\u8c03\u63a5\u53e3\u4fee\u6539 product_price\uff0cid \u4e3a\u4e3b\u952e\u3002<\/p>\n<p>\u7528\u4e50\u89c2\u9501 \u6570\u636e\u6b63\u5e38\u3002<\/p>\n<p>\u4f7f\u7528\u4e8b\u52a1\uff0c\u5f53\u9694\u79bb\u7ea7\u522b\u4e3a SERIALIZABLE \u65f6\uff0c \u4f1a\u6210\u529f\u90e8\u5206\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4f1a\u62a5\u6b7b\u9501\u3002 \u4f7f\u7528\u4e8b\u52a1\uff0c\u5f53\u9694\u79bb\u7ea7\u522b\u4e3a REPEATABLE_READ\uff0cREAD_COMMITTED\uff0cREAD_UNCOMMITTED\uff0c\u90fd\u4f1a\u5168\u90e8\u6210\u529f\uff0c\u53ea\u662f\u6570\u636e\u4e0d\u6b63\u786e\u3002<\/p>\n<p>\u4f7f\u7528\u4e8b\u52a1\u4e0d\u662f\u4f1a\u52a0\u9501\u5417\uff1f \u4e3a\u4ec0\u4e48\u4f1a\u6570\u636e\u4e0d\u6b63\u5e38\uff1f\u5927\u4f6c\u4eec\u6c42\u6307\u70b9<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>18<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4981842\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xxxyh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5efa\u8bae\u628a\u63a5\u53e3\u4ee3\u7801\u653e\u4e0a\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981843\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bbao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             mysql \u9ed8\u8ba4\u81ea\u52a8\u63d0\u4ea4\u3002<br \/>\u4f7f\u7528\u4e8b\u52a1\uff0c\u9700\u8981\u63d0\u524d\u5f00\u59cb\u4e8b\u52a1\u3002<\/p>\n<p>start transaction;<br \/>select xx from table where condition = ? for update ; \uff08\u60b2\u89c2\u9501\uff09<\/p>\n<p>\u5176\u4ed6 session \u67e5\u8be2\u76f8\u540c condition \u65f6\uff0c\u4f1a\u88ab\u9501\u4f4f\uff1b\u76f4\u5230\u8d85\u65f6\u6216\u8005\u5bf9\u65b9 commit;<\/p>\n<p>\u4e50\u89c2\u9501\uff1a<br \/>select xx from table where condition = ? and u.id = ? and \u5f85\u4fee\u6539\u5b57\u6bb5 = \u539f\u59cb\u503c\uff1b<\/p>\n<p>\u5982\u679c\u5f85\u4fee\u6539\u5b57\u6bb5 \uff01= \u539f\u59cb\u503c\uff0c\u8868\u793a\u5df2\u7ecf\u6709\u4eba\u4fee\u6539\u4e86\u6570\u636e\uff0c\u4f60\u5c31\u76f4\u63a5\u4e1a\u52a1\u8fd4\u56de\u5c31\u53ef\u4ee5\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981844\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lllllliu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u4e2a update \u53ea\u9488\u5bf9\u4e00\u6761\u6570\u636e\u7684\u8bdd\u6ca1\u5fc5\u8981\u52a0\u7d22\u5427\u3002\u66f4\u65b0\u5931\u8d25\u5c31\u5931\u8d25\u3002<br \/>\u4e5f\u53ef\u4ee5\u5148\u9501\u4f4f\u8fd9\u4e00\u6761\uff0cselect for update \u3002\u8fd9\u6837\u5176\u4ed6\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u4e2a id \u7684\u65f6\u5019\u4f1a\u7b49\u5f85\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981845\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cheng6563 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5355\u6761 SQL \u5f00\u4e0d\u5f00\u6ca1\u533a\u522b\u7684\uff0c\u5f00\u4e8b\u52a1\u5176\u5b9e\u53ea\u662f\u5173\u6389\u81ea\u52a8\u63d0\u4ea4\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981846\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bruce0 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/www.jianshu.com\/p\/fe708aad6113<\/p>\n<p>\u53ef\u4ee5\u770b\u4e00\u4e0b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981847\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xxxyh <\/p>\n<p> @Transactional(isolation = Isolation.REPEATABLE_READ)<br \/> @Override<br \/> public String changePrice(Integer num,String id) {<br \/> OrderA one = orderAReposity.getOne(&#8220;1&#8221;);<br \/> BigDecimal price = one.getProductPrice().add(BigDecimal.valueOf(num));<br \/> int i = orderAReposity.changePrice1(&#8220;1&#8221;, price);<br \/> return i+&#8221;&#8221;;<\/p>\n<p> }<br \/>\u63a5\u53e3\u4ee3\u7801                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981848\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @bruce0 \u611f\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981849\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xxxyh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kikione \u63a5\u53e3\u4ee3\u7801\u6709\u95ee\u9898\uff0c\u524d\u9762 select \u7684\u65f6\u5019\u6ca1\u52a0\u9501\uff0c\u540e\u9762 update \u7684\u65f6\u5019\u7ed3\u679c\u9519\u8bef\u662f\u6b63\u5e38\u7684\uff0c\u5982\u679c\u4e0d\u60f3\u52a0\u9501\u7684\u8bdd\uff0c\u53ef\u4ee5\u628a sql \u5199\u6210 set product_price = product_price + num                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981850\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : notejava <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e8b\u52a1 != \u52a0\u9501<br \/>\u4e8b\u52a1\u53ea\u80fd\u4fdd\u8bc1\u4e8b\u52a1\u5185\u7684\u8bed\u53e5\u8981\u4e48\u5168\u90e8\u6267\u884c\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002<br \/>\u52a0\u9501\u662f\u540c\u4e00\u65f6\u523b\uff0c\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u6570\u636e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981851\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xxxyh \u611f\u8c22\uff0c\u786e\u5b9e\u662f select \u6ca1\u6709\u9501\u7684\u95ee\u9898\uff0c\u6211\u521a\u60f3\u5230\uff0c\u6211\u6539\u4e00\u4e0b\uff0c\u518d\u8dd1\u8dd1\u770b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981852\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : keepeye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u636e\u6211\u6240\u77e5\uff1a\u9664\u4e86 SERIALIZABLE\uff0c\u5176\u4ed6\u7ea7\u522b\u53ef\u80fd\u53d1\u751f\u810f\u8bfb\u6216\u5e7b\u8bfb\u73b0\u8c61\u3002SERIALIZABLE \u7ea7\u522b\u5728\u4e8b\u52a1\u4e2d\u6709 select \u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u9020\u6210 update \u6b7b\u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981853\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : keepeye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8981\u4e32\u884c\u5316\uff0c\u90a3\u5c31 select for update \u5427\uff0c\u4f46\u8981\u5c0f\u5fc3\u6027\u80fd\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981854\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cmai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             RR \u8bfb\u5728\u4e0d\u52a0 S\/X \u9501\u7684\u60c5\u51b5\u4e0b\u662f\u5feb\u7167\u8bfb\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u540c\u65f6\u8bfb\u5230\u4e86\u540c\u4e00\u7248\u672c\u7684\u6570\u636e\uff0c\u7136\u540e\u505a\u66f4\u65b0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981855\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cmai RR \u8bfb\u5728\u4e0d\u52a0 S\/X \u9501\u7684\u60c5\u51b5\u4e0b\u662f\u5feb\u7167\u8bfb \u8fd9\u53e5\u8bdd\u4e0d\u662f\u5f88\u61c2\uff0cmysql \u529f\u529b\u4e0d\u8db3<br \/>\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u540c\u65f6\u8bfb\u5230\u4e86\u540c\u4e00\u7248\u672c\u7684\u6570\u636e\uff0c\u7136\u540e\u505a\u66f4\u65b0 \u8fd9\u53e5\u8bdd\u8bfb\u61c2\u4e86\uff0c\u8c22\u8c22\u3002 <\/p>\n<p>update order_a u set u.product_price=u.product_price+price where u.id=1 \u6211\u6539\u6210\u8fd9\u6837\u7684\u8bdd\uff0c\u6570\u636e\u6b63\u5e38\u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981856\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @keepeye \u5bf9\u7684\uff0c \u6211\u5c31\u662f\u6709 select \u8bed\u53e5\uff0c\u5c11\u5199\u4e86 sql\uff0c\u6211\u7684\u9505\uff0c\u8c22\u8c22\u60a8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981857\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kikione <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cheng6563<br \/> \u5927\u54e5\uff0c\u60f3\u8bf7\u95ee\u60a8\uff0c\u5355\u6761 sql \u7684\u8bdd\uff0c\u591a\u7ebf\u7a0b\u66f4\u65b0\u5b57\u6bb5\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u51fa\u9519<br \/>\uff0c\u51e0\u767e\u6761\u7ebf\u7a0b\u53bb\u66f4\u65b0\uff0c\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u6570\u636e\u9519\u8bef\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981858\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cheng6563 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kikione <br \/>\u5e38\u89c1\u51e0\u79cd\u60c5\u51b5\u6211\u5217\u4e0b\u5427\u3002<\/p>\n<p>1. \u4e8b\u52a1\u5185\u5355\u6761 update<br \/>update \u6709\u884c\u9501\uff0c\u63d0\u4ea4\u4e4b\u540e\u4e0b\u4e00\u4e2a update \u624d\u80fd\u62ff\u5230\u9501\u7ee7\u7eed\u64cd\u4f5c\uff0c\u5982\u679c\u662f update xx set version=version+1 \u8fd9\u6837\u66f4\u65b0\u662f\u6ca1\u95ee\u9898\u7684\u3002<\/p>\n<p>2.\u4e8b\u52a1\u5185\u5148 select\uff0c\u7136\u540e\u6839\u636e select \u7684\u7ed3\u679c\u518d update \u3002<br \/>\u6bd4\u5982 select version from xx \u628a version \u653e\u7a0b\u5e8f\u53d8\u91cf\u91cc\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u91cc\u8fdb\u884c version++\uff0c\u518d update xx set version=?\u3002<br \/>\u8fd9\u79cd\u60c5\u51b5 select \u662f\u4e0d\u52a0\u9501\u7684\uff0c\u591a\u4e2a\u7ebf\u7a0b\u4f1a\u4e00\u8d77\u62ff\u5230\u4e00\u4e2a\u76f8\u540c\u7684 version\uff0c\u540e\u7eed\u7684 update \u53ef\u80fd\u90fd\u662f\u8bbe\u7f6e\u4e86\u76f8\u540c\u7684\u503c\u3002<\/p>\n<p>3.\u4e8b\u52a1\u5185\u5148 select for update\uff0c\u7136\u540e\u6839\u636e select \u7684\u7ed3\u679c\u518d update \u3002<br \/>select \u52a0\u4e86 for update \u540e\u4e5f\u4f1a\u52a0\u884c\u9501\uff0c\u5728\u4f60\u8fd9\u4e2a\u4e8b\u52a1\u63d0\u4ea4\u524d\u5176\u4ed6\u7ebf\u7a0b\u7684 select for update \u4e5f\u4f1a\u5361\u4f4f\uff0c\u76f4\u5230\u4e8b\u52a1\u63d0\u4ea4\u540e\u624d\u80fd select for update\uff0c\u6570\u636e\u4e5f\u6ca1\u95ee\u9898\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4981859\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zifangsky <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684\u95ee\u9898\u4e0d\u5728\u4e8e\u6570\u636e\u5e93\u4e2d\u600e\u4e48\u52a0\u9501\uff0c\u800c\u5728\u4e8e\u4f60\u9700\u8981\u7ed9\u4f60\u7684\u6574\u4e2a\u4e1a\u52a1\u65b9\u6cd5\u52a0\u9501\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u88ab mysql \u7684\u4e8b\u52a1 \u548c\u9501 \u641e\u61f5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/322792"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322792"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/322792\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322792"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}