{"id":322,"date":"2024-07-13T17:13:09","date_gmt":"2024-07-13T09:13:09","guid":{"rendered":"https:\/\/forelink.top\/?p=322"},"modified":"2024-07-13T17:13:10","modified_gmt":"2024-07-13T09:13:10","slug":"3-%e4%ba%8b%e5%8a%a1%e7%9b%b8%e5%85%b3%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/forelink.top\/index.php\/2024\/07\/13\/3-%e4%ba%8b%e5%8a%a1%e7%9b%b8%e5%85%b3%e9%85%8d%e7%bd%ae\/","title":{"rendered":"3.\u4e8b\u52a1\u76f8\u5173\u914d\u7f6e"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"\u4e8b\u52a1\u76f8\u5173\u914d\u7f6e\u91cd\u8981-\u624b\u52a8\u8bbe\u7f6e\u56de\u6eda\">\u4e8b\u52a1\u76f8\u5173\u914d\u7f6e(\u91cd\u8981 \u624b\u52a8\u8bbe\u7f6e\u56de\u6eda)<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5f02\u5e38\u4e0e\u4e8b\u52a1\u56de\u6eda\uff1a\">\u5f02\u5e38\u4e0e\u4e8b\u52a1\u56de\u6eda\uff1a<\/h2>\n\n\n\n<p>\u200b    \u4e8b\u52a1\u6ce8\u89e3\u6709\u4e0d\u540c\u7684\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\uff0c\u91cd\u70b9\u4ecb\u7ecd <strong>rollbackFor<\/strong> \u8bbe\u7f6e\u4e8b\u52a1\u56de\u6eda\u5f02\u5e38\uff0c\u5728\u63a5\u53d7\u5230\u67d0\u4e9b\u5f02\u5e38\u65f6\u624d\u56de\u6eda\u3002<\/p>\n\n\n\n<p>\u4f8b\uff1a\u82e5\u5728\u670d\u52a1\u5c42\u63a5\u53e3\u7684@Transactional\u6ce8\u89e3\u540e\u52a0\u5165\u5982\u4e0b\u5185\u5bb9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Transactional(rollbackFor( &#91;\u5f02\u5e38\u540d].class ))\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class AccountServiceImpl implements AccountService{\n    @Autowired\n    private AccountDao accountDao;\n    \n    public void transfer(String out, String in, String money){\n        accountDao.outMoney(out,money);\n        if(true){throw new IOException();} \/\/\u5f02\u5e38\u53d1\u751f\u70b9\n        accountDao.inMoney(in,money);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u6b64\u65b9\u6cd5\u540e\uff0c\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u5f02\u5e38\uff0c\u4f46outMoney\u5df2\u7ecf\u6267\u884c\uff0c\u4e8b\u52a1\u662f\u6ca1\u6709\u751f\u6548\u7684<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u9047\u5230\u5982\u4e0b\u4e24\u79cd\u5f02\u5e38\uff0c\u624d\u4f1a\u4f5c\u4e8b\u52a1\u56de\u6eda\uff1a<\/p>\n\n\n\n<p>Error\u7cfb\uff08\u5982\u5185\u5b58\u6ea2\u51fa\uff09\n\u8fd0\u884c\u65f6\u5f02\u5e38\uff08\u5982NullPointException\uff09<\/p>\n\n\n\n<p>\u4e0d\u5c5e\u4e8e\u4e0a\u9762\u7684\u5f02\u5e38\u4e0d\u4f1a\u4f5c <strong>\u4e8b\u52a1\u56de\u6eda<\/strong> IOException\u5c31\u4e0d\u662f\u4e0a\u9762\u7684\u5f02\u5e38\uff0c\u6240\u4ee5\u4e0d\u4f1a\u4f5c<strong>\u4e8b\u52a1\u56de\u6eda<\/strong>\u3002\n\u9700\u8981\u7279\u6b8a\u8bbe\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u9047\u5230IO\u5f02\u5e38\uff0c\u624d\u4f1a\u56de\u6eda\n@Transactional(rollbackFor =\uff08IOException.class)\npublic void transfer(String out, String in, Double money)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u63a5\u7684\u662f\u5b57\u7b26\u4e32\uff08\u5f02\u5e38\u7684\u540d\u5b57\uff09\nrollbackForClassName(\"\")\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6848\u4f8b\uff1a\u8f6c\u8d26\u4e1a\u52a1\u8ffd\u52a0\u65e5\u5fd7\">\u6848\u4f8b\uff1a\u8f6c\u8d26\u4e1a\u52a1\u8ffd\u52a0\u65e5\u5fd7<\/h2>\n\n\n\n<p>\u200b    \u9700\u6c42\uff1a\u5b9e\u73b0\u4efb\u610f\u4e24\u4e2a\u8d26\u6237\u95f4\u8f6c\u8d26\u64cd\u4f5c\uff0c\u5e76\u5bf9\u6bcf\u6b21\u8f6c\u8d26\u64cd\u4f5c\u5728\u6570\u636e\u5e93\u8fdb\u884c\u7559\u75d5\n\u200b    \u9700\u6c42\u5fae\u7f29\uff1aA\u51cf\u94b1\uff0cB\u52a0\u94b1\uff0c\u6570\u636e\u5e93\u8bb0\u5f55\u65e5\u5fd7<\/p>\n\n\n\n<p>\u200b    \u5206\u6790\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    1. \u57fa\u4e8e\u8f6c\u8d26\u64cd\u4f5c\u6848\u4f8b\u6dfb\u52a0\u65e5\u5fd7\u6a21\u5757\uff0c\u5b9e\u73b0\u6570\u636e\u5e93\u4e2d\u8bb0\u5f55\u65e5\u5fd7\n      2. \u4e1a\u52a1\u5c42\u8f6c\u8d26\u64cd\u4f5c (transfer)\uff0c\u8c03\u7528\u51cf\u94b1\uff0c\u52a0\u94b1\u4e0e\u8bb0\u5f55\u65e5\u5fd7\u529f\u80fd\u3002\n<\/code><\/pre>\n\n\n\n<p>\u200b    \u6548\u679c\u9884\u671f\uff1a\u65e0\u8bba\u8f6c\u8d26\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff0c\u5747\u8fdb\u884c\u8f6c\u8d26\u64cd\u4f5c\u7684\u65e5\u5fd7\u7559\u75d5\u3002<\/p>\n\n\n\n<p>\u200b    \u8c03\u7528\u65e5\u5fd7\uff1a<\/p>\n\n\n\n<p>\u200b    <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/AccountServiceImpl\n@Service\npublic class AccountServiceImpl implements AccountService{\n    @Autowired\n    private AccountDao accountDao;\n    \/\/\u52a0\u5165\u65e5\u5fd7\u5bf9\u8c61\n    @Autowired\n    private LogService logService;\n    \n    public void transfer(String out, String in, String money){\n        \/\/\u5982\u679c\u76f4\u63a5\u8bb0\u5f55\u65e5\u5fd7\uff0c\u6709\u53ef\u80fd\u4f1a\u6ca1\u529e\u6cd5\u6267\u884c\u65e5\u5fd7\u529f\u80fd\uff0c\u6240\u4ee5\u9700\u8981\u7528try&amp;finally\u4fdd\u8bc1\u65e5\u5fd7\u529f\u80fd\u4ee3\u7801\u4e00\u5b9a\u88ab\u6267\u884c\n        try{\n            accountDao.outMoney(out,money);\n            int i = 1\/0; \/\/\u52a0\u4e0a\u8fd9\u4e00\u53e5\u540e\uff0c\u65e5\u5fd7\u5c31\u4e0d\u80fd\u8bb0\u5f55\u4e86\u3002\n           accountDao.inMoney(in,money);\n        }finally{\n            logService.log(out, in, money);\/\/LogService\u4e5f\u88ab\u7ed1\u5b9a\u4e86\u4e8b\u52a1\uff0c\u6240\u4ee5\u9700\u8981\u66f4\u6362\u4e00\u4e2a\u5168\u65b0\u7684\u4e8b\u52a1\u3002\n        }\n        \n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u77e5\u8bc6\u70b9\uff1a\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\">\u77e5\u8bc6\u70b9\uff1a\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a<\/h2>\n\n\n\n<p>\u662f\u5426\u8ba9\u4e8b\u52a1\u52a0\u5165\u5176\u4ed6\u4e8b\u52a1\uff0c\u80fd\u5426\u8ba9\u5176\u4ed6\u4e8b\u52a1\u52a0\u5165\uff0c\u5c31\u662f\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface LogService{\n    \/\/ \u8bbe\u7f6e\u4e86\u65b0\u7684\u5ba3\u4f20\/\u4f20\u64ad\u884c\u4e3a\u3002\n    @Transactional(propagation = Propagation.REQUIRES_NEW)\n    void log(String out, String in, Double money);\n}\n<\/code><\/pre>\n\n\n\n<p>\u6240\u6709\u7684\u4e8b\u52a1\u4f20\u64ad\u5c5e\u6027\u53ef\u4ee5\u81ea\u5df1\u67e5\u627e\u4e86\u89e3\u3002<\/p>\n\n\n\n<p>\u6709<\/p>\n\n\n\n<p>REQUIRED \u5982\u679c\u6709\u4e8b\u52a1\u7ba1\u7406\u5458 <strong>\u52a0\u5165<\/strong>\uff0c\u6ca1\u6709 <strong>\u65b0\u5efa<\/strong><\/p>\n\n\n\n<p>REQUIRES_NEW \u4e0d\u7ba1\u6709\u6ca1\u6709\u90fd <strong>\u65b0\u5efa<\/strong>\uff0c\u6210\u4e3a\u4e8b\u52a1\u7ba1\u7406\u5458<\/p>\n\n\n\n<p>SUPPORTS \u5982\u679c\u6709\u4e8b\u52a1\u7ba1\u7406\u5458 <strong>\u52a0\u5165<\/strong>\uff0c\u6ca1\u6709 <strong>\u4e0d\u52a0\u5165<\/strong><\/p>\n\n\n\n<p>NOT_SUPPORTED \u5982\u679c\u6709\u4e8b\u52a1\u7ba1\u7406\u5458 <strong>\u4e0d\u52a0\u5165<\/strong>\uff0c\u6ca1\u6709 <strong>\u4e0d\u52a0\u5165<\/strong><\/p>\n\n\n\n<p>MANDATORY \u5982\u679c\u6709\u4e8b\u52a1\u7ba1\u7406\u5458 <strong>\u52a0\u5165<\/strong>\uff0c\u6ca1\u6709 <strong>\u62a5\u9519<\/strong><\/p>\n\n\n\n<p>NEVER \u5982\u679c\u6709\u4e8b\u52a1\u7ba1\u7406\u5458 <strong>\u62a5\u9519<\/strong>\uff0c\u6ca1\u6709 <strong>\u4e0d\u52a0\u5165<\/strong><\/p>\n\n\n\n<p>NESTED \u8bbe\u7f6e\u56de\u6eda\u70b9\uff08buhui\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8b\u52a1\u76f8\u5173\u914d\u7f6e(\u91cd\u8981 \u624b\u52a8\u8bbe\u7f6e\u56de\u6eda) \u5f02\u5e38\u4e0e\u4e8b\u52a1\u56de\u6eda\uff1a \u200b \u4e8b\u52a1\u6ce8\u89e3\u6709\u4e0d\u540c\u7684\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\uff0c\u91cd\u70b9\u4ecb\u7ecd rollba [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[28,17],"tags":[],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-spring","category-17"],"_links":{"self":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/comments?post=322"}],"version-history":[{"count":1,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"predecessor-version":[{"id":323,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/322\/revisions\/323"}],"wp:attachment":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}