{"id":10006410,"date":"2022-09-06T01:12:16","date_gmt":"2022-09-05T17:12:16","guid":{"rendered":"https:\/\/ityet.com\/?p=10006410"},"modified":"2022-09-06T01:12:16","modified_gmt":"2022-09-05T17:12:16","slug":"%e9%98%bf%e9%87%8c-seata-%e6%96%b0%e7%89%88%e6%9c%ac%e7%bb%88%e4%ba%8e%e8%a7%a3%e5%86%b3%e4%ba%86-tcc-%e6%a8%a1%e5%bc%8f%e7%9a%84%e5%b9%82%e7%ad%89%e3%80%81%e6%82%ac%e6%8c%82%e5%92%8c%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006410","title":{"rendered":"\u963f\u91cc Seata \u65b0\u7248\u672c\u7ec8\u4e8e\u89e3\u51b3\u4e86 TCC \u6a21\u5f0f\u7684\u5e42\u7b49\u3001\u60ac\u6302\u548c\u7a7a\u56de\u6eda\u95ee\u9898"},"content":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u4eca\u5929\u6765\u804a\u4e00\u804a\u963f\u91cc\u5df4\u5df4 Seata \u65b0\u7248\u672c\uff081.5.1\uff09\u662f\u600e\u4e48\u89e3\u51b3 TCC \u6a21\u5f0f\u4e0b\u7684\u5e42\u7b49\u3001\u60ac\u6302\u548c\u7a7a\u56de\u6eda\u95ee\u9898\u7684\u3002\u4f5c\u8005\uff1a\u6731\u664b\u541b\u00a0\u5927\u5bb6\u597d\uff0c\u6211\u662f\u541b\u54e5\u3002\u00a0\u4eca\u5929\u6765\u804a\u4e00\u804a\u963f\u91cc\u5df4\u5df4 Seata \u65b0\u7248\u672c\uff081.5.1\uff09\u662f\u600e\u4e48\u89e3\u51b3 TCC \u6a21\u5f0f\u4e0b\u7684\u5e42\u7b49\u3001\u60ac\u6302\u548c\u7a7a\u56de\u6eda\u95ee\u9898\u7684\u3002\u00a0TCC \u56de\u987e\u00a0TCC \u6a21\u5f0f\u662f\u6700\u7ecf\u5178\u7684\u5206\u5e03\u5f0f\u4e8b\u52a1\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u5c06\u5206\u5e03\u5f0f\u4e8b\u52a1\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\u6765\u6267\u884c\uff0ctry \u9636\u6bb5\u5bf9\u6bcf\u4e2a\u5206\u652f\u4e8b\u52a1\u8fdb\u884c\u9884\u7559\u8d44\u6e90\uff0c\u5982\u679c\u6240\u6709\u5206\u652f\u4e8b\u52a1\u90fd\u9884\u7559\u8d44\u6e90\u6210\u529f\uff0c\u5219\u8fdb\u5165 commit \u9636\u6bb5\u63d0\u4ea4\u5168\u5c40\u4e8b\u52a1\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u8282\u70b9\u9884\u7559\u8d44\u6e90\u5931\u8d25\u5219\u8fdb\u5165 cancel \u9636\u6bb5\u56de\u6eda\u5168\u5c40\u4e8b\u52a1\u3002\u00a0\u4ee5\u4f20\u7edf\u7684\u8ba2\u5355\u3001\u5e93\u5b58\u3001\u8d26\u6237\u670d\u52a1\u4e3a\u4f8b\uff0c\u5728 try \u9636\u6bb5\u5c1d\u8bd5\u9884\u7559\u8d44\u6e90\uff0c\u63d2\u5165\u8ba2\u5355\u3001\u6263\u51cf\u5e93\u5b58\u3001\u6263\u51cf\u91d1\u989d\uff0c\u8fd9\u4e09\u4e2a\u670d\u52a1\u90fd\u662f\u8981\u63d0\u4ea4\u672c\u5730\u4e8b\u52a1\u7684\uff0c\u8fd9\u91cc\u53ef\u4ee5\u628a\u8d44\u6e90\u8f6c\u5165\u4e2d\u95f4\u8868\u3002\u5728 commit \u9636\u6bb5\uff0c\u518d\u628a try \u9636\u6bb5\u9884\u7559\u7684\u8d44\u6e90\u8f6c\u5165\u6700\u7ec8\u8868\u3002\u800c\u5728 cancel \u9636\u6bb5\uff0c\u628a try \u9636\u6bb5\u9884\u7559\u7684\u8d44\u6e90\u8fdb\u884c\u91ca\u653e\uff0c\u6bd4\u5982\u628a\u8d26\u6237\u91d1\u989d\u8fd4\u56de\u7ed9\u5ba2\u6237\u7684\u8d26\u6237\u3002\u00a0\u6ce8\u610f\uff1atry \u9636\u6bb5\u5fc5\u987b\u662f\u8981\u63d0\u4ea4\u672c\u5730\u4e8b\u52a1\u7684\uff0c\u6bd4\u5982\u6263\u51cf\u8ba2\u5355\u91d1\u989d\uff0c\u5fc5\u987b\u628a\u94b1\u4ece\u5ba2\u6237\u8d26\u6237\u6263\u6389\uff0c\u5982\u679c\u4e0d\u6263\u6389\uff0c\u5728 commit \u9636\u6bb5\u5ba2\u6237\u8d26\u6237\u94b1\u4e0d\u591f\u4e86\uff0c\u5c31\u4f1a\u51fa\u95ee\u9898\u3002\u00a0try-commit\u00a0try \u9636\u6bb5\u9996\u5148\u8fdb\u884c\u9884\u7559\u8d44\u6e90\uff0c\u7136\u540e\u5728 commit \u9636\u6bb5\u6263\u9664\u8d44\u6e90\u3002\u5982\u4e0b\u56fe\uff1a\u00a0<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435024\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0try-cancel\u00a0try \u9636\u6bb5\u9996\u5148\u8fdb\u884c\u9884\u7559\u8d44\u6e90\uff0c\u9884\u7559\u8d44\u6e90\u65f6\u6263\u51cf\u5e93\u5b58\u5931\u8d25\u5bfc\u81f4\u5168\u5c40\u4e8b\u52a1\u56de\u6eda\uff0c\u5728 cancel \u9636\u6bb5\u91ca\u653e\u8d44\u6e90\u3002\u5982\u4e0b\u56fe\uff1a\u00a0<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435025\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0TCC \u4f18\u52bf\u00a0TCC \u6a21\u5f0f\u6700\u5927\u7684\u4f18\u52bf\u662f\u6548\u7387\u9ad8\u3002TCC \u6a21\u5f0f\u5728 try \u9636\u6bb5\u7684\u9501\u5b9a\u8d44\u6e90\u5e76\u4e0d\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u9501\u5b9a\uff0c\u800c\u662f\u771f\u5b9e\u63d0\u4ea4\u4e86\u672c\u5730\u4e8b\u52a1\uff0c\u5c06\u8d44\u6e90\u9884\u7559\u5230\u4e2d\u95f4\u6001\uff0c\u5e76\u4e0d\u9700\u8981\u963b\u585e\u7b49\u5f85\uff0c\u56e0\u6b64\u6548\u7387\u6bd4\u5176\u4ed6\u6a21\u5f0f\u8981\u9ad8\u3002\u00a0\u540c\u65f6 TCC \u6a21\u5f0f\u8fd8\u53ef\u4ee5\u8fdb\u884c\u5982\u4e0b\u4f18\u5316\uff1a\u00a0\u5f02\u6b65\u63d0\u4ea4\u00a0try \u9636\u6bb5\u6210\u529f\u540e\uff0c\u4e0d\u7acb\u5373\u8fdb\u5165 confirm\/cancel \u9636\u6bb5\uff0c\u800c\u662f\u8ba4\u4e3a\u5168\u5c40\u4e8b\u52a1\u5df2\u7ecf\u7ed3\u675f\u4e86\uff0c\u542f\u52a8\u5b9a\u65f6\u4efb\u52a1\u6765\u5f02\u6b65\u6267\u884c confirm\/cancel\uff0c\u6263\u51cf\u6216\u91ca\u653e\u8d44\u6e90\uff0c\u8fd9\u6837\u4f1a\u6709\u5f88\u5927\u7684\u6027\u80fd\u63d0\u5347\u3002\u00a0\u540c\u5e93\u6a21\u5f0f\u00a0TCC \u6a21\u5f0f\u4e2d\u6709\u4e09\u4e2a\u89d2\u8272\uff1a\u00a0TM\uff1a\u7ba1\u7406\u5168\u5c40\u4e8b\u52a1\uff0c\u5305\u62ec\u5f00\u542f\u5168\u5c40\u4e8b\u52a1\uff0c\u63d0\u4ea4\/\u56de\u6eda\u5168\u5c40\u4e8b\u52a1\uff1bRM\uff1a\u7ba1\u7406\u5206\u652f\u4e8b\u52a1\uff1bTC: \u7ba1\u7406\u5168\u5c40\u4e8b\u52a1\u548c\u5206\u652f\u4e8b\u52a1\u7684\u72b6\u6001\u3002\u00a0\u4e0b\u56fe\u6765\u81ea Seata \u5b98\u7f51\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435026\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435027\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0TM \u5f00\u542f\u5168\u5c40\u4e8b\u52a1\u65f6\uff0cRM \u9700\u8981\u5411 TC \u53d1\u9001\u6ce8\u518c\u6d88\u606f\uff0cTC \u4fdd\u5b58\u5206\u652f\u4e8b\u52a1\u7684\u72b6\u6001\u3002TM \u8bf7\u6c42\u63d0\u4ea4\u6216\u56de\u6eda\u65f6\uff0cTC \u9700\u8981\u5411 RM \u53d1\u9001\u63d0\u4ea4\u6216\u56de\u6eda\u6d88\u606f\u3002\u8fd9\u6837\u5305\u542b\u4e24\u4e2a\u4e2a\u5206\u652f\u4e8b\u52a1\u7684\u5206\u5e03\u5f0f\u4e8b\u52a1\u4e2d\uff0cTC \u548c RM \u4e4b\u95f4\u6709\u56db\u6b21 RPC\u3002\u00a0\u4f18\u5316\u540e\u7684\u6d41\u7a0b\u5982\u4e0b\u56fe\uff1a\u00a0<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435028\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0TC \u4fdd\u5b58\u5168\u5c40\u4e8b\u52a1\u7684\u72b6\u6001\u3002TM \u5f00\u542f\u5168\u5c40\u4e8b\u52a1\u65f6\uff0cRM \u4e0d\u518d\u9700\u8981\u5411 TC \u53d1\u9001\u6ce8\u518c\u6d88\u606f\uff0c\u800c\u662f\u628a\u5206\u652f\u4e8b\u52a1\u72b6\u6001\u4fdd\u5b58\u5728\u4e86\u672c\u5730\u3002TM \u5411 TC \u53d1\u9001\u63d0\u4ea4\u6216\u56de\u6eda\u6d88\u606f\u540e\uff0cRM \u5f02\u6b65\u7ebf\u7a0b\u9996\u5148\u67e5\u51fa\u672c\u5730\u4fdd\u5b58\u7684\u672a\u63d0\u4ea4\u5206\u652f\u4e8b\u52a1\uff0c\u7136\u540e\u5411 TC \u53d1\u9001\u6d88\u606f\u83b7\u53d6\uff08\u672c\u5730\u5206\u652f\u4e8b\u52a1\u6240\u5728\u7684\uff09\u5168\u5c40\u4e8b\u52a1\u72b6\u6001\uff0c\u4ee5\u51b3\u5b9a\u662f\u63d0\u4ea4\u8fd8\u662f\u56de\u6eda\u672c\u5730\u4e8b\u52a1\u3002\u00a0\u8fd9\u6837\u4f18\u5316\u540e\uff0cRPC \u6b21\u6570\u51cf\u5c11\u4e86 50%\uff0c\u6027\u80fd\u5927\u5e45\u63d0\u5347\u3002\u00a0RM \u4ee3\u7801\u793a\u4f8b\u00a0\u4ee5\u5e93\u5b58\u670d\u52a1\u4e3a\u4f8b\uff0cRM \u5e93\u5b58\u670d\u52a1\u63a5\u53e3\u4ee3\u7801\u5982\u4e0b\uff1a\u00a0@LocalTCC<br \/>public interface StorageService {<\/p>\n<pre><code>\/**\n * \u6263\u51cf\u5e93\u5b58\n * @param xid \u5168\u5c40xid\n * @param productId \u4ea7\u54c1id\n * @param count \u6570\u91cf\n * @return\n *\/\n@TwoPhaseBusinessAction(name = &quot;storageApi&quot;, commitMethod = &quot;commit&quot;, rollbackMethod = &quot;rollback&quot;, useTCCFence = true)\nboolean decrease(String xid, Long productId, Integer count);\n\n\/**\n * \u63d0\u4ea4\u4e8b\u52a1\n * @param actionContext\n * @return\n *\/\nboolean commit(BusinessActionContext actionContext);\n\n\/**\n * \u56de\u6eda\u4e8b\u52a1\n * @param actionContext\n * @return\n *\/\nboolean rollback(BusinessActionContext actionContext);<\/code><\/pre>\n<p>}\u901a\u8fc7 @LocalTCC \u8fd9\u4e2a\u6ce8\u89e3\uff0cRM \u521d\u59cb\u5316\u7684\u65f6\u5019\u4f1a\u5411 TC \u6ce8\u518c\u4e00\u4e2a\u5206\u652f\u4e8b\u52a1\u3002\u5728 try \u9636\u6bb5\u7684\u65b9\u6cd5\uff08decrease\u65b9\u6cd5\uff09\u4e0a\u6709\u4e00\u4e2a @TwoPhaseBusinessAction \u6ce8\u89e3\uff0c\u8fd9\u91cc\u5b9a\u4e49\u4e86\u5206\u652f\u4e8b\u52a1\u7684 resourceId\uff0ccommit \u65b9\u6cd5\u548c cancel \u65b9\u6cd5\uff0cuseTCCFence \u8fd9\u4e2a\u5c5e\u6027\u4e0b\u4e00\u8282\u518d\u8bb2\u3002\u00a0TCC \u5b58\u5728\u95ee\u9898\u00a0TCC \u6a21\u5f0f\u4e2d\u5b58\u5728\u7684\u4e09\u5927\u95ee\u9898\u662f\u5e42\u7b49\u3001\u60ac\u6302\u548c\u7a7a\u56de\u6eda\u3002\u5728 Seata1.5.1 \u7248\u672c\u4e2d\uff0c\u589e\u52a0\u4e86\u4e00\u5f20\u4e8b\u52a1\u63a7\u5236\u8868\uff0c\u8868\u540d\u662f tcc_fence_log \u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u800c\u5728\u4e0a\u4e00\u8282 @TwoPhaseBusinessAction \u6ce8\u89e3\u4e2d\u63d0\u5230\u7684\u5c5e\u6027 useTCCFence \u5c31\u662f\u6765\u6307\u5b9a\u662f\u5426\u5f00\u542f\u8fd9\u4e2a\u673a\u5236\uff0c\u8fd9\u4e2a\u5c5e\u6027\u503c\u9ed8\u8ba4\u662f false\u3002\u00a0tcc_fence_log \u5efa\u8868\u8bed\u53e5\u5982\u4e0b\uff08MySQL \u8bed\u6cd5\uff09\uff1a\u00a0CREATE TABLE IF NOT EXISTS <code>tcc_fence_log<\/code><br \/>(<\/p>\n<pre><code>`xid`           VARCHAR(128)  NOT NULL COMMENT 'global id',\n`branch_id`     BIGINT        NOT NULL COMMENT 'branch id',\n`action_name`   VARCHAR(64)   NOT NULL COMMENT 'action name',\n`status`        TINYINT       NOT NULL COMMENT 'status(tried:1;committed:2;rollbacked:3;suspended:4)',\n`gmt_create`    DATETIME(3)   NOT NULL COMMENT 'create time',\n`gmt_modified`  DATETIME(3)   NOT NULL COMMENT 'update time',\nPRIMARY KEY (`xid`, `branch_id`),\nKEY `idx_gmt_modified` (`gmt_modified`),\nKEY `idx_status` (`status`)<\/code><\/pre>\n<p>) ENGINE = InnoDB<br \/>DEFAULT CHARSET = utf8mb4;\u00a0\u5e42\u7b49\u00a0\u5728 commit\/cancel \u9636\u6bb5\uff0c\u56e0\u4e3a TC \u6ca1\u6709\u6536\u5230\u5206\u652f\u4e8b\u52a1\u7684\u54cd\u5e94\uff0c\u9700\u8981\u8fdb\u884c\u91cd\u8bd5\uff0c\u8fd9\u5c31\u8981\u5206\u652f\u4e8b\u52a1\u652f\u6301\u5e42\u7b49\u3002\u00a0\u6211\u4eec\u770b\u4e00\u4e0b\u65b0\u7248\u672c\u662f\u600e\u4e48\u89e3\u51b3\u7684\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5728 TCCResourceManager \u7c7b\uff1a\u00a0@Override<br \/>public BranchStatus branchCommit(BranchType branchType, String xid, long branchId, String resourceId,<\/p>\n<pre><code>     String applicationData) throws TransactionException {<\/code><\/pre>\n<p>TCCResource tccResource = (TCCResource)tccResourceCache.get(resourceId);<br \/> \/\/\u7701\u7565\u5224\u65ad<br \/> Object targetTCCBean = tccResource.getTargetBean();<br \/> Method commitMethod = tccResource.getCommitMethod();<br \/> \/\/\u7701\u7565\u5224\u65ad<br \/> try {<br \/>  \/\/BusinessActionContext<br \/>  BusinessActionContext businessActionContext = getBusinessActionContext(xid, branchId, resourceId,<br \/>   applicationData);<br \/>  Object[] args = this.getTwoPhaseCommitArgs(tccResource, businessActionContext);<br \/>  Object ret;<br \/>  boolean result;<br \/>  \/\/\u6ce8\u89e3 useTCCFence \u5c5e\u6027\u662f\u5426\u8bbe\u7f6e\u4e3a true<br \/>  if (Boolean.TRUE.equals(businessActionContext.getActionContext(Constants.USE_TCC_FENCE))) {<br \/>   try {<\/p>\n<pre><code>result = TCCFenceHandler.commitFence(commitMethod, targetTCCBean, xid, branchId, args);<\/code><\/pre>\n<p>} catch (SkipCallbackWrapperException | UndeclaredThrowableException e) {<\/p>\n<pre><code>throw e.getCause();<\/code><\/pre>\n<p>}<br \/>  } else {<br \/>   \/\/\u7701\u7565\u903b\u8f91<br \/>  }<br \/>  LOGGER.info(&#8220;TCC resource commit result : {}, xid: {}, branchId: {}, resourceId: {}&#8221;, result, xid, branchId, resourceId);<br \/>  return result ? BranchStatus.PhaseTwo_Committed : BranchStatus.PhaseTwo_CommitFailed_Retryable;<br \/> } catch (Throwable t) {<br \/>  \/\/\u7701\u7565<br \/>  return BranchStatus.PhaseTwo_CommitFailed_Retryable;<br \/> }<br \/>}\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u5230\uff0c\u6267\u884c\u5206\u652f\u4e8b\u52a1\u63d0\u4ea4\u65b9\u6cd5\u65f6\uff0c\u9996\u5148\u5224\u65ad useTCCFence \u5c5e\u6027\u662f\u5426\u4e3a true\uff0c\u5982\u679c\u4e3a true\uff0c\u5219\u8d70 TCCFenceHandler \u7c7b\u4e2d\u7684 commitFence \u903b\u8f91\uff0c\u5426\u5219\u8d70\u666e\u901a\u63d0\u4ea4\u903b\u8f91\u3002\u00a0TCCFenceHandler \u7c7b\u4e2d\u7684 commitFence \u65b9\u6cd5\u8c03\u7528\u4e86 TCCFenceHandler \u7c7b\u7684 commitFence \u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a\u00a0public static boolean commitFence(Method commitMethod, Object targetTCCBean,<\/p>\n<pre><code>      String xid, Long branchId, Object[] args) {<\/code><\/pre>\n<p>return transactionTemplate.execute(status -&gt; {<br \/>  try {<br \/>   Connection conn = DataSourceUtils.getConnection(dataSource);<br \/>   TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);<br \/>   if (tccFenceDO == null) {<\/p>\n<pre><code>throw new TCCFenceException(String.format(&quot;TCC fence record not exists, commit fence method failed. xid= %s, branchId= %s&quot;, xid, branchId),\n  FrameworkErrorCode.RecordAlreadyExists);<\/code><\/pre>\n<p>}<br \/>   if (TCCFenceConstant.STATUS_COMMITTED == tccFenceDO.getStatus()) {<\/p>\n<pre><code>LOGGER.info(&quot;Branch transaction has already committed before. idempotency rejected. xid: {}, branchId: {}, status: {}&quot;, xid, branchId, tccFenceDO.getStatus());\nreturn true;<\/code><\/pre>\n<p>}<br \/>   if (TCCFenceConstant.STATUS_ROLLBACKED == tccFenceDO.getStatus() || TCCFenceConstant.STATUS_SUSPENDED == tccFenceDO.getStatus()) {<\/p>\n<pre><code>if (LOGGER.isWarnEnabled()) {\n LOGGER.warn(&quot;Branch transaction status is unexpected. xid: {}, branchId: {}, status: {}&quot;, xid, branchId, tccFenceDO.getStatus());\n}\nreturn false;<\/code><\/pre>\n<p>}<br \/>   return updateStatusAndInvokeTargetMethod(conn, commitMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_COMMITTED, status, args);<br \/>  } catch (Throwable t) {<br \/>   status.setRollbackOnly();<br \/>   throw new SkipCallbackWrapperException(t);<br \/>  }<br \/> });<br \/>}\u4ece\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u63d0\u4ea4\u4e8b\u52a1\u65f6\u9996\u5148\u4f1a\u5224\u65ad tcc_fence_log \u8868\u4e2d\u662f\u5426\u5df2\u7ecf\u6709\u8bb0\u5f55\uff0c\u5982\u679c\u6709\u8bb0\u5f55\uff0c\u5219\u5224\u65ad\u4e8b\u52a1\u6267\u884c\u72b6\u6001\u5e76\u8fd4\u56de\u3002\u8fd9\u6837\u5982\u679c\u5224\u65ad\u5230\u4e8b\u52a1\u7684\u72b6\u6001\u5df2\u7ecf\u662f STATUS_COMMITTED\uff0c\u5c31\u4e0d\u4f1a\u518d\u6b21\u63d0\u4ea4\uff0c\u4fdd\u8bc1\u4e86\u5e42\u7b49\u3002\u5982\u679c tcc_fence_log \u8868\u4e2d\u6ca1\u6709\u8bb0\u5f55\uff0c\u5219\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff0c\u4f9b\u540e\u9762\u91cd\u8bd5\u65f6\u5224\u65ad\u3002\u00a0Rollback \u7684\u903b\u8f91\u8ddf commit \u7c7b\u4f3c\uff0c\u903b\u8f91\u5728\u7c7b TCCFenceHandler \u7684 rollbackFence \u65b9\u6cd5\u3002\u00a0\u7a7a\u56de\u6eda\u00a0\u5982\u4e0b\u56fe\uff0c\u8d26\u6237\u670d\u52a1\u662f\u4e24\u4e2a\u8282\u70b9\u7684\u96c6\u7fa4\uff0c\u5728 try \u9636\u6bb5\u8d26\u6237\u670d\u52a1 1 \u8fd9\u4e2a\u8282\u70b9\u53d1\u751f\u4e86\u6545\u969c\uff0ctry \u9636\u6bb5\u5728\u4e0d\u8003\u8651\u91cd\u8bd5\u7684\u60c5\u51b5\u4e0b\uff0c\u5168\u5c40\u4e8b\u52a1\u5fc5\u987b\u8981\u8d70\u5411\u7ed3\u675f\u72b6\u6001\uff0c\u8fd9\u6837\u5c31\u9700\u8981\u5728\u8d26\u6237\u670d\u52a1\u4e0a\u6267\u884c\u4e00\u6b21 cancel \u64cd\u4f5c\uff0c\u8fd9\u6837\u5c31\u7a7a\u8dd1\u4e86\u4e00\u6b21\u56de\u6eda\u64cd\u4f5c\u3002\u00a0<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435029\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0Seata \u7684\u89e3\u51b3\u65b9\u6848\u662f\u5728 try \u9636\u6bb5 \u5f80 tcc_fence_log \u00a0\u8868\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff0cstatus \u5b57\u6bb5\u503c\u662f STATUS_TRIED\uff0c\u5728 Rollback \u9636\u6bb5\u5224\u65ad\u8bb0\u5f55\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u56de\u6eda\u64cd\u4f5c\u3002\u4ee3\u7801\u5982\u4e0b\uff1a\u00a0\/\/TCCFenceHandler \u7c7b<br \/>public static Object prepareFence(String xid, Long branchId, String actionName, Callback&lt;Object&gt; targetCallback) {<br \/> return transactionTemplate.execute(status -&gt; {<br \/>  try {<br \/>   Connection conn = DataSourceUtils.getConnection(dataSource);<br \/>   boolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_TRIED);<br \/>   LOGGER.info(&#8220;TCC fence prepare result: {}. xid: {}, branchId: {}&#8221;, result, xid, branchId);<br \/>   if (result) {<\/p>\n<pre><code>return targetCallback.execute();<\/code><\/pre>\n<p>} else {<\/p>\n<pre><code>throw new TCCFenceException(String.format(&quot;Insert tcc fence record error, prepare fence failed. xid= %s, branchId= %s&quot;, xid, branchId),\n  FrameworkErrorCode.InsertRecordError);<\/code><\/pre>\n<p>}<br \/>  } catch (TCCFenceException e) {<br \/>   \/\/\u7701\u7565<br \/>  } catch (Throwable t) {<br \/>   \/\/\u7701\u7565<br \/>  }<br \/> });<br \/>}\u5728 Rollback \u9636\u6bb5\u7684\u5904\u7406\u903b\u8f91\u5982\u4e0b:\u00a0\/\/TCCFenceHandler \u7c7b<br \/>public static boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,<\/p>\n<pre><code>     String xid, Long branchId, Object[] args, String actionName) {<\/code><\/pre>\n<p>return transactionTemplate.execute(status -&gt; {<br \/>  try {<br \/>   Connection conn = DataSourceUtils.getConnection(dataSource);<br \/>   TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);<br \/>   \/\/ non_rollback<br \/>   if (tccFenceDO == null) {<\/p>\n<pre><code>\/\/\u4e0d\u6267\u884c\u56de\u6eda\u903b\u8f91\nreturn true;<\/code><\/pre>\n<p>} else {<\/p>\n<pre><code>if (TCCFenceConstant.STATUS_ROLLBACKED == tccFenceDO.getStatus() || TCCFenceConstant.STATUS_SUSPENDED == tccFenceDO.getStatus()) {\n LOGGER.info(&quot;Branch transaction had already rollbacked before, idempotency rejected. xid: {}, branchId: {}, status: {}&quot;, xid, branchId, tccFenceDO.getStatus());\n return true;\n}\nif (TCCFenceConstant.STATUS_COMMITTED == tccFenceDO.getStatus()) {\n if (LOGGER.isWarnEnabled()) {\n  LOGGER.warn(&quot;Branch transaction status is unexpected. xid: {}, branchId: {}, status: {}&quot;, xid, branchId, tccFenceDO.getStatus());\n }\n return false;\n}<\/code><\/pre>\n<p>}<br \/>   return updateStatusAndInvokeTargetMethod(conn, rollbackMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_ROLLBACKED, status, args);<br \/>  } catch (Throwable t) {<br \/>   status.setRollbackOnly();<br \/>   throw new SkipCallbackWrapperException(t);<br \/>  }<br \/> });<br \/>}\u00a0updateStatusAndInvokeTargetMethod \u65b9\u6cd5\u6267\u884c\u7684 sql \u5982\u4e0b\uff1a\u00a0update tcc_fence_log set status = ?, gmt_modified = ?<\/p>\n<pre><code>where xid = ? and  branch_id = ? and status = ? ;\u53ef\u89c1\u5c31\u662f\u628a tcc_fence_log \u8868\u8bb0\u5f55\u7684 \u00a0status \u00a0\u5b57\u6bb5\u503c\u4ece STATUS_TRIED \u6539\u4e3a STATUS_ROLLBACKED\uff0c\u5982\u679c\u66f4\u65b0\u6210\u529f\uff0c\u5c31\u6267\u884c\u56de\u6eda\u903b\u8f91\u3002\u00a0\u60ac\u6302\u00a0\u60ac\u6302\u662f\u6307\u56e0\u4e3a\u7f51\u7edc\u95ee\u9898\uff0cRM \u5f00\u59cb\u6ca1\u6709\u6536\u5230 try \u6307\u4ee4\uff0c\u4f46\u662f\u6267\u884c\u4e86 Rollback \u540e RM \u53c8\u6536\u5230\u4e86 try \u6307\u4ee4\u5e76\u4e14\u9884\u7559\u8d44\u6e90\u6210\u529f\uff0c\u8fd9\u65f6\u5168\u5c40\u4e8b\u52a1\u5df2\u7ecf\u7ed3\u675f\uff0c\u6700\u7ec8\u5bfc\u81f4\u9884\u7559\u7684\u8d44\u6e90\u4e0d\u80fd\u91ca\u653e\u3002\u5982\u4e0b\u56fe\uff1a\u00a0<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435030\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0Seata \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u6267\u884c Rollback \u65b9\u6cd5\u65f6\u5148\u5224\u65ad tcc_fence_log \u662f\u5426\u5b58\u5728\u5f53\u524d xid \u7684\u8bb0\u5f55\uff0c\u5982\u679c\u6ca1\u6709\u5219\u5411 tcc_fence_log \u8868\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff0c\u72b6\u6001\u662f STATUS_SUSPENDED\uff0c\u5e76\u4e14\u4e0d\u518d\u6267\u884c\u56de\u6eda\u64cd\u4f5c\u3002\u4ee3\u7801\u5982\u4e0b\uff1a\u00a0public static boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,<\/p>\n<pre><code>     String xid, Long branchId, Object[] args, String actionName) {<\/code><\/pre>\n<p>return transactionTemplate.execute(status -&gt; {<br \/>  try {<br \/>   Connection conn = DataSourceUtils.getConnection(dataSource);<br \/>   TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);<br \/>   \/\/ non_rollback<br \/>   if (tccFenceDO == null) {<\/p>\n<pre><code>   \/\/\u63d2\u5165\u9632\u60ac\u6302\u8bb0\u5f55\nboolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_SUSPENDED);\n\/\/\u7701\u7565\u903b\u8f91\nreturn true;<\/code><\/pre>\n<p>} else {<\/p>\n<pre><code>\/\/\u7701\u7565\u903b\u8f91<\/code><\/pre>\n<p>}<br \/>   return updateStatusAndInvokeTargetMethod(conn, rollbackMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_ROLLBACKED, status, args);<br \/>  } catch (Throwable t) {<br \/>   \/\/\u7701\u7565\u903b\u8f91<br \/>  }<br \/> });<br \/>}\u800c\u540e\u9762\u6267\u884c try \u9636\u6bb5\u65b9\u6cd5\u65f6\u9996\u5148\u4f1a\u5411 tcc_fence_log \u8868\u63d2\u5165\u4e00\u6761\u5f53\u524d xid \u7684\u8bb0\u5f55\uff0c\u8fd9\u6837\u5c31\u9020\u6210\u4e86\u4e3b\u952e\u51b2\u7a81\u3002\u4ee3\u7801\u5982\u4e0b\uff1a\u00a0\/\/TCCFenceHandler \u7c7b<br \/>public static Object prepareFence(String xid, Long branchId, String actionName, Callback&lt;Object&gt; targetCallback) {<br \/> return transactionTemplate.execute(status -&gt; {<br \/>  try {<br \/>   Connection conn = DataSourceUtils.getConnection(dataSource);<br \/>   boolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_TRIED);<br \/>   \/\/\u7701\u7565\u903b\u8f91<br \/>  } catch (TCCFenceException e) {<br \/>   if (e.getErrcode() == FrameworkErrorCode.DuplicateKeyException) {<\/p>\n<pre><code>LOGGER.error(&quot;Branch transaction has already rollbacked before,prepare fence failed. xid= {},branchId = {}&quot;, xid, branchId);\naddToLogCleanQueue(xid, branchId);<\/code><\/pre>\n<p>}<br \/>   status.setRollbackOnly();<br \/>   throw new SkipCallbackWrapperException(e);<br \/>  } catch (Throwable t) {<br \/>   \/\/\u7701\u7565<br \/>  }<br \/> });<br \/>}\u00a0\u6ce8\u610f\uff1aqueryTCCFenceDO \u65b9\u6cd5 sql \u4e2d\u4f7f\u7528\u4e86 for update\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u62c5\u5fc3 Rollback \u65b9\u6cd5\u4e2d\u83b7\u53d6\u4e0d\u5230 tcc_fence_log \u8868\u8bb0\u5f55\u800c\u65e0\u6cd5\u5224\u65ad try \u9636\u6bb5\u672c\u5730\u4e8b\u52a1\u7684\u6267\u884c\u7ed3\u679c\u4e86\u3002\u00a0\u603b\u7ed3\u00a0TCC \u6a21\u5f0f\u662f\u5206\u5e03\u5f0f\u4e8b\u52a1\u4f7f\u7528\u6700\u591a\u7684\u6a21\u5f0f\uff0c\u4f46\u662f\u5e42\u7b49\u3001\u60ac\u6302\u548c\u7a7a\u56de\u6eda\u4e00\u76f4\u662f TCC \u6a21\u5f0f\u9700\u8981\u8003\u8651\u7684\u95ee\u9898\uff0cSeata \u6846\u67b6\u5728 1.5.1 \u7248\u672c\u5b8c\u7f8e\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\u3002\u00a0\u5bf9 tcc_fence_log \u8868\u7684\u64cd\u4f5c\u4e5f\u9700\u8981\u8003\u8651\u4e8b\u52a1\u7684\u63a7\u5236\uff0cSeata \u4f7f\u7528\u4e86\u4ee3\u7406\u6570\u636e\u6e90\uff0c\u4f7f tcc_fence_log \u8868\u64cd\u4f5c\u548c RM \u4e1a\u52a1\u64cd\u4f5c\u5728\u540c\u4e00\u4e2a\u672c\u5730\u4e8b\u52a1\u4e2d\u6267\u884c\uff0c\u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1\u672c\u5730\u64cd\u4f5c\u548c\u5bf9 tcc_fence_log \u7684\u64cd\u4f5c\u540c\u65f6\u6210\u529f\u6216\u5931\u8d25\u3002\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=DXoQA4Hrm26SOAYa0wSjiQ%3D%3D.qsz71yUbffoEVhhD6c6%2FQGFhosD9IGaA0P6YnDiVRdjkiDwTyGn7SzdgKAVPQH67\" rel=\"nofollow\">https:\/\/click.aliyun.com\/m\/10&#8230;<\/a>\u672c\u6587\u4e3a\u963f\u91cc\u4e91\u539f\u521b\u5185\u5bb9\uff0c\u672a\u7ecf\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042435022\">https:\/\/segmentfault.com\/a\/1190000042435022<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u4eca\u5929\u6765\u804a\u4e00\u804a\u963f\u91cc\u5df4\u5df4 Seata \u65b0\u7248\u672c\uff081.5.1\uff09\u662f\u600e\u4e48\u89e3\u51b3 TCC \u6a21\u5f0f\u4e0b\u7684\u5e42\u7b49\u3001\u60ac\u6302\u548c\u7a7a\u56de\u6eda\u95ee [&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-10006410","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\/10006410","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=10006410"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006410\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}