{"id":10020635,"date":"2025-01-20T10:31:49","date_gmt":"2025-01-20T02:31:49","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020635"},"modified":"2025-01-20T10:31:49","modified_gmt":"2025-01-20T02:31:49","slug":"seata-%e6%ba%90%e7%a0%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020635","title":{"rendered":"Seata \u6e90\u7801"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u5f20\u58eb\u6b23<\/p>\n<h2>\u6838\u5fc3\u63a5\u53e3\u548c\u7c7b<\/h2>\n<h3>ResourceManager<\/h3>\n<p>ResourceManager \u662f seata \u7684\u91cd\u8981\u7ec4\u4ef6\u4e4b\u4e00\uff0cRM \u8d1f\u8d23\u7ba1\u7406\u5206\u652f\u6570\u636e\u8d44\u6e90\u7684\u4e8b\u52a1\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038762\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n<p>AbstractResourceManager \u5b9e\u73b0 ResourceManager \u63d0\u4f9b\u6a21\u677f\u65b9\u6cd5\u3002DefaultResourceManager \u9002\u914d\u6240\u6709\u7684 ResourceManager\uff0c\u6240\u6709\u65b9\u6cd5\u8c03\u7528\u90fd\u59d4\u6d3e\u7ed9\u5bf9\u5e94\u8d1f\u8d23\u7684 ResourceManager \u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038763\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>DataSourceManager<\/h3>\n<p>\u6b64\u4e3a AT \u6a21\u5f0f\u6838\u5fc3\u7ba1\u7406\u5668\uff0cDataSourceManager \u7ee7\u627f AbstractResourceManager\uff0c\u7ba1\u7406\u6570\u636e\u5e93 Resouce \u7684\u6ce8\u518c\uff0c\u63d0\u4ea4\u4ee5\u53ca\u56de\u6eda\u7b49\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038764\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<\/p>\n<h3>AsyncWorker<\/h3>\n<p>DataSourceManager \u4e8b\u52a1\u63d0\u4ea4\u59d4\u6d3e\u7ed9 AsyncWorker \u8fdb\u884c\u63d0\u4ea4\u7684\uff0c\u56e0\u4e3a\u90fd\u6210\u529f\u4e86\uff0c\u65e0\u9700\u56de\u6eda\u6210\u529f\u7684\u6570\u636e\uff0c\u53ea\u9700\u8981\u5220\u9664\u751f\u6210\u7684\u64cd\u4f5c\u65e5\u5fd7\u5c31\u884c\uff0c\u91c7\u7528\u5f02\u6b65\u65b9\u5f0f\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<pre><code>io.seata.rm.datasource.AsyncWorker#doBranchCommits\nUndoLogManagerFactory.getUndoLogManager(dataSourceProxy.getDbType()).batchDeleteUndoLog(xids, branchIds, conn);\n<\/code><\/pre>\n<h3>UndoLogManager<\/h3>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038765\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<\/p>\n<h3>TransactionManager<\/h3>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038766\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>DefaultTransactionManager<\/h3>\n<p>TransactionManagerHolder \u4e3a\u521b\u5efa\u5355\u4f8b TransactionManager \u7684\u5de5\u5382\uff0c\u53ef\u4ee5\u4f7f\u7528 EnhancedServiceLoader \u7684 spi \u673a\u5236\u52a0\u8f7d\u81ea\u5b9a\u4e49\u7684\u7c7b\uff0c\u9ed8\u8ba4\u4e3aDefaultTransactionManager\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038767\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>GlobalTransaction<\/h3>\n<p>GlobalTransaction \u63a5\u53e3\u63d0\u4f9b\u7ed9\u7528\u6237\u5f00\u542f\u4e8b\u52a1\uff0c\u63d0\u4ea4\uff0c\u56de\u6eda\uff0c\u83b7\u53d6\u72b6\u6001\u7b49\u65b9\u6cd5\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038768\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>DefaultGlobalTransaction<\/h3>\n<p>DefaultGlobalTransaction \u662f GlobalTransaction \u63a5\u53e3\u7684\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u5b83\u6301\u6709 TransactionManager \u5bf9\u8c61\uff0c\u9ed8\u8ba4\u5f00\u542f\u4e8b\u52a1\u8d85\u65f6\u65f6\u95f4\u4e3a 60 \u79d2\uff0c\u9ed8\u8ba4\u540d\u79f0\u4e3a default\uff0c\u56e0\u4e3a\u8c03\u7528\u8005\u7684\u4e1a\u52a1\u65b9\u6cd5\u53ef\u80fd\u591a\u91cd\u5d4c\u5957\u521b\u5efa\u591a\u4e2a GlobalTransaction \u5bf9\u8c61\u5f00\u542f\u4e8b\u52a1\u65b9\u6cd5\uff0c\u56e0\u6b64 GlobalTransaction \u6709 GlobalTransactionRole \u89d2\u8272\u5c5e\u6027\uff0c\u53ea\u6709Launcher \u89d2\u8272\u7684\u624d\u6709\u5f00\u542f\u3001\u63d0\u4ea4\u3001\u56de\u6eda\u4e8b\u52a1\u7684\u6743\u5229\u3002<\/p>\n<h3>GlobalTransactionContext<\/h3>\n<p>GlobalTransactionContext \u4e3a\u64cd\u4f5c GlobalTransaction \u7684\u5de5\u5177\u7c7b\uff0c\u63d0\u4f9b\u521b\u5efa\u65b0\u7684 GlobalTransaction\uff0c\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u6709\u7684 GlobalTransaction \u7b49\u65b9\u6cd5\u3002<\/p>\n<h3>GlobalTransactionScanner<\/h3>\n<p>GlobalTransactionScanner \u7ee7\u627f AbstractAutoProxyCreator \u7c7b\uff0c\u5373\u5b9e\u73b0\u4e86 SmartInstantiationAwareBeanPostProcessor \u63a5\u53e3\uff0c\u4f1a\u5728 spring \u5bb9\u5668\u542f\u52a8\u521d\u59cb\u5316bean \u7684\u65f6\u5019\uff0c\u5bf9 bean \u8fdb\u884c\u4ee3\u7406\u64cd\u4f5c\u3002wrapIfNecessary \u4e3a\u7ee7\u627f\u7236\u7c7b\u4ee3\u7406 bean \u7684\u6838\u5fc3\u65b9\u6cd5\uff0c\u5982\u679c\u914d\u7f6e\u4e86 service.disableGlobalTransaction \u4e3a false \u5c5e\u6027\u5219\u6ce8\u89e3\u4e0d\u751f\u6548\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5219\u5bf9 GlobalTransactional \u6216 GlobalLock \u7684\u65b9\u6cd5\u8fdb\u884c\u62e6\u622a\u4ee3\u7406\u3002<\/p>\n<h3>GlobalTransactionalInterceptor<\/h3>\n<p>GlobalTransactionalInterceptor \u5b9e\u73b0 aop \u7684 MethodInterceptor \u63a5\u53e3\uff0c\u5bf9\u6709 @GlobalTransactional \u6216 GlobalLock \u6ce8\u89e3\u7684\u65b9\u6cd5\u8fdb\u884c\u4ee3\u7406\u3002<\/p>\n<h3>TransactionalTemplate<\/h3>\n<p>TransactionalTemplate \u6a21\u677f\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f00\u542f\u4e8b\u52a1\uff0c\u6267\u884c\u4e1a\u52a1\uff0c\u6210\u529f\u63d0\u4ea4\u548c\u5931\u8d25\u56de\u6eda\u7684\u6a21\u677f\u65b9\u6cd5 execute(TransactionalExecutor business)\u3002<\/p>\n<pre><code>try {\n    \/\/ 2. If the tx role is 'GlobalTransactionRole.Launcher', send the request of beginTransaction to TC,\n    \/\/    else do nothing. Of course, the hooks will still be triggered.\n    beginTransaction(txInfo, tx);\n\n    Object rs;\n    try {\n        \/\/ Do Your Business\n        rs = business.execute();\n    } catch (Throwable ex) {\n        \/\/ 3. The needed business exception to rollback.\n        completeTransactionAfterThrowing(txInfo, tx, ex);\n        throw ex;\n    }\n\n    \/\/ 4. everything is fine, commit.\n    commitTransaction(tx);\n\n    return rs;\n} finally {\n    \/\/5. clear\n    resumeGlobalLockConfig(previousConfig);\n    triggerAfterCompletion();\n    cleanUp();\n}\n<\/code><\/pre>\n<h3>Resource<\/h3>\n<p>Resource \u80fd\u88ab ResourceManager \u7ba1\u7406\u5e76\u4e14\u80fd\u591f\u5173\u8054 GlobalTransaction\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038769\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>DataSourceProxy<\/h3>\n<p>DataSourceProxy \u5b9e\u73b0 Resource \u63a5\u53e3\uff0cBranchType \u4e3a AT \u81ea\u52a8\u6a21\u5f0f\u3002\u5b83\u7ee7\u627f AbstractDataSourceProxy \u4ee3\u7406\u7c7b\uff0c\u6240\u6709\u7684 DataSource \u76f8\u5173\u7684\u65b9\u6cd5\u8c03\u7528\u4f20\u5165\u7684targetDataSource \u4ee3\u7406\u7c7b\u7684\u65b9\u6cd5\uff0c\u9664\u4e86\u521b\u5efa connection \u65b9\u6cd5\u4e3a\u521b\u5efa ConnectionProxy \u4ee3\u7406\u7c7b\u3002\u5bf9\u8c61\u521d\u59cb\u5316\u65f6\u83b7\u53d6\u8fde\u63a5\u7684 jdbcUrl \u4f5c\u4e3a resourceId\uff0c\u5e76\u6ce8\u518c\u81f3DefaultResourceManager \u8fdb\u884c\u7ba1\u7406\u3002\u540c\u65f6\u8fd8\u63d0\u4f9b\u83b7\u53d6\u539f\u59cb\u8fde\u63a5\u4e0d\u88ab\u4ee3\u7406\u7684 getPlainConnection \u65b9\u6cd5\u3002<\/p>\n<h3>ExecuteTemplate<\/h3>\n<p>ExecuteTemplate \u4e3a\u5177\u4f53 statement \u7684 execute\uff0cexecuteQuery \u548c executeUpdate \u6267\u884c\u63d0\u4f9b\u6a21\u677f\u65b9\u6cd5\u3002<\/p>\n<h3>Executor<\/h3>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038770\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>SQLRecognizer<\/h3>\n<p>SQLRecognizer \u8bc6\u522b sql \u7c7b\u578b\uff0c\u83b7\u53d6\u8868\u540d\uff0c\u8868\u522b\u540d\u4ee5\u53ca\u539f\u751f sql<\/p>\n<h3>UndoExecutorFactory<\/h3>\n<p>UndoExecutorFactory \u6839\u636e sqlType \u751f\u6210\u5bf9\u5e94\u7684 AbstractUndoExecutor\u3002<\/p>\n<p>UndoExecutor \u4e3a\u751f\u6210\u6267\u884c undoSql \u7684\u6838\u5fc3\u3002\u5982\u679c\u5168\u5c40\u4e8b\u52a1\u56de\u6eda\uff0c\u5b83\u4f1a\u6839\u636e beforeImage \u548c afterImage \u4ee5\u53ca sql \u7c7b\u578b\u751f\u6210\u5bf9\u5e94\u7684\u53cd\u5411 sql \u6267\u884c\u56de\u6eda\u6570\u636e\uff0c\u5e76\u6dfb\u52a0\u810f\u6570\u636e\u6821\u9a8c\u673a\u5236\uff0c\u4f7f\u56de\u6eda\u6570\u636e\u66f4\u52a0\u53ef\u9760\u3002<\/p>\n<h3>DefaultCoordinator<\/h3>\n<p>DefaultCoordinator \u5373\u4e3a TC\uff0c\u5168\u5c40\u4e8b\u52a1\u9ed8\u8ba4\u7684\u4e8b\u52a1\u534f\u8c03\u5668\u3002\u5b83\u7ee7\u627f AbstractTCInboundHandler \u63a5\u53e3\uff0c\u4e3a TC \u63a5\u6536 RM \u548c TM \u7684 request \u8bf7\u6c42\u6570\u636e\uff0c\u662f\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u7684\u5904\u7406\u5668\u3002\u5b9e\u73b0 TransactionMessageHandler \u63a5\u53e3\uff0c\u53bb\u5904\u7406\u6536\u5230\u7684 RPC \u4fe1\u606f\u3002\u5b9e\u73b0 ResourceManagerInbound \u63a5\u53e3\uff0c\u53d1\u9001\u81f3 RM \u7684 branchCommit\uff0cbranchRollback \u8bf7\u6c42\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038771\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038772\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<\/p>\n<h3>Core<\/h3>\n<p>Core \u63a5\u53e3\u4e3a seata \u5904\u7406\u5168\u5c40\u4e8b\u52a1\u534f\u8c03\u5668 TC \u7684\u6838\u5fc3\u5904\u7406\u5668\uff0c\u5b83\u7ee7\u627f ResourceManagerOutbound \u63a5\u53e3\uff0c\u63a5\u53d7\u6765\u81ea RM \u7684 rpc \u7f51\u7edc\u8bf7\u6c42 \uff08branchRegister\uff0cbranchReport\uff0clockQuery\uff09\u3002\u540c\u65f6\u7ee7\u627f TransactionManager \u63a5\u53e3\uff0c\u63a5\u53d7\u6765\u81ea TM \u7684 rpc \u7f51\u7edc\u8bf7\u6c42\uff08begin\uff0c commit,rollback,getStatus\uff09\uff0c\u53e6\u5916\u63d0\u4f9b\u63d0\u4f9b 3 \u4e2a\u63a5\u53e3\u65b9\u6cd5\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046038773\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>GlobalSession<\/h3>\n<p>GlobalSession \u662f seata \u534f\u8c03\u5668 DefaultCoordinator \u7ba1\u7406\u7ef4\u62a4\u7684\u91cd\u8981\u90e8\u4ef6\uff0c\u5f53\u7528\u6237\u5f00\u542f\u5168\u5c40\u5206\u5e03\u5f0f\u4e8b\u52a1\uff0cTM \u8c03\u7528 begin \u65b9\u6cd5\u8bf7\u6c42\u81f3 TC\uff0cTC \u5219\u521b\u5efaGlobalSession \u5b9e\u4f8b\u5bf9\u8c61\uff0c\u8fd4\u56de\u552f\u4e00\u7684 xid\u3002\u5b83\u5b9e\u73b0 SessionLifecycle \u63a5\u53e3\uff0c\u63d0\u4f9b begin\uff0cchangeStatus\uff0cchangeBranchStatus\uff0caddBranch\uff0cremoveBranch \u7b49\u64cd\u4f5c session \u548c branchSession \u7684\u65b9\u6cd5\u3002<\/p>\n<h3>BranchSession<\/h3>\n<p>BranchSession \u4e3a\u5206\u652f session\uff0c\u7ba1\u7406\u5206\u652f\u6570\u636e\uff0c\u53d7 globalSession \u7edf\u4e00\u8c03\u5ea6\u7ba1\u7406\uff0c\u5b83\u7684 lock \u548c unlock \u65b9\u6cd5\u7531 lockManger \u5b9e\u73b0\u3002<\/p>\n<h3>LockManager<\/h3>\n<p>DefaultLockManager \u662f LockManager \u7684\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u5b83\u83b7\u53d6 branchSession \u7684 lockKey\uff0c\u8f6c\u6362\u6210 List&lt;RowLock&gt;\uff0c\u59d4\u6d3e Locker \u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h3>Locker<\/h3>\n<p>Locker \u63a5\u53e3\u63d0\u4f9b\u6839\u636e\u884c\u6570\u636e\u83b7\u53d6\u9501\uff0c\u91ca\u653e\u9501\uff0c\u662f\u5426\u9501\u4f4f\u548c\u6e05\u9664\u6240\u6709\u9501\u7684\u65b9\u6cd5\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046038760\">https:\/\/segmentfault.com\/a\/1190000046038760<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u5f20\u58eb\u6b23 \u6838\u5fc3\u63a5\u53e3\u548c\u7c7b ResourceManager ResourceManager \u662f se [&hellip;]<\/p>\n","protected":false},"author":1,"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-10020635","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\/10020635","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020635"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020635\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}