{"id":10020645,"date":"2025-02-05T14:51:53","date_gmt":"2025-02-05T06:51:53","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020645"},"modified":"2025-02-05T14:51:53","modified_gmt":"2025-02-05T06:51:53","slug":"transaction-rolled-back-because-marked-as-rollback-only%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020645","title":{"rendered":"Transaction rolled back because marked as rollback-only\u95ee\u9898\u89e3\u51b3"},"content":{"rendered":"<h2>1\u3001\u80cc\u666f<\/h2>\n<p>\u5728\u6211\u4eec\u7684\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u5b58\u5728\u5728\u4e00\u4e2aService\u5c42\u4e2d\u8c03\u7528\u53e6\u5916\u4e00\u4e2aService\u5c42\u7684\u65b9\u6cd5\u3002<code>\u6bd4\u5982<\/code>\uff1a\u6211\u4eec\u6709\u4e00\u4e2aTaskService\uff0c\u91cc\u9762\u6709\u4e00\u4e2aexecTask\u65b9\u6cd5\uff0c\u4e14\u8fd9\u4e2a\u65b9\u6cd5\u5b58\u5728\u4e8b\u52a1\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6267\u884c\u5b8c\u4e4b\u540e\uff0c\u9700\u8981\u8c03\u7528LogService\u7684insertLog\u65b9\u6cd5\u8bb0\u5f55\u4e00\u6761\u65e5\u5fd7\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4e0a\u4e5f\u6709\u4e8b\u52a1\uff0c\u4e0d\u7ba1\u65e5\u5fd7\u8bb0\u5f55\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0c\u90fd\u4e0d\u80fd\u5f71\u54cdexecTask\u65b9\u6cd5\u7684\u6267\u884c\u3002\u56e0\u6b64\u6211\u4eec\u5f88\u5bb9\u6613\u5199\u51fa\u5982\u4e0b\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"java\">@Transactional\npublic void execTaskV1(){\n    log.info(&quot;\u5f00\u59cb\u6267\u884c\u4efb\u52a1&quot;);\n    try {\n        logService.insertLogV1();\n    } catch (Exception e) {\n        log.error(&quot;\u6dfb\u52a0\u65e5\u5fd7\u51fa\u73b0\u9519\u8bef&quot;);\n    }\n    log.info(&quot;\u5b8c\u6210\u4efb\u52a1\u6267\u884c&quot;);\n}<\/code><\/pre>\n<p><code>\u601d\u8003\uff1a<\/code> \u4e0a\u65b9\u7684\u4ee3\u7801\uff0c\u5982\u679c<code>insertLogV1<\/code>\u8dd1\u51fa\u4e86\u5f02\u5e38\uff0c<code>execTaskV1<\/code>\u65b9\u6cd5\u7684\u4e8b\u52a1\u53ef\u4ee5\u6b63\u5e38\u63d0\u4ea4\u5417\uff1f<\/p>\n<h2>2\u3001\u5f02\u5e38\u662f\u5982\u4f55\u5b9e\u73b0\u51fa\u73b0\u7684<\/h2>\n<h3>1\u3001\u4e86\u89e3Spring\u4e8b\u52a1\u7684\u4f20\u64ad\u5c5e\u6027<\/h3>\n<table>\n<thead>\n<tr>\n<th align=\"left\"><strong>\u4f20\u64ad\u884c\u4e3a<\/strong><\/th>\n<th align=\"left\"><strong>\u63cf\u8ff0<\/strong><\/th>\n<th align=\"left\"><strong>\u5e94\u7528\u573a\u666f<\/strong><\/th>\n<th align=\"left\"><strong>\u884c\u4e3a\u7279\u70b9<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><code>Propagation.REQUIRED<\/code><\/td>\n<td align=\"left\">\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/td>\n<td align=\"left\">\u5927\u591a\u6570\u573a\u666f\uff0c\u5982\u591a\u4e2a\u65b9\u6cd5\u9700\u8981\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u5b8c\u6210\u3002<\/td>\n<td align=\"left\">&#8211; \u5982\u679c\u5f53\u524d\u4e8b\u52a1\u5b58\u5728\uff0c\u65b9\u6cd5\u6267\u884c\u5728\u5f53\u524d\u4e8b\u52a1\u4e0a\u4e0b\u6587\u4e2d\u3002<br \/>&#8211; \u5982\u679c\u5f53\u524d\u4e8b\u52a1\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u4e8b\u52a1\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>Propagation.SUPPORTS<\/code><\/td>\n<td align=\"left\">\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\u3002<\/td>\n<td align=\"left\">\u5bf9\u4e8b\u52a1\u652f\u6301\u6ca1\u6709\u5f3a\u5236\u8981\u6c42\u7684\u573a\u666f\uff0c\u5982\u53ea\u8bfb\u67e5\u8be2\u3002<\/td>\n<td align=\"left\">&#8211; \u5982\u679c\u5f53\u524d\u4e8b\u52a1\u5b58\u5728\uff0c\u65b9\u6cd5\u6267\u884c\u5728\u5f53\u524d\u4e8b\u52a1\u4e0a\u4e0b\u6587\u4e2d\u3002<br \/>&#8211; \u5982\u679c\u5f53\u524d\u4e8b\u52a1\u4e0d\u5b58\u5728\uff0c\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>Propagation.MANDATORY<\/code><\/td>\n<td align=\"left\">\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/td>\n<td align=\"left\">\u5fc5\u987b\u5728\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u4e8b\u52a1\u4e2d\u6267\u884c\u7684\u573a\u666f\u3002<\/td>\n<td align=\"left\">&#8211; \u5fc5\u987b\u5728\u5df2\u6709\u4e8b\u52a1\u4e2d\u6267\u884c\uff0c\u5426\u5219\u629b\u51fa <code>IllegalTransactionStateException<\/code>\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>Propagation.REQUIRES_NEW<\/code><\/td>\n<td align=\"left\">\u6bcf\u6b21\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\u90fd\u4f1a\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002\u5f53\u524d\u4e8b\u52a1\uff08\u5982\u679c\u6709\uff09\u4f1a\u88ab\u6302\u8d77\u3002<\/td>\n<td align=\"left\">\u9700\u8981\u72ec\u7acb\u4e8b\u52a1\u7684\u573a\u666f\uff0c\u5982\u65e5\u5fd7\u8bb0\u5f55\u6216\u72ec\u7acb\u7684\u4e1a\u52a1\u64cd\u4f5c\u3002<\/td>\n<td align=\"left\">&#8211; \u603b\u662f\u521b\u5efa\u65b0\u4e8b\u52a1\u3002<br \/>&#8211; \u5f53\u524d\u4e8b\u52a1\uff08\u5982\u679c\u6709\uff09\u4f1a\u88ab\u6302\u8d77\uff0c\u76f4\u5230\u65b0\u4e8b\u52a1\u5b8c\u6210\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>Propagation.NOT_SUPPORTED<\/code><\/td>\n<td align=\"left\">\u603b\u662f\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\uff0c\u5e76\u4e14\u6682\u505c\u5f53\u524d\u4e8b\u52a1\uff08\u5982\u679c\u6709\uff09\u3002<\/td>\n<td align=\"left\">\u4e0d\u9700\u8981\u4e8b\u52a1\u7684\u573a\u666f\uff0c\u5982\u7b80\u5355\u7684\u67e5\u8be2\u64cd\u4f5c\u3002<\/td>\n<td align=\"left\">&#8211; \u603b\u662f\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\u3002<br \/>&#8211; \u6682\u505c\u5f53\u524d\u4e8b\u52a1\uff08\u5982\u679c\u6709\uff09\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>Propagation.NEVER<\/code><\/td>\n<td align=\"left\">\u603b\u662f\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/td>\n<td align=\"left\">\u4e25\u683c\u7981\u6b62\u4e8b\u52a1\u7684\u573a\u666f\uff0c\u5982\u67d0\u4e9b\u975e\u4e8b\u52a1\u6027\u64cd\u4f5c\u3002<\/td>\n<td align=\"left\">&#8211; \u5fc5\u987b\u5728\u975e\u4e8b\u52a1\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\uff0c\u5426\u5219\u629b\u51fa <code>TransactionException<\/code>\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><code>Propagation.NESTED<\/code><\/td>\n<td align=\"left\">\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u5728\u5d4c\u5957\u4e8b\u52a1\u5185\u6267\u884c\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/td>\n<td align=\"left\">\u9700\u8981\u5d4c\u5957\u4e8b\u52a1\u7684\u573a\u666f\uff0c\u5982\u590d\u6742\u7684\u4e1a\u52a1\u6d41\u7a0b\u4e2d\u9700\u8981\u72ec\u7acb\u7684\u56de\u6eda\u70b9\u3002<\/td>\n<td align=\"left\">&#8211; \u5982\u679c\u5f53\u524d\u4e8b\u52a1\u5b58\u5728\uff0c\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u4e8b\u52a1\uff08\u4f9d\u8d56\u4e8e\u6570\u636e\u5e93\u652f\u6301\uff09\u3002<br \/>&#8211; \u5982\u679c\u5f53\u524d\u4e8b\u52a1\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u4e8b\u52a1\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2\u3001\u6a21\u62df\u5f02\u5e38\u51fa\u73b0<\/h3>\n<p><code>Transaction rolled back because it has been marked as rollback-only<\/code> \u8fd9\u4e2a\u5f02\u5e38\u5728\u4e0a\u8ff0\u7684\u6848\u4f8b\u4e2d\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5462\uff1f<br \/><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"621\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdhtDZ\" alt=\"\" title=\"\"><\/p>\n<p>\u4ece\u4e0a\u56fe\u4e2d\u53ef\u77e5\uff0c\u51fa\u73b0\u4e86<code>Transaction rolled back because it has been marked as rollback-only<\/code>\u8fd9\u4e2a\u5f02\u5e38\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5f02\u5e38\u662f\u5982\u4f55\u51fa\u73b0\u7684\u5462\uff1f<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e2a\u662f\u548cSpring\u4e8b\u52a1\u7684<code>\u4f20\u64ad\u5c5e\u6027Propagation<\/code>\u6709\u5173\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b<code>@Transaction<\/code>\u7684\u4f20\u64ad\u5c5e\u6027\u662f<code>Propagation.REQUIRED<\/code>, \u5373<code>\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/code> \u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e8b\u52a1\u7684\u9694\u79bb\u7ea7\u522b\u90fd\u662f<code>Propagation.REQUIRED<\/code>\uff0c\u5373\u662f\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u56e0\u6b64<code>insertLogV1<\/code>\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u540e\uff0c\u867d\u7136\u4e0a\u5c42\u6355\u83b7\u5230\u4e86\uff0c\u4f46\u5176\u5b9e\u8fd9\u4e2a\u65f6\u5019\u8fd9\u4e2a\u4e8b\u52a1\u5df2\u7ecf\u88ab\u6807\u8bb0\u6210<code>\u56de\u6eda<\/code>\u72b6\u6001\u4e86\uff0c\u56e0\u6b64\u4e8b\u52a1\u65e0\u6cd5\u63d0\u4ea4\u6210\u529f\u3002<\/p>\n<p><code>\u5982\u4f55\u89e3\u51b3\uff1a<\/code>  \u53ea\u9700\u8981\u4fee\u6539<code>insertLogV1<\/code>\u4e8b\u52a1\u7684\u4f20\u64ad\u5c5e\u6027\u4e3a<code>Propagation.REQUIRES_NEW<\/code>\u5373\u53ef\u3002<\/p>\n<h2>3\u3001\u5b8c\u6574\u4ee3\u7801<\/h2>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=rrQAouiv0zSY7tVX29wiYw%3D%3D.YHxcJhDd9eA03BQb3cm9A2vk9IIwe810YQDua9%2B%2B97Ma8XslY3V94%2BGntWufTtG2Xbiy66O9rJOZYVFl6m1sP%2FFG8EFmTPjO6mHoezwZQQZj0KFdXXYgvmgpHfeiOQBt\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u5b8c\u6574\u4ee3\u7801-https:\/\/gitee.com\/huan1993\/spring-cloud-parent\/tree\/master\/springboot\/springboot-transaction-v1<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046072202\">https:\/\/segmentfault.com\/a\/1190000046072202<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u80cc\u666f \u5728\u6211\u4eec\u7684\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u5b58\u5728\u5728\u4e00\u4e2aService\u5c42\u4e2d\u8c03\u7528\u53e6\u5916\u4e00\u4e2aService\u5c42\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\uff1a\u6211\u4eec [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020645","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020645","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"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020645"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020645\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}