{"id":16823,"date":"2025-01-26T22:38:58","date_gmt":"2025-01-26T14:38:58","guid":{"rendered":"https:\/\/92it.top\/?p=16823"},"modified":"2025-01-26T22:43:45","modified_gmt":"2025-01-26T14:43:45","slug":"%e4%b8%80%e7%af%87%e6%96%87%e7%ab%a0%e5%bd%bb%e5%ba%95%e6%90%9e%e6%87%82-spring-%e7%9a%84%e4%ba%8b%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16823","title":{"rendered":"\u4e00\u7bc7\u6587\u7ae0\u5f7b\u5e95\u641e\u61c2 Spring \u7684\u4e8b\u52a1"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzkwMzQzNzQwNQ==&amp;mid=2247484415&amp;idx=1&amp;sn=b45226c86bcde996b9a09c0fdd47bddd&amp;chksm=c177c74866bc5c336721d597bb06cbc5295b4420f4396bba8a3cd71c7b00d6799ea33328708c&amp;mpshare=1&amp;scene=1&amp;srcid=0126lEUeRknU22lUt3o4IzRU&amp;sharer_shareinfo=17fb64481d66b98d1658894dcf94a0e0&amp;sharer_shareinfo_first=23448235f4cdc166f7606475cb0de51f&amp;exportkey=n_ChQIAhIQ0UwuPnZtPWKtWveLdqJ1rBL0AQIE97dBBAEAAAAAABB2BoZxh9cAAAAOpnltbLcz9gKNyK89dVj03YcTcwI9owK1lBiWcZsZqd420BYbx8a2tNQhHWuSlK%2FYR58%2B4KYLoSikHx6hJcvM%2F6HgksYczwFNtgXYvaemuhrW6pIgU5PRORPifTrdlqzkq0YBnNoqp4Ji7NvG2auLPvdNqTf1gJWr1eFZFbCaLVnaKDO74NZ2Lu%2FN4HGqdfJoO920IvXtPBK5exe%2FO0mC1bttfwsVQ7jWpvw3fjdAUAmqzYzdzsJUf3PCtduKTNrgLrphh7vKtW826hJi72k4ruq3PXSnFOEOgnZywOI%3D&amp;acctmode=0&amp;pass_ticket=wG%2Fz56gikmxGDJLmZbzNRaBBhl03hXaSc6u9QEq2%2BXGJKog%2F6I4%2FsH2V5GROMwa5&amp;wx_header=0#rd\">\u4e00\u7bc7\u6587\u7ae0\u5f7b\u5e95\u641e\u61c2 Spring \u7684\u4e8b\u52a1<\/a><\/p>\n\n\n\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4f5c\u4e3a\u6280\u672f\u4eba\uff0c\u5f88\u5fcc\u8bb3\u5bf9\u67d0\u4e2a\u95ee\u9898\u6d45\u5c1d\u8f84\u6b62\uff0c\u4e00\u77e5\u534a\u89e3\uff0c\u7279\u522b\u662f\u7ecf\u5e38\u4f7f\u7528\u7684\u6280\u672f\u3002Spring \u4f5c\u4e3a\u7ecf\u4e45\u4e0d\u8870\u7684 Java \u6846\u67b6\uff0c\u81ea\u7136\u5c11\u4e0d\u4e86\u5bf9\u4e8b\u52a1\u7684\u652f\u6301\u3002\u90a3\u4e48\uff0c\u5173\u4e8e Spring \u7684\u4e8b\u52a1\uff0c\u4f60\u4e86\u89e3\u591a\u5c11\u5462\uff1f\u5982\u679c\u53ea\u662f\u77e5\u9053\u5728\u65b9\u6cd5\u4e0a\u52a0\u4e00\u4e2a @Transactional \u6ce8\u89e3\u5c31\u53ef\u4ee5\u652f\u6301\u4e8b\u52a1\uff0c\u6216\u8005\u8bf4\u53ea\u662f\u7b80\u5355\u5730\u77e5\u9053 Spring \u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u80cc\u540e\u539f\u7406\u662f AOP\uff0c\u6050\u6015\u8fd8\u4e0d\u591f\u3002\u4eca\u5929\u6211\u4eec\u5c31\u4e00\u8d77\u6df1\u5165\u4e86\u89e3\u4e0b Spring \u7684\u4e8b\u52a1\u3002<\/p>\n\n\n\n<p><strong>\u4e00\u3001Srping \u4e2d\u5982\u4f55\u5f00\u542f\u4e8b\u52a1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Spring<\/strong>&nbsp;\u4e2d\u5f00\u542f<strong>\u4e8b\u52a1<\/strong>\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f&nbsp;<strong>\u58f0\u660e\u5f0f\u4e8b\u52a1<\/strong>\uff0c\u53e6\u4e00\u79cd\u662f<strong>\u7f16\u7a0b\u5f0f\u4e8b\u52a1<\/strong><\/p>\n\n\n\n<p>&nbsp;<strong>1. \u58f0\u660e\u5f0f\u4e8b\u52a1<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)\npublic void test() {\n    \/\/ \u4e1a\u52a1\u903b\u8f91\n}<\/pre>\n\n\n\n<p><strong>2. \u7f16\u7a0b\u5f0f\u4e8b\u52a1<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528&nbsp;<code>TransactionTemplate<\/code>\u7b49\u7c7b\u548c API \u624b\u52a8\u7ba1\u7406\u4e8b\u52a1\uff0c\u63a7\u5236\u4e8b\u52a1\u7684\u65b0\u5efa\u3001\u63d0\u4ea4\u3001\u56de\u6eda\u7b49\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<ul>\n<li><strong>TransactionTemplate<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Resource\nprivate TransactionTemplate transactionTemplate;\n\n@Transactional\npublic void test() {\n    transactionTemplate.executeWithoutResult(status -> {\n        \/\/ \u4e1a\u52a1\u903b\u8f91\n        if (something not right) {\n            \/\/ \u56de\u6eda\n            status.setRollbackOnly();\n        }\n    });\n}<\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>TransactionManager<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Resource\nprivate PlatformTransactionManager transactionManager;\n\n@Transactional\npublicvoidtest() {\n    \/\/ \u5b9a\u4e49\u4e8b\u52a1\n    TransactionDefinitiontransactionDefinition=newDefaultTransactionDefinition();\n    \/\/ \u83b7\u53d6\u4e8b\u52a1\u72b6\u6001\n    TransactionStatustransactionStatus= transactionManager.getTransaction(transactionDefinition);\n\n    try {\n        \/\/ \u4e1a\u52a1\u64cd\u4f5c\n        \/\/ \u63d0\u4ea4\u4e8b\u52a1\n        transactionManager.commit(transactionStatus);\n    } catch (Exception e) {\n        \/\/ \u5f02\u5e38\u56de\u6eda\u4e8b\u52a1\n        transactionManager.rollback(transactionStatus);\n        throw e;\n    }\n}<\/pre>\n\n\n\n<p><strong>\u4e8c\u3001\u4e3a\u4ec0\u4e48\u4e0d\u5efa\u8bae\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0a\u9762\u7684\u4e24\u79cd\u4e8b\u52a1\u5b9e\u73b0\u65b9\u5f0f\uff0c\u660e\u663e<code>\u58f0\u660e\u5f0f\u4e8b\u52a1<\/code>&nbsp;<code>\u66f4\u7b80\u5355<\/code>\uff0c<code>\u66f4\u7b80\u6d01<\/code>\uff0c\u5bf9\u4e1a\u52a1\u4e5f<code>\u6ca1\u6709\u4fb5\u5165\u6027<\/code>\u3002\u4f46\u4e3a\u4ec0\u4e48\u8bf4<code>\u4e0d\u5efa\u8bae<\/code>\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1\u5462\uff1f<\/p>\n\n\n\n<p>\u5f53\u7136\u8fd9\u4e2a\u4e5f\u4e0d\u662f\u7b14\u8005\u5efa\u8bae\u7684\uff0c\u800c\u662f\u963f\u91cc\u5df4\u5df4\u300aJava\u5f00\u53d1\u624b\u518c v1.5.0 \u534e\u5c71\u7248\u300b&nbsp;\u5efa\u8bae\u7684\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"140\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-1024x140.png\" alt=\"\" class=\"wp-image-16824\" style=\"width:536px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-1024x140.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-300x41.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-768x105.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-1536x210.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-830x114.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-230x32.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-350x48.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8-480x66.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/image-8.png 1796w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u4e3a\u4ec0\u4e48\u5b83\u8981\u8fd9\u4e48\u5efa\u8bae\u5462? \u7b54\u6848\u80af\u5b9a\u662f<code>\u58f0\u660e\u5f0f\u4e8b\u52a1<\/code>&nbsp;\u5b58\u5728\u7740\u8ba9\u4eba\u96be\u4ee5\u5ffd\u7565\u7684\u7f3a\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u4e8b\u52a1\u7c92\u5ea6<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u6700\u5bb9\u6613\u60f3\u5230\u7684\u5e94\u8be5\u662f<strong>\u4e8b\u52a1\u7684\u7c92\u5ea6<\/strong>\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a<code>\u58f0\u660e\u5f0f\u4e8b\u52a1<\/code>\u80fd\u63a7\u5236\u7684<code>\u6700\u5c0f\u7c92\u5ea6<\/code>\u662f<code>\u65b9\u6cd5<\/code>\uff0c\u6574\u4e2a\u65b9\u6cd5\u90fd\u5305\u542b\u5728\u4e8b\u52a1\u5185\uff0c\u4f46\u6709\u65f6\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\uff0c\u6211\u4eec\u5e0c\u671b\u4e8b\u52a1\u7c92\u5ea6\u80fd\u66f4\u5c0f\u4e00\u70b9\uff0c\u6bd4\u5982\u8bf4\u53ea\u6709\u67d0\u51e0\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u624d\u9700\u8981\u4e8b\u52a1\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\uff0c\u5982\u679c\u6211\u4eec\u5c06&nbsp;<code>RPC\u8c03\u7528<\/code>&nbsp;\u653e\u5165\u4e8b\u52a1\u65b9\u6cd5\u4e2d\uff0c\u5982\u679c\u4e8b\u52a1<code>\u63d0\u4ea4\u5931\u8d25<\/code>\uff0c\u6570\u636e\u5e93\u6700\u540e\u56de\u6eda\uff0c\u4f46\u662f<code>RPC\u8c03\u7528<\/code>&nbsp;\u65e0\u6cd5\u56de\u6eda\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4e86\u4e25\u91cd\u7684<code>\u6570\u636e\u4e0d\u4e00\u81f4<\/code>&nbsp;\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u53c8\u6bd4\u5982\uff0c\u6211\u4eec\u5728\u4e8b\u52a1\u65b9\u6cd5\u4e2d\u52a0\u5165\u592a\u591a<code>\u8017\u65f6\u64cd\u4f5c<\/code>\uff0c\u4f8b\u5982\u6587\u4ef6\u64cd\u4f5c\uff0c\u66f4\u65b0\u7f13\u5b58\uff0c\u53d1\u9001\u6d88\u606f\u7b49\uff0c\u5c31\u4f1a\u8ba9\u4e8b\u52a1\u53d8\u6210\u4e00\u4e2a<code>\u957f\u4e8b\u52a1<\/code>\uff0c<code>\u6570\u636e\u5e93\u8fde\u63a5<\/code>\u4f1a\u957f\u65f6\u95f4\u5730\u88ab\u5360\u7528\uff0c\u5c31\u53ef\u80fd\u5bfc\u81f4<code>\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u8017\u5c3d<\/code>\uff0c\u4e5f\u66f4\u5bb9\u6613\u4ea7\u751f<code>\u6b7b\u9501<\/code>\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u66f4\u4e3a\u5173\u952e\u7684\u662f\uff0c\u7531\u4e8e\u8fd9\u79cd\u4e8b\u52a1\u6ce8\u89e3\u5f88\u5bb9\u6613\u88ab\u4eba\u5ffd\u7565\uff0c\u5e76\u4e14\u8fd8\u5b58\u5728<code>\u65b9\u6cd5\u5d4c\u5957<\/code>\uff0c\u6240\u4ee5\u4e0a\u9762\u7684\u95ee\u9898\u5f88\u5bb9\u6613<code>\u201c\u9632\u4e0d\u614e\u9632\u201d<\/code>\u3002\u76f8\u53cd\uff0c\u4f7f\u7528<code>\u7f16\u7a0b\u5f0f\u4e8b\u52a1<\/code>\uff0c\u80fd\u8ba9\u5f00\u53d1\u8005\u5f88\u6e05\u695a\u5730\u660e\u786e<code>\u4e8b\u52a1\u7684\u8fb9\u754c<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u4e8b\u52a1\u5931\u6548<\/strong><\/p>\n\n\n\n<p>\u53e6\u5916\u4e00\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u95ee\u9898\u5c31\u662f\uff0c\u7531\u4e8e\u5f00\u53d1\u8005\u7684<code>\u758f\u5ffd<\/code>\u6216\u8005\u6280\u672f\u6c34\u5e73\u7684\u539f\u56e0\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u58f0\u660e\u5f0f\u4e8b\u52a1<code>\u5931\u6548<\/code>\u3002<\/p>\n\n\n\n<p>\u5728\u8bb2&nbsp;<strong>\u4e8b\u52a1\u5931\u6548<\/strong>&nbsp;\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u7b80\u5355\u4e86\u89e3\u4e00\u4e0b&nbsp;<code>Spring \u58f0\u660e\u5f0f\u4e8b\u52a1<\/code>\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff0c\u8fd9\u91cc\u540e\u9762\u4f1a\u6df1\u5165\u63a2\u7a76\uff0c\u8fd9\u91cc\u5148\u7b80\u5355\u6982\u8ff0\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Spring \u58f0\u660e\u5f0f\u4e8b\u52a1\u901a\u8fc7AOP\u5b9e\u73b0\uff0c\u57fa\u4e8e@Transactional\u6ce8\u89e3\u548c\u4e8b\u52a1\u7ba1\u7406\u5668\u3002Spring\u4f7f\u7528\u4ee3\u7406\u6a21\u5f0f\uff08JDK\u52a8\u6001\u4ee3\u7406\u6216CGLIB\uff09\u62e6\u622a\u5e26\u6709@Transactional \u7684\u65b9\u6cd5\u8c03\u7528\uff0c\u5728\u65b9\u6cd5\u6267\u884c\u524d\u83b7\u53d6\u4e8b\u52a1\u914d\u7f6e\uff0c\u542f\u52a8\u4e8b\u52a1\uff1b\u82e5\u65b9\u6cd5\u6210\u529f\u6267\u884c\uff0c\u63d0\u4ea4\u4e8b\u52a1\uff1b\u82e5\u53d1\u751f\u5f02\u5e38\uff0c\u6839\u636e\u914d\u7f6e\u56de\u6eda\u4e8b\u52a1\u3002\u8fd9\u4e9b\u6d41\u7a0b\u7531 TransactionInterceptor \u8c03\u7528PlatformTransactionManager \u5b8c\u6210\uff0c\u4ece\u800c\u5b9e\u73b0\u81ea\u52a8\u7ba1\u7406\u4e8b\u52a1\u8fb9\u754c\u3002<\/pre>\n\n\n\n<p>\u65e2\u7136&nbsp;<code>Spring\u58f0\u660e\u5f0f\u4e8b\u52a1<\/code>&nbsp;\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e&nbsp;<code>AOP<\/code>\uff0c\u90a3\u4e48\u6309\u7167\u9053\u7406\u8bf4\uff0c\u6240\u6709\u80fd\u5230\u5bfc\u81f4&nbsp;<code>AOP \u5931\u6548<\/code>&nbsp;\u7684\u60c5\u51b5\u4e5f\u90fd\u4f1a\u5bfc\u81f4<code>\u58f0\u660e\u5f0f\u4e8b\u52a1\u4e8b\u52a1\u5b9e\u6548<\/code>\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u53c8\u662f\u54ea\u4e9b\u60c5\u51b5\u4f1a\u5bfc\u81f4&nbsp;<code>AOP \u5931\u6548<\/code>\u5462\uff1f<\/p>\n\n\n\n<p>\u8fd9\u5c31\u8981\u7ee7\u7eed\u8ffd\u95ee\uff0c<code>AOP<\/code>&nbsp;\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p><code>Spring<\/code>&nbsp;\u4e2d<code>AOP<\/code>&nbsp;\u7684\u539f\u7406\u5c31\u662f&nbsp;<code>JDK\u52a8\u6001\u4ee3\u7406<\/code>\u548c&nbsp;<code>CGLIB \u4ee3\u7406<\/code>\uff0c\u6709\u63a5\u53e3\u7684\u4f7f\u7528&nbsp;<code>JDK\u52a8\u6001\u4ee3\u7406<\/code>\uff0c\u6ca1\u63a5\u53e3\u7684\u4f7f\u7528&nbsp;<code>CGLIB \u4ee3\u7406<\/code>\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u4ee5\u4e0b\u60c5\u51b5<code>\u4e0d\u8d70\u4ee3\u7406<\/code>\u6216\u8005<code>\u65e0\u6cd5\u4ee3\u7406<\/code>\u7684\u60c5\u51b5\u4f1a\u5bfc\u81f4&nbsp;<code>AOP \u5931\u6548<\/code>\uff0c\u4ece\u800c\u5bfc\u81f4&nbsp;<code>\u4e8b\u52a1\u5931\u6548<\/code>\u3002<\/p>\n\n\n\n<ul>\n<li><strong>\u5185\u90e8\u8c03\u7528<\/strong>\n<ul>\n<li>\u2022&nbsp;<code>\u7c7b\u5185\u90e8\u65b9\u6cd5\u8c03\u7528<\/code>\u76f4\u63a5\u8c03\u7528<code>\u539f\u59cb\u5bf9\u8c61<\/code>\uff0c\u6839\u672c\u4e0d\u6d89\u53ca<code>\u4ee3\u7406\u5bf9\u8c61<\/code>\uff0c\u6240\u4ee5\u4e8b\u52a1\u5fc5\u7136\u5931\u6548\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u81ea\u5df1\u6765\u89c4\u907f\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u2022&nbsp;<strong>\u975e public \u65b9\u6cd5\u3001final \u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5<\/strong>\n<ul>\n<li>\u2022 \u8fd9\u662f\u4e00\u4e2a\u5e38\u88ab\u5ffd\u7565\u7684\u70b9\uff0c\u52a8\u6001\u4ee3\u7406\u65e0\u6cd5\u4ee3\u7406<code>\u975epublic\u65b9\u6cd5<\/code>\u3001<code>final\u65b9\u6cd5<\/code>\u3001<code>\u9759\u6001\u65b9\u6cd5<\/code>\uff0c\u6240\u4ee5\u8fd9\u4e9b\u65b9\u6cd5\u4e0a\u7684\u4e8b\u52a1\u4e5f\u4f1a\u5931\u6548\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u8bb2\u5230\u7684\u90a3\u4e9b<code>AOP<\/code>\u7684\u539f\u56e0\uff0c\u8fd8\u6709\u4e00\u4e9b\u8ddf Spring&nbsp;<code>\u4e8b\u52a1\u5c5e\u6027\u914d\u7f6e<\/code>\u76f8\u5173\u7684\u3002<\/p>\n\n\n\n<ul>\n<li>\u2022&nbsp;<strong>rollbackFor \u8bbe\u7f6e\u9519\u8bef<\/strong>\n<ul>\n<li>\u2022 \u8fd9\u4e2a\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u5c31\u662f\u53d1\u751f\u5f02\u5e38\u548c\u8bbe\u7f6e\u5f02\u5e38\u4e0d\u5339\u914d\uff0c\u5bfc\u81f4\u4e8b\u52a1\u672a\u56de\u6eda\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u2022&nbsp;<strong>propagation \u8bbe\u7f6e\u9519\u8bef<\/strong>\n<ul>\n<li>\u2022 \u8fd9\u4e2a\u7b49\u4e0b\u6df1\u5165\u63a2\u8ba8<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u5176\u5b9e\u4e0a\u9762\u8bb2\u7684\u90fd\u662f\u53ef\u80fd\u56e0\u4e3a\u5f00\u53d1\u4eba\u5458\u4eba\u4e3a\u758f\u5ffd\u5bfc\u81f4\u7684\u4e8b\u52a1\u95ee\u9898\uff0c\u4f46\u662f\u6b63\u662f\u56e0\u4e3a\u8c01\u4e5f\u4e0d\u80fd\u4fdd\u8bc1\u5f00\u53d1\u4eba\u5458\u4e0d\u4f1a\u72af\u9519\uff0c\u6c34\u5e73\u6781\u9ad8\uff0c\u6240\u4ee5\u53ea\u80fd\u901a\u8fc7\u4e00\u4e9b\u5f00\u53d1\u89c4\u8303\u6765\u5c3d\u91cf\u89c4\u907f\u8fd9\u4e9b\u95ee\u9898\uff0c\u4f7f\u7528\u7f16\u7a0b\u5f0f\u4e8b\u52a1\uff0c\u5c31\u80fd\u591f\u5927\u5927\u51cf\u5c11\u4e0a\u8ff0\u95ee\u9898\u7684\u53d1\u751f\u3002\u5f53\u7136\uff0c\u8fd9\u4e5f\u4e0d\u662f\u8bf4\u58f0\u660e\u5f0f\u4e8b\u52a1\u5b8c\u5168\u4e0d\u80fd\u7528\uff0c\u53ea\u662f\u8bf4\u4e0d\u80fd\u6ee5\u7528\u3002<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u6df1\u5165\u63a2\u7a76\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5176\u5b9e\u521a\u624d\u8fd8\u6f0f\u8bb2\u4e86\u4e00\u4e2a\u53ef\u80fd\u5bfc\u81f4<code>\u4e8b\u52a1\u5931\u6548<\/code>\u7684\u539f\u56e0\uff1a<code>\u591a\u7ebf\u7a0b<\/code>\uff0c<code>\u8de8\u7ebf\u7a0b\u7684\u4e8b\u52a1\u7ba1\u7406<\/code>&nbsp;Spring \u7684<code>\u58f0\u660e\u5f0f\u4e8b\u52a1<\/code>\u4e5f\u662f\u4e0d\u652f\u6301\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u8981\u5f00\u59cb\u4ece\u58f0\u660e\u5f0f\u4e8b\u52a1\u7684\u539f\u7406\uff0c\u4e5f\u5c31\u662f\u5b83\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5f00\u59cb\u8bb2\u8d77\u4e86\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u4e0a\u6587\u4e2d\u7b80\u5355\u63d0\u5230\u4e86&nbsp;<code>AOP<\/code>&nbsp;\u548c&nbsp;<code>\u4e8b\u52a1\u7ba1\u7406\u5668 - TransactionManager<\/code>\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4ece\u6e90\u7801\u83b7\u53d6\u66f4\u591a\u7ec6\u8282\u3002<\/p>\n\n\n\n<p><strong>1\u3001\u5165\u53e3\u7c7b\uff1aTransactionAspectSupport<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public abstractclassTransactionAspectSupport {\n    \n    \/\/ \u6838\u5fc3\u65b9\u6cd5: \u5728\u4e8b\u52a1\u73af\u5883\u4e2d\u6267\u884c\u76ee\u6807\u65b9\u6cd5\n    protected Object invokeWithinTransaction(Method method, Class&lt;?> targetClass, InvocationCallback invocation)throws Throwable {\n        \/\/ 1. \u83b7\u53d6\u4e8b\u52a1\u5c5e\u6027\n        TransactionAttributeSourcetas= getTransactionAttributeSource();\n        TransactionAttributetxAttr= (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);\n        \n        \/\/ 2. \u786e\u5b9a\u4e8b\u52a1\u7ba1\u7406\u5668\n        TransactionManagertm= determineTransactionManager(txAttr, targetClass);\n        \n        \/\/ 3. \u5904\u7406\u54cd\u5e94\u5f0f\u4e8b\u52a1\n        if (this.reactiveAdapterRegistry != null &amp;&amp; tm instanceof ReactiveTransactionManager rtm) {\n            \/\/ \u5904\u7406\u54cd\u5e94\u5f0f\u4e8b\u52a1\u903b\u8f91...\n            return handleReactiveTransaction(\/*...*\/);\n        }\n        \n        \/\/ 4. \u5904\u7406\u666e\u901a\u4e8b\u52a1 \u4f7f\u7528 PlatformTransactionManager\n        PlatformTransactionManagerptm= asPlatformTransactionManager(tm);\n        StringjoinpointIdentification= methodIdentification(method, targetClass, txAttr);\n        \n        \/\/ 5. \u6267\u884c\u4e8b\u52a1\u5904\u7406\n        if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager)) {\n            \/\/ \u6807\u51c6\u4e8b\u52a1\u5904\u7406\u6d41\u7a0b:\n            TransactionInfotxInfo= createTransactionIfNecessary(ptm, txAttr, joinpointIdentification);\n            ObjectretVal=null;\n            try {\n                \/\/ \u6267\u884c\u76ee\u6807\u65b9\u6cd5\n                retVal = invocation.proceedWithInvocation();\n            }\n            catch (Throwable ex) {\n                \/\/ \u5f02\u5e38\u56de\u6eda\n                completeTransactionAfterThrowing(txInfo, ex);\n                throw ex;\n            }\n            finally {\n                if (txInfo != null) {\n                    txInfo.restoreThreadLocalStatus();\n                }\n            }\n            \/\/ \u63d0\u4ea4\u4e8b\u52a1\n            commitTransactionAfterReturning(txInfo);\n            return retVal;\n        }\n        else {\n            \/\/ \u56de\u8c03\u5f0f\u4e8b\u52a1\u5904\u7406...\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u6211\u4eec\u53ef\u4ee5\u5f88\u6e05\u6670\u5730\u770b\u5230\u6574\u4f53\u4e8b\u52a1\u5904\u7406\u6d41\u7a0b\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u4ece&nbsp;<code>\"5. \u6267\u884c\u4e8b\u52a1\u5904\u7406\"<\/code>&nbsp;\u4e2d\u7684\u5f88\u91cd\u8981\u7684\u4e00\u4e2a\u7c7b&nbsp;<code>TransactionInfo<\/code>&nbsp;\u8bf4\u8d77\uff0c\u770b\u4e0b\u5b83\u91cc\u9762\u5b58\u7684\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">protected staticfinalclassTransactionInfo {\n    \/\/ \u4e8b\u52a1\u7ba1\u7406\u5668\n    privatefinal PlatformTransactionManager transactionManager;\n    \/\/ \u4e8b\u52a1\u5c5e\u6027(\u4f20\u64ad\u884c\u4e3a\u3001\u9694\u79bb\u7ea7\u522b\u7b49\u914d\u7f6e)\n    privatefinal TransactionAttribute transactionAttribute;\n    \/\/ \u65b9\u6cd5\u6807\u8bc6(\u7528\u4e8e\u65e5\u5fd7)\n    privatefinal String joinpointIdentification;\n    \/\/ \u5f53\u524d\u4e8b\u52a1\u72b6\u6001\n    private TransactionStatus transactionStatus;\n    \/\/ \u7236\u4e8b\u52a1\u4fe1\u606f\n    private TransactionInfo oldTransactionInfo;\n}<\/pre>\n\n\n\n<p><code>TransactionInfo<\/code>&nbsp;\u5b58\u50a8\u7684\u5c31\u662f<code>\u4e8b\u52a1\u4fe1\u606f<\/code>\uff0c\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\uff1a<\/p>\n\n\n\n<ul>\n<li>\u2022&nbsp;<strong>\u4e8b\u52a1\u4e0a\u4e0b\u6587\u4fe1\u606f\u7684\u5c01\u88c5<\/strong>\n<ul>\n<li>\u2022 \u4fdd\u5b58\u5f53\u524d\u4e8b\u52a1\u7684\u5b8c\u6574\u4e0a\u4e0b\u6587\u4fe1\u606f<\/li>\n\n\n\n<li>\u2022 \u5305\u62ec<code>\u4e8b\u52a1\u7ba1\u7406\u5668<\/code>\u3001<code>\u4e8b\u52a1\u5c5e\u6027\u914d\u7f6e<\/code>\u3001<code>\u4e8b\u52a1\u72b6\u6001<\/code>\u7b49<\/li>\n\n\n\n<li>\u2022 \u4f5c\u4e3a\u4e8b\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4fe1\u606f\u8f7d\u4f53<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u4e8b\u52a1\u4fe1\u606f&nbsp;<code>TransactionInfo<\/code>&nbsp;\u4fdd\u5b58\u5728\u54ea\u91cc\u5462\uff1f\u8fd9\u662f\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6765\u68b3\u7406\u4e0b&nbsp;<code>TransactionInfo<\/code>&nbsp;\u7684\u9700\u6c42\uff1a<\/p>\n\n\n\n<ol>\n<li>1. \u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684<code>\u4e8b\u52a1\u4e0a\u4e0b\u6587<\/code>\uff0c\u4e8b\u52a1\u8ddf\u5f53\u524d\u8c03\u7528\u7ebf\u7a0b\u606f\u606f\u76f8\u5173\u3002<\/li>\n\n\n\n<li>2. \u4e8b\u52a1\u53ef\u4ee5\u8bbe\u7f6e\u5404\u79cd<code>\u4f20\u64ad\u5c5e\u6027<\/code>\uff08REQUIRED\u3001REQUIRES_NEW\u7b49\uff09\uff0c\u4e5f\u5c31\u662f\u540c\u4e00\u7ebf\u7a0b\u5185\u7684\u65b9\u6cd5\u8c03\u7528\u5e94\u8be5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bbf\u95ee\u5f53\u524d\u4e8b\u52a1\u4fe1\u606f\uff0c\u4ece\u800c\u51b3\u5b9a\u662f\u5426\u65b0\u5efa\u4e8b\u52a1\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e5f\u5c31\u662f&nbsp;<code>\u4e8b\u52a1\u53ef\u4ee5\u5d4c\u5957\u4f20\u64ad<\/code>\u3002<\/li>\n\n\n\n<li>3. \u53e6\u5916\uff0c\u6211\u4eec\u4e5f\u4e0d\u5e0c\u671b<code>\u5728\u65b9\u6cd5\u8c03\u7528\u94fe\u4e2d\u663e\u5f0f\u4f20\u9012\u4e8b\u52a1\u4fe1\u606f<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u9700\u6c42\uff0c\u6211\u4eec\u81ea\u7136\u800c\u7136\u5730\u60f3\u5230\u4e86Java\u4e2d\u7684&nbsp;<code>ThreadLocal<\/code>\uff0c\u5b83\u7528\u6765\u4fdd\u5b58\u4e8b\u52a1\u4fe1\u606f\u518d\u5408\u9002\u4e0d\u8fc7\uff0c\u4e8b\u5b9e\u4e5f\u7684\u786e\u5982\u6b64\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u770b\u4e0b&nbsp;<code>prepareTransactionInfo() -&gt; bindToThread()<\/code>&nbsp;\uff0c\u5176\u5b9e\u5c31\u662f\u5c06\u5f53\u524d\u4e8b\u52a1\u4fe1\u606f&nbsp;<code>TransactionInfo<\/code>&nbsp;\u4fdd\u5b58\u5230&nbsp;<code>ThreadLocal<\/code>&nbsp;\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u7236\u4e8b\u52a1\u4fe1\u606f\nprivate TransactionInfo oldTransactionInfo;\n\n\/\/ \u4fdd\u5b58\u5f53\u524d\u8c03\u7528\u7ebf\u7a0b\u7684\u4e8b\u52a1\u4fe1\u606f\nprivatestaticfinal ThreadLocal&lt;TransactionInfo> transactionInfoHolder = \n      newNamedThreadLocal&lt;>(\"Current aspect-driven transaction\");\n\n\/\/ \u5c06\u5f53\u524d\u4e8b\u52a1\uff0c\u653e\u5165\u4e8b\u52a1\u4e0a\u4e0b\u6587 ThreadLocal\nprivatevoidbindToThread() {\n    \/\/ \u6682\u5b58\u7236\u4e8b\u52a1\u5230 oldTransactionInfo\n    this.oldTransactionInfo = transactionInfoHolder.get();\n    \/\/ \u4fdd\u5b58\u5f53\u524d\u4e8b\u52a1\u5230 ThreadLocal\n    transactionInfoHolder.set(this);\n}<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u4e5f\u5c31\u660e\u767d\u4e86\u4e3a\u4ec0\u4e48\u4e00\u5f00\u59cb\u8bf4\uff0c<code>\u591a\u7ebf\u7a0b\u4e5f\u4f1a\u5bfc\u81f4\u58f0\u660e\u5f0f\u4e8b\u52a1\u5931\u6548<\/code>\uff0c\u56e0\u4e3a&nbsp;<code>ThreadLocal<\/code>&nbsp;\u4fdd\u5b58\u7684\u5185\u5bb9\u4e0d\u80fd<code>\u8de8\u7ebf\u7a0b<\/code>\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u6211\u4eec\u7ee7\u7eed\u56de\u5230&nbsp;<code>TransactionInfo<\/code>\uff0c\u5b83\u91cc\u9762\u5176\u5b83\u51e0\u4e2a\u5c5e\u6027\u975e\u5e38\u597d\u7406\u89e3\uff0c\u4f46\u662f\u5176\u4e2d&nbsp;<code>oldTransactionInfo<\/code>&nbsp;\u8ba9\u4eba\u89c9\u5f97\u6709\u70b9\u5947\u602a\uff0c\u5b83\u5230\u5e95\u6709\u4ec0\u4e48\u7528\uff1f\u5176\u5b9e\u5b83<code>\u975e\u5e38\u91cd\u8981<\/code>\uff0c\u5177\u6709\u7279\u6b8a\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u770b\u4e0b\u9762\u8fd9\u4e2a\u573a\u666f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Transactional\npublicvoidouter() {\n    \/\/ \u521b\u5efa TransactionInfo1\n    inner();  \/\/ \u8c03\u7528\u5185\u5c42\u4e8b\u52a1\u65b9\u6cd5\n    \/\/ \u6062\u590d\u5230 TransactionInfo1\n}\n\n@Transactional\npublicvoidinner() {\n    \/\/ \u521b\u5efa TransactionInfo2\n    \/\/ oldTransactionInfo \u6307\u5411 TransactionInfo1\n    \/\/ \u65b9\u6cd5\u7ed3\u675f\u65f6\u6062\u590d\u5230 TransactionInfo1\n}<\/pre>\n\n\n\n<p>\u770b\u5230\u8fd9\u91cc\u662f\u4e0d\u662f\u604d\u7136\u5927\u609f\uff0c\u5728\u8fd9\u79cd\u65b9\u6cd5\u5d4c\u5957\u4e2d\uff0c\u8981\u600e\u4e48\u5904\u7406<code>\u7236\u4e8b\u52a1<\/code>\u548c<code>\u5b50\u4e8b\u52a1<\/code>&nbsp;\uff1f\u7b54\u6848\u5c31\u5728&nbsp;<code>oldTransactionInfo<\/code>&nbsp;\u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private void bindToThread() {\n    \/\/ \u53d6\u51fa transactionInfoHolder(ThreadLocal) \u4e2d\u7684\u7236\u4e8b\u52a1 TransactionInfo1 \u5230 oldTransactionInfo\n    this.oldTransactionInfo = transactionInfoHolder.get();\n    \/\/ \u5c06\u65b0\u4e8b\u52a1 TransactionInfo2 \u4fdd\u5b58\u5230 transactionInfoHolder\n    transactionInfoHolder.set(this);\n}\n\nprivate void restoreThreadLocalStatus() {\n    \/\/ \u5904\u7406\u5b8c\u4e86 TransactionInfo2\uff0c\u6062\u590d\u4e4b\u524d\u4fdd\u5b58\u7684\u7236\u4e8b\u52a1 TransactionInfo1\n    transactionInfoHolder.set(this.oldTransactionInfo);\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5c0f\u7ed3\uff1a oldTransactionInfo \u7684\u4f5c\u7528<\/h4>\n\n\n\n<ul>\n<li>\u2022&nbsp;<strong>\u4e8b\u52a1\u73b0\u573a\u4fdd\u62a4\u548c\u652f\u6301\u4e8b\u52a1\u5d4c\u5957<\/strong>\n<ul>\n<li>\u2022 \u5728<code>\u5d4c\u5957\u4e8b\u52a1<\/code>\u573a\u666f\u4e2d\uff0c\u4fdd\u5b58\u5916\u5c42\u4e8b\u52a1\u7684\u4fe1\u606f\u3002\u8fd9\u6837\u786e\u4fdd\u5185\u5c42\u4e8b\u52a1\u6267\u884c\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u6062\u590d\u5230\u5916\u5c42\u4e8b\u52a1\u7684\u4e0a\u4e0b\u6587\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u2022&nbsp;<strong>\u4e8b\u52a1\u4e0a\u4e0b\u6587\u7684\u5b8c\u6574\u6027<\/strong>\n<ul>\n<li>\u2022 \u7ef4\u62a4&nbsp;<code>ThreadLocal<\/code>&nbsp;\u4e2d\u4e8b\u52a1\u4fe1\u606f&nbsp;<code>TransactionInfo<\/code>&nbsp;\u7684\u5b8c\u6574\u6027<\/li>\n\n\n\n<li>\u2022 \u5f62\u6210\u4e8b\u52a1\u4fe1\u606f\u7684<code>\u94fe\u5f0f\u7ed3\u6784<\/code>\uff0c\u652f\u6301<code>\u591a\u5c42\u4e8b\u52a1\u5d4c\u5957<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u63a5\u7740\u6211\u4eec\u518d\u56de\u5230\u5165\u53e3\u5904\uff0c\u770b\u4e0b&nbsp;<code>createTransactionIfNecessary<\/code>&nbsp;\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u6bd4\u8f83\u5173\u952e\uff0c\u4ece\u4e2d\u53ef\u4ee5\u770b\u51fa\u6211\u4eec\u7684\u4e8b\u52a1\u662f\u5982\u4f55\u521b\u5efa\u7684\uff0c\u4ee5\u53ca\u5728\u6ce8\u89e3\u4e2d\u8bbe\u7f6e\u7684&nbsp;<code>propagation<\/code>&nbsp;\u5c5e\u6027\u5728\u8fd9\u91cc\u4f1a\u8d77\u4ec0\u4e48\u4f5c\u7528\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">protected TransactionInfo createTransactionIfNecessary(PlatformTransactionManager tm,\n        TransactionAttribute txAttr, final String joinpointIdentification) {\n\n    \/\/ \u5982\u679c\u4e8b\u52a1\u6ca1\u6709\u6307\u5b9a\u540d\u79f0\uff0c\u4f7f\u7528\u65b9\u6cd5\u540d\u79f0\u4f5c\u4e3a\u4e8b\u52a1\u540d\u79f0\n    if (txAttr != null &amp;&amp; txAttr.getName() == null) {\n        txAttr = newDelegatingTransactionAttribute(txAttr)....\n    }\n\n    \/\/ \u4ece\u4e8b\u52a1\u7ba1\u7406\u5668\u83b7\u53d6\u4e8b\u52a1\u72b6\u6001\n    TransactionStatusstatus=null;\n    \/\/ \u4e8b\u52a1\u5c5e\u6027\u4e0d\u4e3a\u7a7a\n    if (txAttr != null) {\n        \/\/\u5b58\u5728\u4e8b\u52a1\u7ba1\u7406\u5668\n        if (tm != null) {\n            \/\/ \u6839\u636e\u6307\u5b9a\u7684\u4f20\u64ad\u884c\u4e3a\uff0c\u8fd4\u56de\u5f53\u524d\u6d3b\u8dc3\u7684\u4e8b\u52a1\u6216\u8005\u65b0\u5efa\u4e00\u4e2a\u4e8b\u52a1\n            status = tm.getTransaction(txAttr);\n        }\n    }\n    return prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u5148\u8df3\u8fc7<\/p>\n\n\n\n<p><code>PlatformTransactionManager#getTransaction<\/code><\/p>\n\n\n\n<p>\u5148\u770b\u4e0b<\/p>\n\n\n\n<p><code>TransactionAspectSupport#prepareTransactionInfo<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">protected TransactionInfo prepareTransactionInfo(@Nullable PlatformTransactionManager tm,\n        @Nullable TransactionAttribute txAttr, String joinpointIdentification,\n        @Nullable TransactionStatus status) {\n\n    \/\/ 1. \u521b\u5efa\u4e8b\u52a1\u4fe1\u606f\u5bf9\u8c61\n    TransactionInfotxInfo=newTransactionInfo(tm, txAttr, joinpointIdentification);\n    \n    if (txAttr != null) {\n        \/\/ 2. \u5982\u679c\u6709\u4e8b\u52a1\u5c5e\u6027\u914d\u7f6e\uff0c\u8bf4\u660e\u9700\u8981\u4e8b\u52a1\n        \/\/ 3. \u8bbe\u7f6e\u4e8b\u52a1\u72b6\u6001\n        txInfo.newTransactionStatus(status);\n    }\n    else {\n        \/\/ 4. \u6ca1\u6709\u4e8b\u52a1\u5c5e\u6027\uff0c\u8bf4\u660e\u4e0d\u9700\u8981\u4e8b\u52a1\n    }\n\n    \/\/ 5. \u91cd\u8981\uff1a\u603b\u662f\u7ed1\u5b9a\u5230ThreadLocal\n    txInfo.bindToThread();\n    return txInfo;\n}<\/pre>\n\n\n\n<p><code>prepareTransactionInfo<\/code>&nbsp;\u4e3b\u8981\u5c31\u662f\u521b\u5efa&nbsp;<code>TransactionInfo<\/code>&nbsp;\u5bf9\u8c61\uff08\u5305\u542b\uff1a<code>\u4e8b\u52a1\u7ba1\u7406\u5668<\/code>\u3001<code>\u4e8b\u52a1\u5c5e\u6027<\/code>\u3001<code>\u65b9\u6cd5\u6807\u8bc6\u7b26<\/code>\u7b49\uff09\uff0c\u7ef4\u62a4<code>\u4e8b\u52a1\u72b6\u6001<\/code>\uff0c\u5e76\u4e14\u7ed1\u5b9a\u5230&nbsp;<code>ThreadLocal<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u4e8b\u52a1\u7ba1\u7406\u5668\uff1aPlatformTransactionManager<\/strong><\/p>\n\n\n\n<p>\u73b0\u5728\u7ee7\u7eed\u6df1\u5165\u5230<\/p>\n\n\n\n<p><code>PlatformTransactionManager#getTransaction(TransactionDefinition)<\/code><\/p>\n\n\n\n<p><code>PlatformTransactionManager<\/code>&nbsp;\u5176\u5b9e\u662f\u4e2a<strong>\u63a5\u53e3<\/strong>\uff0c\u6240\u4ee5\u8981\u770b\u7ee7\u627f\u5b83\u7684\u62bd\u8c61\u7c7b\uff1a<\/p>\n\n\n\n<p><code>AbstractPlatformTransactionManager#getTransaction<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public final TransactionStatus getTransaction(TransactionDefinition definition)\n            throws TransactionException {\n\n    \/\/ \u5982\u679c\u6ca1\u6709\u4e8b\u52a1\u5b9a\u4e49\u4f7f\u7528\u9ed8\u8ba4\u503c\n    TransactionDefinitiondef= (definition != null ? definition : TransactionDefinition.withDefaults());\n\n    \/\/ \u83b7\u53d6\u4e8b\u52a1\uff0c\u4e0d\u540c\u7684ORM\u6846\u67b6\uff08JDBC\u3001JPA\u3001Hibernate\u7b49\uff09\u83b7\u53d6\u4e8b\u52a1\u7684\u65b9\u5f0f\u53ef\u80fd\u4e0d\u540c\uff0c\u4ea4\u7531\u5b50\u7c7b\u53bb\u5b9e\u73b0\n    Objecttransaction= doGetTransaction();\n\n    \/\/ \u5982\u679c\u5f53\u524d\u5df2\u7ecf\u5b58\u5728\u4e8b\u52a1\uff0c\u5e76\u4e14\u8be5\u4e8b\u52a1\u5904\u4e8e\u6d3b\u8dc3\u72b6\u6001\n    if (isExistingTransaction(transaction)) {\n        \/\/ \u6839\u636e\u8bbe\u7f6e\u7684\u4f20\u64ad\u884c\u4e3a\u51b3\u5b9a\u5982\u4f55\u5904\u7406\n        return handleExistingTransaction(def, transaction, debugEnabled);\n    }\n\n    \/\/ \u4e3a\u65b0\u4e8b\u52a1\u68c0\u67e5\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\n    if (def.getTimeout() &lt; TransactionDefinition.TIMEOUT_DEFAULT) {\n        thrownewInvalidTimeoutException(\"Invalid transaction timeout\", def.getTimeout());\n    }\n\n    \/\/ \u5f53\u524d\u4e0d\u5b58\u5728\u4e8b\u52a1\uff0c\u6839\u636e\u8bbe\u7f6e\u7684\u4f20\u64ad\u884c\u4e3a\u51b3\u5b9a\u5982\u4f55\u5904\u7406\uff0c\u5982\u679c\u8bbe\u5b9a\u4e3a PROPAGATION_MANDATORY\uff0c\u629b\u51fa\u5f02\u5e38\n    if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {\n        thrownewIllegalTransactionStateException(\n                \"No existing transaction found for transaction marked with propagation 'mandatory'\");\n    }\n    \/\/ \u5982\u679c\u662f\u5176\u5b83\u7684\uff0c\u5f00\u542f\u4e00\u4e2a\u65b0\u4e8b\u52a1\n    elseif (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||\n            def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||\n            def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {\n        SuspendedResourcesHoldersuspendedResources= suspend(null);\n    \n        try {\n            \/\/ \u5f00\u542f\u65b0\u4e8b\u52a1\n            return startTransaction(def, transaction, false, debugEnabled, suspendedResources);\n        }\n        catch (RuntimeException | Error ex) {\n            \/\/ \u6062\u590d\u6302\u8d77\u8d44\u6e90\n            resume(null, suspendedResources);\n            throw ex;\n        }\n    }\n    else {\n        \/\/ \u521b\u5efa\u201c\u7a7a\u201d\u4e8b\u52a1\uff1a\u6ca1\u6709\u5b9e\u9645\u4e8b\u52a1\uff0c\u4f46\u53ef\u80fd\u4f1a\u6709\u4e8b\u52a1\u540c\u6b65\u5668\u3002\n        booleannewSynchronization= (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);\n        return prepareTransactionStatus(def, null, true, newSynchronization, debugEnabled, null);\n    }\n}\n<\/pre>\n\n\n\n<p>\u63a5\u7740\u770b\u770b&nbsp;<code>handleExistingTransaction<\/code>&nbsp;\uff0c\u770b\u4e0b\u5982\u679c<code>\u5f53\u524d\u4e8b\u52a1\u4e0a\u4e0b\u6587<\/code>\u5b58\u5728<code>\u6d3b\u8dc3\u4e8b\u52a1<\/code>\u4f1a\u5982\u4f55\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private TransactionStatus handleExistingTransaction(\nTransactionDefinition definition, Object transaction, boolean debugEnabled)\n        throws TransactionException {\n\n    \/\/ \u5982\u679c\u4f20\u64ad\u884c\u4e3a\u662f PROPAGATION_NEVER\uff0c\u629b\u51fa\u5f02\u5e38\n    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {\n        thrownewIllegalTransactionStateException...\n    }\n\n    \/\/ \u5982\u679c\u4f20\u64ad\u884c\u4e3a\u662f PROPAGATION_NOT_SUPPORTED\uff0c\u76f4\u63a5\u5728\u975e\u4e8b\u52a1\u4e2d\u8fd0\u884c\n    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {\n        \/\/ \u6302\u8d77\u5f53\u524d\u4e8b\u52a1\n        ObjectsuspendedResources= suspend(transaction);\n        booleannewSynchronization= (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);\n        \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012null\uff0c\u8868\u793a\u4e0d\u9700\u8981\u4e8b\u52a1\n        return prepareTransactionStatus(\n                definition, null, false, newSynchronization, debugEnabled, suspendedResources);\n    }\n\n    \/\/ \u5982\u679c\u662f PROPAGATION_REQUIRES_NEW\uff0c\n    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {\n        \/\/ \u6302\u8d77\u5f53\u524d\u4e8b\u52a1\n        SuspendedResourcesHoldersuspendedResources= suspend(transaction);\n        try {\n            \/\/ \u5f00\u542f\u65b0\u4e8b\u52a1\n            return startTransaction(definition, transaction, false, debugEnabled, suspendedResources);\n        }\n        catch (RuntimeException | Error beginEx) {\n            \/\/ \u51fa\u73b0\u5f02\u5e38\uff0c\u6062\u590d\u6302\u8d77\u4e8b\u52a1\n            resumeAfterBeginException(transaction, suspendedResources, beginEx);\n            throw beginEx;\n        }\n    }\n\n    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {\n        \/\/ \u7701\u7565\n    }\n\n    \/\/ PROPAGATION_REQUIRED, PROPAGATION_SUPPORTS, PROPAGATION_MANDATORY:\n    \/\/ \u539f\u4e8b\u52a1\u662f\u5426\u6709\u6548\n    if (isValidateExistingTransaction()) {\n        \/\/ \u65b0\u52a0\u5165\u7684\u4e8b\u52a1\u5fc5\u987b\u4e0e\u539f\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u76f8\u540c \u5426\u5219\u62a5\u9519\n        if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {\n            IntegercurrentIsolationLevel= TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();\n            if (currentIsolationLevel == null || currentIsolationLevel != definition.getIsolationLevel()) {\n                thrownewIllegalTransactionStateException...\n            }\n        }\n        \/\/ \u5f53\u524d\u4e8b\u52a1\u975e\u53ea\u8bfb\u4e8b\u52a1 \u4f46\u662f\u5df2\u7ecf\u5b58\u5728\u7684\u4e8b\u52a1\u662f\u53ea\u8bfb \u62a5\u9519\n        if (!definition.isReadOnly()) {\n            if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {\n                thrownewIllegalTransactionStateException...\n            }\n        }\n    }\n\n    \/\/ \u4e8b\u52a1\u540c\u6b65\u5668\uff1a\u63d0\u4f9b\u4e86\u4e8b\u52a1\u751f\u547d\u5468\u671f\u7684\u94a9\u5b50\u65b9\u6cd5 \u7528\u4e8e\u8d44\u6e90\u7ba1\u7406\u3001\u72b6\u6001\u6e05\u7406\u3001\u76d1\u63a7\u7b49\u573a\u666f\n    booleannewSynchronization= (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);\n    \/\/ \u6839\u636e\u7ed9\u5b9a\u53c2\u6570\u521b\u5efa\u65b0\u7684\u4e8b\u52a1\u72b6\u6001\n    return prepareTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null);\n}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa<\/p>\n\n\n\n<p><code>AbstractPlatformTransactionManager#getTransaction<\/code><\/p>\n\n\n\n<p>\u4f1a\u6839\u636e\u4e0d\u540c\u7684<code>\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a<\/code>\u505a\u51fa\u4e0d\u540c\u884c\u4e3a\uff0c\u521b\u5efa\u65b0\u4e8b\u52a1\uff0c\u6216\u8005\u4f7f\u7528\u73b0\u6709\u4e8b\u52a1\uff0c\u6216\u8005\u6302\u8d77\u5f53\u524d\u4e8b\u52a1\uff0c\u6216\u8005\u629b\u51fa\u5f02\u5e38\u7b49\u3002<\/p>\n\n\n\n<p>\u5c31\u6bd4\u5982<code>PROPAGATION_REQUIRED<\/code>&nbsp;\u548c&nbsp;<code>PROPAGATION_REQUIRES_NEW<\/code>\u7684\u5728\u5904\u7406\u4e0a\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol>\n<li>1.&nbsp;<strong>PROPAGATION_REQUIRED<\/strong><\/li>\n<\/ol>\n\n\n\n<ul>\n<li>\u2022&nbsp;<code>\u4e0d\u6302\u8d77<\/code>\u5f53\u524d\u4e8b\u52a1<\/li>\n\n\n\n<li>\u2022&nbsp;<code>\u4e0d\u521b\u5efa<\/code>\u65b0\u4e8b\u52a1<\/li>\n\n\n\n<li>\u2022 \u8fd4\u56de\u7684&nbsp;<code>TransactionStatus<\/code>&nbsp;\u4e2d&nbsp;<code>transaction<\/code>&nbsp;\u4e3a&nbsp;<code>\u73b0\u6709\u4e8b\u52a1<\/code><\/li>\n<\/ul>\n\n\n\n<ol>\n<li>2.&nbsp;<strong>PROPAGATION_REQUIRES_NEW<\/strong><\/li>\n<\/ol>\n\n\n\n<ul>\n<li>\u2022&nbsp;<code>\u6302\u8d77<\/code>\u5f53\u524d\u4e8b\u52a1<\/li>\n\n\n\n<li>\u2022&nbsp;<code>\u521b\u5efa<\/code>\u65b0\u4e8b\u52a1<\/li>\n\n\n\n<li>\u2022 \u8fd4\u56de\u7684&nbsp;<code>TransactionStatus<\/code>&nbsp;\u4e2d&nbsp;<code>transaction<\/code>&nbsp;\u4e3a&nbsp;<code>\u65b0\u4e8b\u52a1<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u91cc\u7684\u8fd4\u56de\u503c&nbsp;<code>TransactionStatus<\/code>&nbsp;\u5176\u5b9e\u5c31\u662f&nbsp;<code>\u4e8b\u52a1\u72b6\u6001<\/code>\uff0c\u4f1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u4e0a\u9762\u7684&nbsp;<code>prepareTransactionInfo()<\/code>&nbsp;\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public classDefaultTransactionStatusextendsAbstractTransactionStatus {\n    \/\/ \u4e8b\u52a1\u540d\u79f0\n    privatefinal String transactionName;\n    \/\/ \u4e8b\u52a1\n    privatefinal Object transaction;\n    \/\/ \u662f\u5426\u662f\u65b0\u4e8b\u52a1\n    privatefinalboolean newTransaction;\n    \/\/ \u662f\u5426\u662f\u65b0\u7684\u4e8b\u52a1\u540c\u6b65\u5668\n    privatefinalboolean newSynchronization;\n    \/\/ \u662f\u5426\u5d4c\u5957\n    privatefinalboolean nested;\n    \/\/ \u662f\u5426\u53ea\u8bfb\n    privatefinalboolean readOnly;\n    \/\/ debug\u6807\u8bb0\n    privatefinalboolean debug;\n    \/\/ \u4e8b\u52a1\u6302\u8d77\u6682\u5b58\u7684\u8d44\u6e90\n    privatefinal Object suspendedResources;\n}<\/pre>\n\n\n\n<p><strong>\u603b\u7ed3\uff1a\u58f0\u660e\u5f0f\u4e8b\u52a1\u5b9e\u73b0\u539f\u7406\u548c\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<p><strong>\u4e00\u3001\u6838\u5fc3\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<ul>\n<li>\u2022&nbsp;<code>TransactionAspectSupport<\/code>\uff1a\u4e8b\u52a1\u5207\u9762\u652f\u6301\u7c7b<\/li>\n\n\n\n<li>\u2022&nbsp;<code>PlatformTransactionManager<\/code>\uff1a\u4e8b\u52a1\u7ba1\u7406\u5668<\/li>\n\n\n\n<li>\u2022&nbsp;<code>TransactionInfo<\/code>\uff1a\u4e8b\u52a1\u4fe1\u606f\u8f7d\u4f53<\/li>\n\n\n\n<li>\u2022&nbsp;<code>ThreadLocal<\/code>\uff1a\u4e8b\u52a1\u4e0a\u4e0b\u6587\u5b58\u50a8\u5668<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e8c\u3001\u5b9e\u73b0\u8fc7\u7a0b<\/strong><\/p>\n\n\n\n<p><strong>\u6574\u4f53\u6d41\u7a0b\u56fe\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"1024\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-570x1024.webp\" alt=\"\" class=\"wp-image-16825\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-570x1024.webp 570w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-167x300.webp 167w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-768x1380.webp 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-855x1536.webp 855w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-830x1491.webp 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-230x413.webp 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-350x629.webp 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640-480x862.webp 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/01\/640.webp 1072w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/figure><\/div>\n\n\n<p><strong>\u6d41\u7a0b\u8bf4\u660e<\/strong>\uff1a<\/p>\n\n\n\n<ol>\n<li>1.&nbsp;<strong>\u4e8b\u52a1\u62e6\u622a<\/strong>\n<ul>\n<li>\u2022 \u901a\u8fc7&nbsp;<code>AOP<\/code>&nbsp;\u62e6\u622a\u5e26\u6709&nbsp;<code>@Transactional<\/code>&nbsp;\u6ce8\u89e3\u7684<code>\u65b9\u6cd5<\/code>\uff0c\u8c03\u7528&nbsp;<code>TransactionAspectSupport.invokeWithinTransaction()<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>2.&nbsp;<strong>\u4e8b\u52a1\u51c6\u5907<\/strong>\n<ul>\n<li>\u2022 \u83b7\u53d6\u4e8b\u52a1\u5c5e\u6027(<code>TransactionAttribute<\/code>)<\/li>\n\n\n\n<li>\u2022 \u786e\u5b9a\u4e8b\u52a1\u7ba1\u7406\u5668(<code>PlatformTransactionManager<\/code>)<\/li>\n\n\n\n<li>\u2022 \u4e8b\u52a1\u4e0a\u4e0b\u6587\u7ba1\u7406\n<ul>\n<li>\u2022 \u521b\u5efa&nbsp;<code>TransactionInfo<\/code>&nbsp;\u5e76\u4fdd\u5b58\u4e8b\u52a1\u72b6\u6001<\/li>\n\n\n\n<li>\u2022 \u6839\u636e<code>\u4f20\u64ad\u884c\u4e3a<\/code>&nbsp;\u51b3\u5b9a\u662f\u5426\u521b\u5efa\u65b0\u4e8b\u52a1\u6216\u8005\u6302\u8d77\u5f53\u524d\u4e8b\u52a1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u2022 \u901a\u8fc7&nbsp;<code>ThreadLocal<\/code>&nbsp;\u5b58\u50a8\u4e8b\u52a1\u4fe1\u606f<\/li>\n\n\n\n<li>\u2022 \u652f\u6301<code>\u4e8b\u52a1\u5d4c\u5957<\/code>(\u901a\u8fc7&nbsp;<code>oldTransactionInfo<\/code>)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>3.&nbsp;<strong>\u65b9\u6cd5\u6267\u884c<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">try {\n    \/\/ \u6267\u884c\u4e1a\u52a1\u65b9\u6cd5\n    retVal = invocation.proceedWithInvocation();\n    \/\/ \u63d0\u4ea4\u4e8b\u52a1\n    commitTransactionAfterReturning();\n} catch (Exception ex) {\n    \/\/ \u56de\u6eda\u4e8b\u52a1\n    completeTransactionAfterThrowing();\n    throw ex;\n} finally {\n    \/\/ \u6e05\u7406\u4e8b\u52a1\u4fe1\u606f\n    cleanupTransactionInfo();\n}<\/pre>\n\n\n\n<p><strong>\u56db\u3001\u573a\u666f\u9898<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u51fa\u4e00\u4e2a\u573a\u666f\u9898\u6765\u770b\u4e0b\u4f60\u5bf9\u521a\u624d\u7684<code>\u4e8b\u52a1\u7684\u4f20\u64ad\u884c\u4e3a<\/code>\u7684\u7406\u89e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublicclassDemoServiceA {\n\n    @Resource\n    private DemoServiceB demoServiceB;\n\n    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)\n    publicvoida() {\n        \/\/ \u6b65\u9aa4A\uff1a\u5199\u6570\u636e\u5e931\n        \/\/ \u6b65\u9aa4B\uff1a\u5199\u6570\u636e\u5e932\n        \/\/ \u6b65\u9aa4C\uff1a\u8c03\u7528 DemoServiceB.b() \u65b9\u6cd5\u8bfb\u6570\u636e\n        vardata= demoServiceB.b();\n    }\n\n}\n\n@Service\npublicclassDemoServiceB {\n\n    @Transactional(rollbackFor = Exception.class, propagation = Propagation.?)\n    public Object b() {\n        \/\/ read data from db\n    }\n\n}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u573a\u666f\u4e2d\uff0c<code>demoServiceB<\/code>&nbsp;\u7684&nbsp;<code>b()<\/code>&nbsp;\u65b9\u6cd5\u7684<code>\u4f20\u64ad\u884c\u4e3a-propagation<\/code>&nbsp;\u5e94\u8be5\u8bbe\u7f6e\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u662f\u5e94\u8be5\u8bbe\u7f6e\u4e3a\uff1a<\/p>\n\n\n\n<p><code>Propagation.NOT_SUPPORTED<\/code><\/p>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u6837\u7684\u8bdd\uff0c\u5982\u679c<code>\u6b65\u9aa4C<\/code>&nbsp;<code>\u8bfb\u53d6\u6570\u636e<\/code>\u5931\u8d25\uff0c\u4e0d\u4f1a\u5bfc\u81f4<code>\u6b65\u9aa4A<\/code>&nbsp;\u548c&nbsp;<code>\u6b65\u9aa4B<\/code>&nbsp;\u4e2d<code>\u6570\u636e\u4fee\u6539\u56de\u6eda<\/code>\u3002<\/p>\n\n\n\n<p>\u90a3\u5982\u679c\u662f\u4e0b\u9762\u8fd9\u6837\u5462\uff1f<\/p>\n\n\n\n<p>\u8c03\u7528&nbsp;<code>demoServiceB.b()<\/code>&nbsp;\u6b65\u9aa4\u5728\u4e2d\u95f4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)\npublic void a() {\n      \/\/ \u6b65\u9aa4A\uff1a\u5199\u6570\u636e\u5e931\n      \/\/ \u6b65\u9aa4B\uff1a\u8c03\u7528 DemoServiceB.b() \u65b9\u6cd5\u8bfb\u6570\u636e\n      var data = demoServiceB.b();\n      \/\/ \u6b65\u9aa4C\uff1a\u5199\u6570\u636e\u5e932\n}<\/pre>\n\n\n\n<p>\u7b54\u6848\u662f\u5e94\u8be5\u8bbe\u7f6e\u4e3a\uff1a<\/p>\n\n\n\n<p><code>Propagation.REQUIRED<\/code><\/p>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u6837\u7684\u8bdd\uff0c\u5982\u679c<code>\u6b65\u9aa4B<\/code>&nbsp;\u8bfb\u53d6\u6570\u636e\u5931\u8d25\uff0c<code>\u6b65\u9aa4C<\/code>&nbsp;\u8fd8\u6ca1\u5f00\u59cb\uff0c<code>\u6b65\u9aa4A<\/code>&nbsp;\u4fee\u6539\u4e86\u7684\u6570\u636e\u5e94\u8be5\u56de\u6eda\u7684\u3002<\/p>\n\n\n\n<p>\u5e0c\u671b\u5230\u8fd9\u91cc\uff0c\u4f60\u5bf9 Spring \u7684\u4e8b\u52a1\u539f\u7406\u548c\u4e8b\u52a1\u7684\u4f7f\u7528\u6709\u66f4\u591a\u7684\u8ba4\u8bc6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u4e00\u7bc7\u6587\u7ae0\u5f7b\u5e95\u641e\u61c2 Spring \u7684\u4e8b\u52a1 \u5f15\u8a00 \u4f5c\u4e3a\u6280\u672f\u4eba\uff0c\u5f88\u5fcc\u8bb3\u5bf9\u67d0\u4e2a\u95ee\u9898\u6d45\u5c1d\u8f84\u6b62\uff0c\u4e00\u77e5\u534a\u89e3\uff0c\u7279\u522b\u662f\u7ecf\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16823"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16823"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16823\/revisions"}],"predecessor-version":[{"id":16831,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16823\/revisions\/16831"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}