{"id":5216,"date":"2021-06-10T22:14:29","date_gmt":"2021-06-10T14:14:29","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5216"},"modified":"2021-06-10T22:14:29","modified_gmt":"2021-06-10T14:14:29","slug":"%e5%88%86%e4%ba%ab%e7%be%a4%e5%8f%8b%e4%bd%bf%e7%94%a8synchronized-transactional-%e9%80%a0%e6%88%90%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5216","title":{"rendered":"\u5206\u4eab\u7fa4\u53cb\u4f7f\u7528synchronized + @Transactional \u9020\u6210\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u60f3\u4e0d\u5230\uff0csynchronized + @Transactional \u9020\u6210\u7684\u91cd\u5927\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6628\u5929\uff0c\u5fae\u4fe1\u7fa4\u91cc\u4e00\u4f4d\u5c0f\u4f19\u4f34\u9047\u5230\u4e86\u4e00\u4e2a synchronized + @Transactional \u4e8b\u52a1\u9020\u6210\u7aef\u5348\u793c\u54c1\u5206\u914d\u4e0d\u5747\u7684\u95ee\u9898\uff0c\u6700\u7ec8\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002\u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u5206\u4eab\u4e00\u4e0b\u8fd9\u4e2a\u95ee\u9898\u662f\u5982\u4f55\u4ea7\u751f\u7684\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ee\u9898\u4ea7\u751f\u7684\u73b0\u8c61\u662f\uff0c\u8fd9\u4f4d\u5c0f\u4f19\u4f34\u5199\u7684\u516c\u53f8\u5185\u90e8\u7528\u7684\u7aef\u5348\u9886\u793c\u54c1\u7a0b\u5e8f\u53d1\u751f\u4e86\u201c\u9519\u9886\u201d\u95ee\u9898\u3002\u5177\u4f53\u662f\uff0c\u4e09\u53ea\u677e\u9f20 500 \u7bb1\uff0c\u7cbd\u5b50 500 \u7bb1\uff0c\u7c97\u7cae 67 \u7bb1\u3002\u516c\u53f8\u4e00\u5171 1067 \u4eba\uff0c\u901a\u8fc7\u7a0b\u5e8f\u53bb\u62a2\u8fd9\u4e9b\u793c\u54c1\u3002\u7ed3\u679c\u53d1\u73b0\u9886\u5bfc\u4e09\u53ea\u677e\u9f20\u7684\u4eba\u5374\u8d85\u8fc7 500 \u4eba\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/06\/synchronized-transactional.png\" alt=\"\" \/><figcaption>synchronized + @Transactional\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u7f51\u53cb\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u6211\u901a\u8fc7\u8131\u654f\u540e\uff0c\u62bd\u51fa\u6838\u5fc3\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Transactional(rollbackFor = ServiceException.class)\npublic void saveGiftTicket(GiftTicket giftTicket) {\n    synchronized (this.class) {\n        \/\/ \u68c0\u67e5\u5bf9\u5e94\u793c\u54c1\u662f\u5426\u6709\u5269\u4f59\n        preCheckGiftTicket(giftTicket);\n  \n        \/\/ \u6263\u51cf\u793c\u54c1\n        modifyGiftTicketAmount(giftTicket);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801\u5b58\u5728\u7684\u95ee\u9898\u6211\u7b80\u5355\u63cf\u8ff0\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5bf9\u5e94\u5206\u7c7b\u7684\u793c\u54c1\u5269\u4f59\u4e3a 1 \u65f6\uff0c\u7ebf\u7a0b A \u62ff\u5230\u9501\u8fdb\u5165\u540c\u6b65\u4ee3\u7801\u5757\uff0c\u6263\u51cf\u793c\u54c1\uff0c\u7ebf\u7a0b B \u7b49\u5f85\u9501\uff1b\u5f53\u7ebf\u7a0b A \u6267\u884c\u5b8c\u540c\u6b65\u4ee3\u7801\u5757\u65f6\uff0c\u7ebf\u7a0b B \u62ff\u5230\u9501\uff0c\u6267\u884c\u540c\u6b65\u4ee3\u7801\u5757\uff0c\u68c0\u67e5\u5230\u5269\u4f59\u7684\u793c\u54c1\u4ecd\u4e3a 1 (\u6b64\u65f6\uff0c\u5269\u4f59\u793c\u54c1\u5e94\u8be5\u4e3a 0\uff0cpreCheckGiftTicket \u65b9\u6cd5\u5e94\u8be5\u629b\u51fa\u5f02\u5e38)\uff0c\u4e8e\u662f\u4e5f\u8fdb\u884c\u4e86\u6263\u51cf\u793c\u54c1\uff1b\u6700\u7ec8\u5bfc\u81f4\u8d85\u8fc7\u4e86 500 \u4eba\u9886\u53d6\u5230\u4e86\u4e09\u53ea\u677e\u9f20\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f88\u591a\u4eba\u4e5f\u90fd\u5199\u8fc7\u8fd9\u79cd\u4ee3\u7801\uff0c\u4e5f\u5305\u62ec\u6211\u7684\u540c\u4e8b\u3002\u53ea\u4e0d\u8fc7\u4ed6\u7684 synchronized \u6362\u4e86\u4e00\u4e2a\u4f4d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Transactional(rollbackFor = ServiceException.class)\npublic synchronized void saveGiftTicket(GiftTicket giftTicket) {\n    \/\/ \u68c0\u67e5\u5bf9\u5e94\u793c\u54c1\u662f\u5426\u6709\u5269\u4f59\n    preCheckGiftTicket(giftTicket);\n\n    \/\/ \u6263\u51cf\u793c\u54c1\n    modifyGiftTicketAmount(giftTicket);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u4ee3\u7801\u90fd\u662f\u6709\u95ee\u9898\u7684\uff0c\u4e00\u822c\u8fd8\u4e0d\u5bb9\u6613\u53d1\u73b0\u3002\u56e0\u4e3a\u5e76\u53d1\u5e76\u4e0d\u662f\u5f88\u5927\uff0c\u5e76\u6ca1\u6709\u4e00\u822c\u7535\u5546\u9879\u76ee\u7684\u5e76\u53d1\u9ad8\uff0c\u672c\u8eab\u7528\u6237\u91cf\u4e5f\u4e0d\u5927\uff0c\u8fd8\u662f\u4e00\u4e2a\u5355\u4f53\u5e94\u7528\uff0c\u65e2\u6709\u9501\uff0c\u53c8\u6709\u4e8b\u52a1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u521d\u8fd9\u4f4d\u7f51\u53cb\u9047\u5230\u95ee\u9898\u540e\uff0c\u6211\u770b\u4e86\u4ed6\u7684\u4ee3\u7801\uff0c\u5927\u6982\u5c31\u731c\u51fa\u95ee\u9898\u6240\u5728\u4e86\u3002\u6211\u8ba9\u4ed6\u5199\u4e86\u5355\u5143\u6d4b\u8bd5\uff0c\u4f7f\u7528 CountDownLatch \u6765\u91cd\u73b0\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CountDownLatch countDownLatch = new CountDownLatch(1);\nfor (int i = 0; i &lt; 888; i++) {\n    new Thread(() -&gt; {\n        try {\n            countDownLatch.await();\n            String parter = \"\u3010\" + Thread.currentThread().getName() + \"\u3011\";\n            System.out.println(parter + \"\u5f00\u59cb\u6267\u884c\u2026\u2026\");\n            GiftTicket giftTicket = new GiftTicket();\n            saveGiftTicket(giftTicket);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }).start();\n}\n\nThread.sleep(2000);\n\/\/ \u62a2\u793c\u54c1\ncountDownLatch.countDown();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u73b0\u95ee\u9898\u540e\uff0c\u4ed6\u95ee\u6211\u4e3a\u4ec0\u4e48\u9501\u6ca1\u6709\u7528\uff0c\u4e3a\u4ec0\u4e48 Transactional \u4e8b\u52a1\u4e0d\u8d77\u4f5c\u7528\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u5728\u7fa4\u91cc\u544a\u8bc9\u4ed6\uff0c\u6839\u672c\u539f\u56e0\u662f\uff1a@Transactional \u4e8b\u52a1\u65f6 Spring \u901a\u8fc7 AOP \u5b9e\u73b0\u7684\uff0c\u5f53\u6211\u4eec\u8c03\u7528 saveGiftTicket \u65b9\u6cd5\u540e\uff0c\u5728 saveGiftTicket \u65b9\u6cd5\u6267\u884c\u4e4b\u524d Spring \u5c31\u4f1a\u5f00\u542f\u4e8b\u52a1\uff0c\u4e4b\u540e\u4f1a\u6709\u63d0\u4ea4\u4e8b\u52a1\u903b\u8f91\u3002\u800c synchronized \u4ee3\u7801\u5757\u6267\u884c\u662f\u5728\u4e8b\u52a1\u4e4b\u5185\u6267\u884c\u7684\uff0c\u5f53 synchronized \u4ee3\u7801\u5757\u6267\u884c\u5b8c\u540e\uff0c\u4e8b\u52a1\u8fd8\u672a\u63d0\u4ea4\uff0c\u5176\u4ed6\u7ebf\u7a0b\u8fdb\u5165 synchronized \u4ee3\u7801\u5757\u540e\uff0c\u8bfb\u53d6\u7684\u6570\u636e\u4e0d\u662f\u6700\u65b0\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u5c31\u662f\u8981\u628a @Transactional \u548c synchronized \u7406\u89e3\u6e05\u695a\u3002\u8981\u4e48\u4e0d\u52a0\u9501\uff0c\u8981\u4e48\u8ba9\u9501\u7684\u8303\u56f4\u6bd4\u4e8b\u52a1\u5927\u3002\u5f53\u7136\u5982\u679c\u80fd\u7528\u65e0\u9501\u4ee3\u7801\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u65f6\u66f4\u597d\u7684\u3002\u6bd4\u5982\uff0c\u6211\u4e0b\u9762\u8fd9\u4e2a SQL \u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u4e1a\u52a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update\u00a0xttblog_gift\u00a0set\u00a0amount\u00a0=\u00a0amount\u00a0-\u00a01\u00a0where\u00a0gift_type\u00a0=\u00a0'\u4e09\u53ea\u677e\u9f20'\u00a0AND\u00a0amount\u00a0&gt;\u00a00;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\u4f7f\u7528\u60b2\u89c2\u9501 for update\uff0c\u4e50\u89c2\u9501\u7248\u672c\u63a7\u5236\uff0c\u5206\u5e03\u5f0f\u9501\uff0c\u961f\u5217\u7b49\u3002\u4f46\u6211\u5efa\u8bae\u8fd9\u91cc\u4e0d\u8981\u5b9e\u73b0\u7684\u592a\u590d\u6742\uff0c\u56e0\u4e3a\u4f60\u8fd9\u4e2a\u9879\u76ee\u672c\u8eab\u7684\u4ef7\u503c\u70b9\u4e0d\u5728\u8fd9\u91cc\uff0c\u4e0d\u8981\u56e0\u4e3a\u4e00\u4e2a\u5c0f\u95ee\u9898\u5c31\u5e26\u6765\u5de8\u5927\u7684\u6210\u672c\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5216\">\u5206\u4eab\u7fa4\u53cb\u4f7f\u7528synchronized + @Transactional \u9020\u6210\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u60f3\u4e0d\u5230\uff0csynchronized + @Transactional \u9020\u6210\u7684\u91cd\u5927\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898 \u6628\u5929\uff0c\u5fae\u4fe1\u7fa4\u91cc\u4e00\u4f4d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[839,1758,426,838,531],"class_list":["post-5216","post","type-post","status-publish","format-standard","hentry","category-java","tag-transactional","tag-abstractqueuedsynchronizer","tag-synchronized","tag-838","tag-531"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5216"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5216\/revisions"}],"predecessor-version":[{"id":5217,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5216\/revisions\/5217"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}