{"id":10006602,"date":"2022-09-26T16:34:32","date_gmt":"2022-09-26T08:34:32","guid":{"rendered":"https:\/\/ityet.com\/?p=10006602"},"modified":"2022-09-26T16:34:32","modified_gmt":"2022-09-26T08:34:32","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb86%ef%bc%9aspringboot-%e4%ba%8b%e5%8a%a1%e4%b8%8d%e5%9b%9e%e6%bb%9a%ef%bc%9f%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006602","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb86\uff1aSpringBoot \u4e8b\u52a1\u4e0d\u56de\u6eda\uff1f\u600e\u4e48\u89e3\u51b3\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u9020\u6210\u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\u7684\u573a\u666f\u6709\u5f88\u591a\uff0c\u6bd4\u5982\u4ee5\u4e0b\u8fd9\u4e9b\uff1a<\/p>\n<ol>\n<li>\u975e public \u4fee\u9970\u7684\u65b9\u6cd5\u4e2d\u7684\u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\uff1b<\/li>\n<li>\u5f53 @Transactional \u9047\u4e0a try\/catch \u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\uff1b<\/li>\n<li>\u8c03\u7528\u7c7b\u5185\u90e8\u7684 @Transactional \u65b9\u6cd5\u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\uff1b<\/li>\n<li>\u629b\u51fa\u68c0\u67e5\u5f02\u5e38\u65f6\u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\uff1b<\/li>\n<li>\u6570\u636e\u5e93\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u4e8b\u52a1\u4e5f\u4e0d\u4f1a\u81ea\u52a8\u56de\u6eda\u3002<\/li>\n<\/ol>\n<p>\u90a3\u4e48\u5bf9\u4e8e\u4e0a\u9762\u7684\u8fd9\u4e9b\u573a\u666f\uff0c\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u5462\uff1f\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u4e00\u6765\u770b\u3002<\/p>\n<h2>1.\u975e public \u65b9\u6cd5\u89e3\u51b3\u65b9\u6848<\/h2>\n<p><strong>\u975e public \u65b9\u6cd5\u4e2d\u4e8b\u52a1\u4e0d\u56de\u6eda\u7684\u76f4\u63a5\u539f\u56e0\u662f\uff0c\u5728\u975e public \u65b9\u6cd5\u4e0a\u6dfb\u52a0\u7684 @Transactional \u5173\u952e\u5b57\u662f\u65e0\u6548\u7684\uff0c\u4e5f\u5c31\u662f\u6b64\u65b9\u6cd5\u672c\u8eab\u662f\u4ee5\u975e\u4e8b\u52a1\u7684\u65b9\u5f0f\u8fd0\u884c\u7684\uff0c\u6240\u4ee5\u5b83\u5f53\u7136\u4e0d\u4f1a\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\u4e86\u3002<\/strong><\/p>\n<p><strong>\u56e0\u4e3a @Transactional \u4f7f\u7528\u7684\u662f Spring AOP \u5b9e\u73b0\u7684\uff0c\u800c Spring AOP \u662f\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u7684\uff0c\u800c @Transactional \u5728\u751f\u6210\u4ee3\u7406\u65f6\u4f1a\u5224\u65ad\uff0c\u5982\u679c\u65b9\u6cd5\u4e3a\u975e public \u4fee\u9970\u7684\u65b9\u6cd5\uff0c\u5219\u4e0d\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\uff0c\u8fd9\u6837\u4e5f\u5c31\u6ca1\u529e\u6cd5\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\u4e86<\/strong>\uff0c\u5b83\u7684\u90e8\u5206\u5b9e\u73b0\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">protected TransactionAttribute computeTransactionAttribute(Method method, Class&lt;?&gt; targetClass) {\n   \/\/ Don't allow no-public methods as required.\n   \/\/ \u975e public \u65b9\u6cd5\uff0c\u8bbe\u7f6e\u4e3a null\n   if (allowPublicMethodsOnly() &amp;&amp; !Modifier.isPublic(method.getModifiers())) {\n      return null;\n   }\n   \/\/ \u540e\u9762\u4ee3\u7801\u7701\u7565....\n }<\/code><\/pre>\n<p><strong>\u6b64\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u65b9\u6cd5\u7684\u6743\u9650\u4fee\u9970\u7b26\u6539\u4e3a public \u5373\u53ef\u3002<\/strong><\/p>\n<h2>2.try\/catch \u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u5f53\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4e86 try\/catch \u4ee3\u7801\u65f6\uff0c\u4e8b\u52a1\u4e0d\u4f1a\u81ea\u52a8\u56de\u6eda\uff0c\u8fd9\u662f\u56e0\u4e3a @Transactional \u6ce8\u89e3\u5728\u5176\u5b9e\u73b0\u65f6\uff0c\u9700\u8981\u611f\u77e5\u5230\u5f02\u5e38\u624d\u4f1a\u81ea\u52a8\u56de\u6eda\uff0c\u800c\u7528\u6237\u81ea\u884c\u5728\u4ee3\u7801\u4e2d\u52a0\u5165\u4e86 try\/catch \u4e4b\u540e\uff0c@Transactional \u5c31\u65e0\u6cd5\u611f\u77e5\u5230\u5f02\u5e38\u4e86\uff0c\u90a3\u4e48\u4e5f\u5c31\u4e0d\u80fd\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\u4e86\u3002<\/p>\n<p><strong>\u6b64\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u6709\u4e24\u79cd\uff1a\u4e00\u79cd\u662f\u5728 catch \u4e2d\u5c06\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u53bb\uff0c\u53e6\u4e00\u79cd\u662f\u4f7f\u7528\u4ee3\u7801\u624b\u52a8\u5c06\u4e8b\u52a1\u56de\u6eda\u3002<\/strong><\/p>\n<h3>\u89e3\u51b3\u65b9\u68481\uff1a\u5c06\u5f02\u5e38\u91cd\u65b0\u629b\u51fa<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543773\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h3>\u89e3\u51b3\u65b9\u68482\uff1a\u4f7f\u7528\u4ee3\u7801\u624b\u52a8\u56de\u6eda\u4e8b\u52a1<\/h3>\n<p>\u9664\u4e86\u89e3\u51b3\u65b9\u6848 1 \u8fd9\u79cd\u4e0d\u662f\u5f88\u53cb\u597d\u7684\u56de\u6eda\u4e8b\u52a1\u7684\u65b9\u5f0f\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u62e9\u66f4\u52a0\u53cb\u597d\u7684\uff0c\u4e0d\u62a5\u9519\uff0c\u4f46\u53ef\u4ee5\u56de\u6eda\u4e8b\u52a1\u7684\u65b9\u5f0f\uff0c\u5176\u6838\u5fc3\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543774\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>3.\u8c03\u7528\u5185\u90e8 @Transactional \u65b9\u6cd5\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u8c03\u7528\u7c7b\u5185\u90e8 @Transactional \u7684\u65b9\u6cd5\u4e0d\u81ea\u52a8\u56de\u6eda\u4e8b\u52a1\u7684\u539f\u56e0\u662f\uff0c@Transactional \u662f\u57fa\u4e8e Spring AOP \u5b9e\u73b0\u7684\uff0c\u800c Spring AOP \u53c8\u662f\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u7684\uff0c\u800c\u5f53\u8c03\u7528\u7c7b\u5185\u90e8\u7684\u65b9\u6cd5\u65f6\uff0c\u4e0d\u662f\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u5b8c\u6210\u7684\uff0c\u800c\u662f\u901a\u8fc7 this \u5bf9\u8c61\u5b9e\u73b0\u7684\uff0c\u8fd9\u6837\u5c31\u7ed5\u8fc7\u4e86\u4ee3\u7406\u5bf9\u8c61\uff0c\u4ece\u800c\u4e8b\u52a1\u5c31\u5931\u6548\u4e86\u3002<\/p>\n<p><strong>\u6b64\u65f6\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7ed9\u8c03\u7528\u7684\u65b9\u6cd5\u4e0a\u4e5f\u52a0\u4e0a @Transactional<\/strong>\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543775\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>4.\u68c0\u67e5\u5f02\u5e38\u7684\u4e8b\u52a1\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u6240\u8c13\u7684\u68c0\u67e5\u5f02\u5e38\uff08Checked Excetion\uff09\u6307\u7684\u662f\u7f16\u8bd1\u5668\u8981\u6c42\u5f00\u53d1\u8005\u5fc5\u987b\u5904\u7406\u7684\u5f02\u5e38\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543776\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/><strong>\u68c0\u67e5\u5f02\u5e38\u4e0d\u56de\u6eda\u4e8b\u52a1\u7684\u539f\u56e0\u662f\u56e0\u4e3a\uff0c@Transactional \u9ed8\u8ba4\u53ea\u56de\u6eda\u8fd0\u884c\u65f6\u5f02\u5e38 RuntimeException \u548c Error\uff0c\u800c\u5bf9\u4e8e\u68c0\u67e5\u5f02\u5e38\u9ed8\u8ba4\u662f\u4e0d\u56de\u6eda\u7684\u3002<\/strong><\/p>\n<p><strong>\u6b64\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7ed9 @Transactional \u6ce8\u89e3\u4e0a\uff0c\u6dfb\u52a0 rollbackFor \u53c2\u6570\u5e76\u8bbe\u7f6e Exception.class \u503c\u5373\u53ef<\/strong>\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543777\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>5.\u6570\u636e\u5e93\u4e0d\u652f\u6301\u4e8b\u52a1\u7684\u89e3\u51b3\u65b9\u6848<\/h2>\n<p><strong>\u5f53\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u4e86 @Transactional\uff0c\u76f8\u5f53\u4e8e\u7ed9\u8c03\u7528\u7684\u6570\u636e\u5e93\u53d1\u9001\u4e86\uff1a\u5f00\u59cb\u4e8b\u52a1\u3001\u63d0\u4ea4\u4e8b\u52a1\u3001\u56de\u6eda\u4e8b\u52a1\u7684\u6307\u4ee4\uff0c\u4f46\u662f\u5982\u679c\u6570\u636e\u5e93\u672c\u8eab\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u6bd4\u5982 MySQL \u4e2d\u8bbe\u7f6e\u4e86\u4f7f\u7528 MyISAM \u5f15\u64ce\uff0c\u56e0\u4e3a\u5b83\u672c\u8eab\u662f\u4e0d\u652f\u6301\u4e8b\u52a1\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5373\u4f7f\u5728\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u4e86 @Transactional \u6ce8\u89e3\uff0c\u90a3\u4e48\u4f9d\u7136\u4e0d\u4f1a\u6709\u4e8b\u52a1\u7684\u884c\u4e3a\uff0c\u4e5f\u5c31\u4e0d\u4f1a\u6267\u884c\u4e8b\u52a1\u7684\u81ea\u52a8\u56de\u6eda\u4e86\u3002<\/strong><\/p>\n<p><strong>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8bbe\u7f6e MySQL \u7684\u5f15\u64ce\u4e3a InnoDB \u5c31\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\u4e86\uff0c\u56e0\u4e3a InnoDB \u662f\u652f\u6301\u4e8b\u52a1\u7684<\/strong>\uff0c\u5f53\u7136 MySQL 5.1 \u4e4b\u540e\u7684\u9ed8\u8ba4\u5f15\u64ce\u5c31\u662f InnoDB\uff0c\u5f15\u64ce\u7684\u8bbe\u7f6e\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<br \/>\u5728\u65b0\u5efa\u8868\u65f6\u8bbe\u7f6e\u6570\u636e\u5e93\u5f15\u64ce\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543778\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u5728\u4fee\u6539\u8868\u65f6\u8bbe\u7f6e\u6570\u636e\u5e93\u5f15\u64ce\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042543779\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<blockquote><p>PS\uff1a\u4e5f\u5c31\u662f\u6570\u636e\u5e93\u7684\u5f15\u64ce\u662f\u548c\u8868\u76f4\u63a5\u76f8\u5173\u7684\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6b63\u786e\u7684\u8bbe\u7f6e\u5f15\u64ce\u4e4b\u540e\uff0c\u4e8b\u52a1\u5c31\u53ef\u4ee5\u6b63\u5e38\u7684\u6267\u884c\u4e86\u3002<\/p><\/blockquote>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u6211\u4eec\u4ecb\u7ecd\u4e86 5 \u79cd\u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\u7684\u573a\u666f\u548c\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f00\u53d1\u8005\u5e94\u8be5\u6839\u636e\u81ea\u5df1\u7684\u5b9e\u9645\u60c5\u51b5\uff0c\u9009\u62e9\u5408\u9002\u81ea\u5df1\u89e3\u51b3\u65b9\u6848\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=mClqmjMA3CrxIVArlx69WQ%3D%3D.lzfm%2FXFqIz5ufnlI2guNK9%2Bxnn7dTYdZ83zavvYh4niennFWsx4tn6Y9PyMyIE06\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042543771\">https:\/\/segmentfault.com\/a\/1190000042543771<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u9020\u6210\u4e8b\u52a1\u4e0d\u81ea\u52a8\u56de\u6eda\u7684\u573a\u666f\u6709\u5f88\u591a\uff0c\u6bd4\u5982\u4ee5\u4e0b\u8fd9\u4e9b\uff1a \u975e public \u4fee\u9970\u7684\u65b9\u6cd5\u4e2d [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006602","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006602"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006602\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}