{"id":39444,"date":"2025-05-23T18:13:32","date_gmt":"2025-05-23T10:13:32","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/39444.html"},"modified":"2025-05-23T18:13:32","modified_gmt":"2025-05-23T10:13:32","slug":"seata%e6%ba%90%e7%a0%81-7-seata-tcc%e6%a8%a1%e5%bc%8f%e7%9a%84%e4%ba%8b%e5%8a%a1%e5%a4%84%e7%90%86%e4%b8%80","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/39444.html","title":{"rendered":"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00"},"content":{"rendered":"<p>\u5927\u7eb2<\/p>\n<p>1.Seata TCC\u5206\u5e03\u5f0f\u4e8b\u52a1\u6848\u4f8b\u914d\u7f6e<\/p>\n<p>2.Seata TCC\u6848\u4f8b\u670d\u52a1\u63d0\u4f9b\u8005\u542f\u52a8\u5206\u6790<\/p>\n<p>3.&#064;TwoPhaseBusinessAction\u6ce8\u89e3\u626b\u63cf\u6e90\u7801<\/p>\n<p>4.Seata TCC\u6848\u4f8b\u5206\u5e03\u5f0f\u4e8b\u52a1\u5165\u53e3\u5206\u6790<\/p>\n<p>5.TCC\u6838\u5fc3\u6ce8\u89e3\u626b\u63cf\u4e0e\u4ee3\u7406\u521b\u5efa\u5165\u53e3\u6e90\u7801<\/p>\n<p>6.TCC\u52a8\u6001\u4ee3\u7406\u62e6\u622a\u5668TccActionInterceptor<\/p>\n<p>7.Action\u62e6\u622a\u5904\u7406\u5668ActionInterceptorHandler<\/p>\n<p>8.Seata TCC\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u6ce8\u518c\u63d0\u4ea4\u56de\u6eda\u5904\u7406\u6e90\u7801<\/p>\n<\/p>\n<p>1.Seata TCC\u5206\u5e03\u5f0f\u4e8b\u52a1\u6848\u4f8b\u914d\u7f6e<\/p>\n<p>(1)\u4f4d\u4e8eseata-samples\u7684tcc\u6a21\u5757\u4e0b\u7684Demo\u5de5\u7a0b<\/p>\n<p>(2)Demo\u5de5\u7a0b\u7684\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>(3)Demo\u5de5\u7a0b\u8fd0\u884c\u8bf4\u660e<\/p>\n<\/p>\n<p>(1)\u4f4d\u4e8eseata-samples\u7684tcc\u6a21\u5757\u4e0b\u7684Demo\u5de5\u7a0b<\/p>\n<p>dubbo-tcc-sample\u6a21\u5757\u4e3b\u8981\u6f14\u793a\u4e86TCC\u6a21\u5f0f\u4e0b\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u63d0\u4ea4\u548c\u56de\u6eda\u3002\u8be5Demo\u4e2d\u4e00\u4e2a\u5206\u5e03\u5f0f\u4e8b\u52a1\u5185\u4f1a\u6709\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005&#xff0c;\u8fd9\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\u5206\u522b\u662fTccActionOne\u548cTccActionTwo\u3002\u5206\u5e03\u5f0f\u4e8b\u52a1\u63d0\u4ea4\u5219\u4e24\u8005\u5747\u63d0\u4ea4&#xff0c;\u5206\u5e03\u5f0f\u4e8b\u52a1\u56de\u6eda\u5219\u4e24\u8005\u5747\u56de\u6eda\u3002<\/p>\n<\/p>\n<p>\u8fd9\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\u5747\u662fDubbo\u8fdc\u7a0b\u670d\u52a1\u3002\u4e00\u4e2a\u5e94\u7528\u4f5c\u4e3a\u670d\u52a1\u63d0\u4f9b\u65b9&#xff0c;\u4f1a\u5b9e\u73b0\u8fd9\u4e24\u4e2aTCC\u53c2\u4e0e\u8005&#xff0c;\u5e76\u5c06\u5b83\u4eec\u53d1\u5e03\u6210Dubbo\u670d\u52a1\u3002\u53e6\u5916\u4e00\u4e2a\u5e94\u7528\u4f5c\u4e3a\u4e8b\u52a1\u53d1\u8d77\u65b9&#xff0c;\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1&#xff0c;\u7136\u540e\u8c03\u7528\u7f16\u6392TCC\u53c2\u4e0e\u8005&#xff0c;\u6267\u884c\u8fdc\u7a0bDubbo\u670d\u52a1\u3002<\/p>\n<\/p>\n<p>TccActionOne\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>public interface TccActionOne {<br \/>\n    &#064;TwoPhaseBusinessAction(name &#061; &#034;DubboTccActionOne&#034;, commitMethod &#061; &#034;commit&#034;, rollbackMethod &#061; &#034;rollback&#034;)<br \/>\n    public boolean prepare(BusinessActionContext actionContext,<br \/>\n        &#064;BusinessActionContextParameter(paramName &#061; &#034;a&#034;) int a);<br \/>\n    public boolean commit(BusinessActionContext actionContext);<br \/>\n    public boolean rollback(BusinessActionContext actionContext);<br \/>\n} <\/p>\n<p>TccActionTwo\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>public interface TccActionTwo {<br \/>\n    &#064;TwoPhaseBusinessAction(name &#061; &#034;DubboTccActionTwo&#034;, commitMethod &#061; &#034;commit&#034;, rollbackMethod &#061; &#034;rollback&#034;)<br \/>\n    public boolean prepare(BusinessActionContext actionContext,<br \/>\n        &#064;BusinessActionContextParameter(paramName &#061; &#034;b&#034;) String b,<br \/>\n        &#064;BusinessActionContextParameter(paramName &#061; &#034;c&#034;, index &#061; 1) List list);<br \/>\n    public boolean commit(BusinessActionContext actionContext);<br \/>\n    public boolean rollback(BusinessActionContext actionContext);<br \/>\n} <\/p>\n<p>(2)Demo\u5de5\u7a0b\u7684\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>\u4e00.seata-tcc.xml<\/p>\n<p>&lt;beans xmlns:xsi&#061;&#034;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#034;<br \/>\n       xmlns&#061;&#034;http:\/\/www.springframework.org\/schema\/beans&#034;<br \/>\n       xsi:schemaLocation&#061;&#034;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&#034;<br \/>\n       default-autowire&#061;&#034;byName&#034;&gt;<\/p>\n<p>    &lt;!&#8211; fescar bean scanner &#8211;&gt;<br \/>\n    &lt;bean class&#061;&#034;io.seata.spring.annotation.GlobalTransactionScanner&#034;&gt;<br \/>\n        &lt;constructor-arg value&#061;&#034;tcc-sample&#034;\/&gt;<br \/>\n        &lt;constructor-arg value&#061;&#034;my_test_tx_group&#034;\/&gt;<br \/>\n    &lt;\/bean&gt;<\/p>\n<p>    &lt;bean id&#061;&#034;tccActionOneImpl&#034; class&#061;&#034;io.seata.samples.tcc.dubbo.action.impl.TccActionOneImpl&#034;\/&gt;<br \/>\n    &lt;bean id&#061;&#034;tccActionTwoImpl&#034; class&#061;&#034;io.seata.samples.tcc.dubbo.action.impl.TccActionTwoImpl&#034;\/&gt;<br \/>\n    &lt;bean id&#061;&#034;tccTransactionService&#034; class&#061;&#034;io.seata.samples.tcc.dubbo.service.TccTransactionService&#034;\/&gt;<br \/>\n&lt;\/beans&gt; <\/p>\n<p>\u4e8c.seata-dubbo-provider.xml<\/p>\n<p>&lt;beans xmlns:xsi&#061;&#034;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#034;<br \/>\n       xmlns:dubbo&#061;&#034;http:\/\/code.alibabatech.com\/schema\/dubbo&#034;<br \/>\n       xmlns&#061;&#034;http:\/\/www.springframework.org\/schema\/beans&#034;<br \/>\n       xsi:schemaLocation&#061;&#034;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd<br \/>\n       http:\/\/code.alibabatech.com\/schema\/dubbo<br \/>\n       http:\/\/code.alibabatech.com\/schema\/dubbo\/dubbo.xsd&#034; default-autowire&#061;&#034;byName&#034;&gt;<br \/>\n    &lt;dubbo:application name&#061;&#034;tcc-sample&#034;&gt;<br \/>\n        &lt;dubbo:parameter key&#061;&#034;qos.enable&#034; value&#061;&#034;false&#034;\/&gt;<br \/>\n    &lt;\/dubbo:application&gt;<\/p>\n<p>    &lt;!&#8211;\u4f7f\u7528 zookeeper \u6ce8\u518c\u4e2d\u5fc3\u66b4\u9732\u670d\u52a1&#xff0c;\u6ce8\u610f\u8981\u5148\u5f00\u542f zookeeper&#8211;&gt;<br \/>\n    &lt;dubbo:registry address&#061;&#034;zookeeper:\/\/127.0.0.1:2181&#034;\/&gt;<br \/>\n    &lt;dubbo:protocol name&#061;&#034;dubbo&#034; port&#061;&#034;-1&#034;\/&gt;<br \/>\n    &lt;dubbo:provider timeout&#061;&#034;10000&#034; threads&#061;&#034;10&#034; threadpool&#061;&#034;fixed&#034; loadbalance&#061;&#034;roundrobin&#034;\/&gt;<\/p>\n<p>    &lt;!&#8211; \u7b2c\u4e00\u4e2aTCC \u53c2\u4e0e\u8005\u670d\u52a1\u53d1\u5e03 &#8211;&gt;<br \/>\n    &lt;dubbo:service interface&#061;&#034;io.seata.samples.tcc.dubbo.action.TccActionOne&#034; ref&#061;&#034;tccActionOneImpl&#034;\/&gt;<br \/>\n    &lt;!&#8211; \u7b2c\u4e8c\u4e2aTCC \u53c2\u4e0e\u8005\u670d\u52a1\u53d1\u5e03 &#8211;&gt;<br \/>\n    &lt;dubbo:service interface&#061;&#034;io.seata.samples.tcc.dubbo.action.TccActionTwo&#034; ref&#061;&#034;tccActionTwoImpl&#034;\/&gt;<br \/>\n&lt;\/beans&gt; <\/p>\n<p>\u4e09.seata-dubbo-reference.xml<\/p>\n<p>&lt;beans xmlns:xsi&#061;&#034;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#034;<br \/>\n       xmlns:dubbo&#061;&#034;http:\/\/code.alibabatech.com\/schema\/dubbo&#034;<br \/>\n       xmlns&#061;&#034;http:\/\/www.springframework.org\/schema\/beans&#034;<br \/>\n       xsi:schemaLocation&#061;&#034;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd<br \/>\n       http:\/\/code.alibabatech.com\/schema\/dubbo<br \/>\n       http:\/\/code.alibabatech.com\/schema\/dubbo\/dubbo.xsd&#034; default-autowire&#061;&#034;byName&#034;&gt;<br \/>\n    &lt;dubbo:application name&#061;&#034;tcc-sample-reference&#034;&gt;<br \/>\n        &lt;dubbo:parameter key&#061;&#034;qos.enable&#034; value&#061;&#034;false&#034;\/&gt;<br \/>\n    &lt;\/dubbo:application&gt;<\/p>\n<p>    &lt;!&#8211;\u4f7f\u7528 zookeeper \u6ce8\u518c\u4e2d\u5fc3\u66b4\u9732\u670d\u52a1&#xff0c;\u6ce8\u610f\u8981\u5148\u5f00\u542f zookeeper&#8211;&gt;<br \/>\n    &lt;dubbo:registry address&#061;&#034;zookeeper:\/\/127.0.0.1:2181&#034;\/&gt;<br \/>\n    &lt;dubbo:protocol name&#061;&#034;dubbo&#034; port&#061;&#034;-1&#034;\/&gt;<br \/>\n    &lt;dubbo:provider timeout&#061;&#034;10000&#034; threads&#061;&#034;10&#034; threadpool&#061;&#034;fixed&#034; loadbalance&#061;&#034;roundrobin&#034;\/&gt;<\/p>\n<p>    &lt;!&#8211; \u7b2c\u4e00\u4e2aTCC\u53c2\u4e0e\u8005 \u670d\u52a1\u8ba2\u9605 &#8211;&gt;<br \/>\n    &lt;dubbo:reference id&#061;&#034;tccActionOne&#034; interface&#061;&#034;io.seata.samples.tcc.dubbo.action.TccActionOne&#034; check&#061;&#034;false&#034; lazy&#061;&#034;true&#034;\/&gt;<br \/>\n    &lt;!&#8211; \u7b2c\u4e8c\u4e2aTCC\u53c2\u4e0e\u8005 \u670d\u52a1\u8ba2\u9605 &#8211;&gt;<br \/>\n    &lt;dubbo:reference id&#061;&#034;tccActionTwo&#034; interface&#061;&#034;io.seata.samples.tcc.dubbo.action.TccActionTwo&#034; check&#061;&#034;false&#034; lazy&#061;&#034;true&#034;\/&gt;<br \/>\n&lt;\/beans&gt; <\/p>\n<p>(3)Demo\u5de5\u7a0b\u8fd0\u884c\u6307\u5357<\/p>\n<p>\u4e00.\u542f\u52a8Seata Server<\/p>\n<\/p>\n<p>\u4e8c.\u542f\u52a8Dubbo\u670d\u52a1\u5e94\u7528<\/p>\n<p>\u8fd0\u884cDubboTccProviderStarter\u3002\u8be5\u5e94\u7528\u4f1a\u53d1\u5e03Dubbo\u670d\u52a1&#xff0c;\u5e76\u4e14\u5b9e\u73b0\u4e86\u4e24\u4e2aTCC\u53c2\u4e0e\u8005\u3002<\/p>\n<p>public class TccProviderStarter extends AbstractStarter {<br \/>\n    public static void main(String[] args) throws Exception {<br \/>\n        new TccProviderStarter().start0(args);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    protected void start0(String[] args) throws Exception {<br \/>\n        ClassPathXmlApplicationContext applicationContext &#061; new ClassPathXmlApplicationContext(<br \/>\n            new String[]{&#034;spring\/seata-tcc.xml&#034;, &#034;spring\/seata-dubbo-provider.xml&#034;}<br \/>\n        );<br \/>\n        new ApplicationKeeper().keep();<br \/>\n    }<br \/>\n}<\/p>\n<p>public class TccActionOneImpl implements TccActionOne {<br \/>\n    &#064;Override<br \/>\n    public boolean prepare(BusinessActionContext actionContext, int a) {<br \/>\n        String xid &#061; actionContext.getXid();<br \/>\n        System.out.println(&#034;TccActionOne prepare, xid:&#034; &#043; xid &#043; &#034;, a:&#034; &#043; a);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean commit(BusinessActionContext actionContext) {<br \/>\n        String xid &#061; actionContext.getXid();<br \/>\n        System.out.println(&#034;TccActionOne commit, xid:&#034; &#043; xid &#043; &#034;, a:&#034; &#043; actionContext.getActionContext(&#034;a&#034;));<br \/>\n        ResultHolder.setActionOneResult(xid, &#034;T&#034;);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean rollback(BusinessActionContext actionContext) {<br \/>\n        String xid &#061; actionContext.getXid();<br \/>\n        System.out.println(&#034;TccActionOne rollback, xid:&#034; &#043; xid &#043; &#034;, a:&#034; &#043; actionContext.getActionContext(&#034;a&#034;));<br \/>\n        ResultHolder.setActionOneResult(xid, &#034;R&#034;);<br \/>\n        return true;<br \/>\n    }<br \/>\n}<\/p>\n<p>public class TccActionTwoImpl implements TccActionTwo {<br \/>\n    &#064;Override<br \/>\n    public boolean prepare(BusinessActionContext actionContext, String b, List list) {<br \/>\n        String xid &#061; actionContext.getXid();<br \/>\n        System.out.println(&#034;TccActionTwo prepare, xid:&#034; &#043; xid &#043; &#034;, b:&#034; &#043; b &#043; &#034;, c:&#034; &#043; list.get(1));<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean commit(BusinessActionContext actionContext) {<br \/>\n        String xid &#061; actionContext.getXid();<br \/>\n        System.out.println(&#034;TccActionTwo commit, xid:&#034; &#043; xid &#043; &#034;, b:&#034; &#043; actionContext.getActionContext(&#034;b&#034;) &#043; &#034;, c:&#034; &#043; actionContext.getActionContext(&#034;c&#034;));<br \/>\n        ResultHolder.setActionTwoResult(xid, &#034;T&#034;);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean rollback(BusinessActionContext actionContext) {<br \/>\n        String xid &#061; actionContext.getXid();<br \/>\n        System.out.println(&#034;TccActionTwo rollback, xid:&#034; &#043; xid &#043; &#034;, b:&#034; &#043; actionContext.getActionContext(&#034;b&#034;) &#043; &#034;, c:&#034; &#043; actionContext.getActionContext(&#034;c&#034;));<br \/>\n        ResultHolder.setActionTwoResult(xid, &#034;R&#034;);<br \/>\n        return true;<br \/>\n    }<br \/>\n} <\/p>\n<p>\u4e09.\u542f\u52a8\u4e8b\u52a1\u5e94\u7528<\/p>\n<p>\u8fd0\u884cTccConsumerStarter\u3002\u8be5\u5e94\u7528\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1&#xff0c;\u53d1\u8d77\u5206\u5e03\u5f0f\u4e8b\u52a1&#xff0c;\u8c03\u7528\u4e0a\u8ff0\u4e24\u4e2aTCC\u53c2\u4e0e\u8005&#xff0c;\u5185\u542bTCC\u4e8b\u52a1\u63d0\u4ea4\u573a\u666f\u548cTCC\u4e8b\u52a1\u56de\u6eda\u573a\u666f\u7684\u6f14\u793a\u3002<\/p>\n<p>public class TccConsumerStarter extends AbstractStarter {<br \/>\n    static TccTransactionService tccTransactionService &#061; null;<\/p>\n<p>    public static void main(String[] args) throws Exception {<br \/>\n        new TccConsumerStarter().start0(args);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    protected void start0(String[] args) throws Exception {<br \/>\n        ClassPathXmlApplicationContext applicationContext &#061; new ClassPathXmlApplicationContext(<br \/>\n            new String[]{&#034;spring\/seata-tcc.xml&#034;, &#034;spring\/seata-dubbo-reference.xml&#034;}<br \/>\n        );<br \/>\n        tccTransactionService &#061; (TccTransactionService) applicationContext.getBean(&#034;tccTransactionService&#034;);<br \/>\n        \/\/\u5206\u5e03\u5f0f\u4e8b\u52a1\u63d0\u4ea4demo<br \/>\n        transactionCommitDemo();<br \/>\n        \/\/\u5206\u5e03\u5f0f\u4e8b\u52a1\u56de\u6edademo<br \/>\n        transactionRollbackDemo();<br \/>\n    }<\/p>\n<p>    private static void transactionCommitDemo() throws InterruptedException {<br \/>\n        String txId &#061; tccTransactionService.doTransactionCommit();<br \/>\n        System.out.println(txId);<br \/>\n        Assert.isTrue(StringUtils.isNotEmpty(txId), &#034;\u4e8b\u52a1\u5f00\u542f\u5931\u8d25&#034;);<br \/>\n        System.out.println(&#034;transaction commit demo finish.&#034;);<br \/>\n    }<\/p>\n<p>    private static void transactionRollbackDemo() throws InterruptedException {<br \/>\n        Map map &#061; new HashMap(16);<br \/>\n        try {<br \/>\n            tccTransactionService.doTransactionRollback(map);<br \/>\n            Assert.isTrue(false, &#034;\u5206\u5e03\u5f0f\u4e8b\u52a1\u672a\u56de\u6eda&#034;);<br \/>\n        } catch (Throwable t) {<br \/>\n            Assert.isTrue(true, &#034;\u5206\u5e03\u5f0f\u4e8b\u52a1\u5f02\u5e38\u56de\u6eda&#034;);<br \/>\n        }<br \/>\n        String txId &#061; (String) map.get(&#034;xid&#034;);<br \/>\n        System.out.println(txId);<br \/>\n        System.out.println(&#034;transaction rollback demo finish.&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>public class TccTransactionService {<br \/>\n    private TccActionOne tccActionOne;<br \/>\n    private TccActionTwo tccActionTwo;<\/p>\n<p>    \/\/\u63d0\u4ea4\u5206\u5e03\u5f0f\u4e8b\u52a1<br \/>\n    &#064;GlobalTransactional<br \/>\n    public String doTransactionCommit() {<br \/>\n        \/\/\u7b2c\u4e00\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        boolean result &#061; tccActionOne.prepare(null, 1);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionOne failed.&#034;);<br \/>\n        }<br \/>\n        List list &#061; new ArrayList();<br \/>\n        list.add(&#034;c1&#034;);<br \/>\n        list.add(&#034;c2&#034;);<br \/>\n        \/\/\u7b2c\u4e8c\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        result &#061; tccActionTwo.prepare(null, &#034;two&#034;, list);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionTwo failed.&#034;);<br \/>\n        }<br \/>\n        return RootContext.getXID();<br \/>\n    }<\/p>\n<p>    \/\/\u56de\u6eda\u5206\u5e03\u5f0f\u4e8b\u52a1<br \/>\n    &#064;GlobalTransactional<br \/>\n    public String doTransactionRollback(Map map) {<br \/>\n        \/\/\u7b2c\u4e00\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        boolean result &#061; tccActionOne.prepare(null, 1);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionOne failed.&#034;);<br \/>\n        }<br \/>\n        List list &#061; new ArrayList();<br \/>\n        list.add(&#034;c1&#034;);<br \/>\n        list.add(&#034;c2&#034;);<br \/>\n        \/\/\u7b2c\u4e8c\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        result &#061; tccActionTwo.prepare(null, &#034;two&#034;, list);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionTwo failed.&#034;);<br \/>\n        }<br \/>\n        map.put(&#034;xid&#034;, RootContext.getXID());<br \/>\n        throw new RuntimeException(&#034;transacton rollback&#034;);<br \/>\n    }<\/p>\n<p>    public void setTccActionOne(TccActionOne tccActionOne) {<br \/>\n        this.tccActionOne &#061; tccActionOne;<br \/>\n    }<\/p>\n<p>    public void setTccActionTwo(TccActionTwo tccActionTwo) {<br \/>\n        this.tccActionTwo &#061; tccActionTwo;<br \/>\n    }<br \/>\n} <\/p>\n<p>2.Seata TCC\u6848\u4f8b\u670d\u52a1\u63d0\u4f9b\u8005\u542f\u52a8\u5206\u6790<\/p>\n<p>\u6dfb\u52a0\u4e86&#064;TwoPhaseBusinessAction\u6ce8\u89e3\u7684\u63a5\u53e3\u53d1\u5e03\u6210Dubbo\u670d\u52a1&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"448\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523101330-68304a4a6248d.png\" width=\"1080\" \/><\/p>\n<\/p>\n<p>3.&#064;TwoPhaseBusinessAction\u6ce8\u89e3\u626b\u63cf\u6e90\u7801<\/p>\n<p>(1)\u5168\u5c40\u4e8b\u52a1\u6ce8\u89e3\u626b\u63cf\u5668\u7684wrapIfNecessary()\u65b9\u6cd5\u626b\u63cfSpring Bean<\/p>\n<p>(2)TCCBeanParserUtils\u7684isTccAutoProxy()\u65b9\u6cd5\u5224\u65ad\u662f\u5426\u8981\u521b\u5efaTCC\u52a8\u6001\u4ee3\u7406<\/p>\n<\/p>\n<p>(1)\u5168\u5c40\u4e8b\u52a1\u6ce8\u89e3\u626b\u63cf\u5668\u7684wrapIfNecessary()\u65b9\u6cd5\u626b\u63cfSpring Bean<\/p>\n<p>\u5168\u5c40\u4e8b\u52a1\u6ce8\u89e3\u626b\u63cf\u5668GlobalTransactionScanner\u4f1a\u5728\u8c03\u7528initClient()\u65b9\u6cd5\u521d\u59cb\u5316Seata Client\u5ba2\u6237\u7aef\u540e&#xff0c;\u901a\u8fc7wrapIfNecessary()\u65b9\u6cd5\u626b\u63cfSpring Bean\u4e2d\u542b\u6709&#064;TwoPhaseBusinessAction\u6ce8\u89e3\u7684\u65b9\u6cd5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"929\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523101330-68304a4a9df0e.png\" width=\"1080\" \/><\/p>\n<p>\/\/AbstractAutoProxyCreator&#xff1a;Spring\u7684\u52a8\u6001\u4ee3\u7406\u81ea\u52a8\u521b\u5efa\u8005<br \/>\n\/\/ConfigurationChangeListener&#xff1a;\u5173\u6ce8\u914d\u7f6e\u53d8\u66f4\u4e8b\u4ef6\u7684\u76d1\u542c\u5668<br \/>\n\/\/InitializingBean&#xff1a;Spring Bean\u521d\u59cb\u5316\u56de\u8c03<br \/>\n\/\/ApplicationContextAware&#xff1a;\u7528\u6765\u611f\u77e5Spring\u5bb9\u5668<br \/>\n\/\/DisposableBean&#xff1a;\u652f\u6301\u53ef\u629b\u5f03Bean<br \/>\npublic class GlobalTransactionScanner extends AbstractAutoProxyCreator<br \/>\n        implements ConfigurationChangeListener, InitializingBean, ApplicationContextAware, DisposableBean {<br \/>\n    &#8230;<\/p>\n<p>    \/\/InitializingBean\u63a5\u53e3\u7684\u56de\u8c03\u65b9\u6cd5<br \/>\n    \/\/Spring\u5bb9\u5668\u542f\u52a8\u548c\u521d\u59cb\u5316\u5b8c\u6bd5\u540e&#xff0c;\u4f1a\u8c03\u7528\u5982\u4e0b\u7684afterPropertiesSet()\u65b9\u6cd5\u8fdb\u884c\u56de\u8c03<br \/>\n    &#064;Override<br \/>\n    public void afterPropertiesSet() {<br \/>\n        \/\/\u662f\u5426\u7981\u7528\u4e86\u5168\u5c40\u4e8b\u52a1&#xff0c;\u9ed8\u8ba4\u662ffalse<br \/>\n        if (disableGlobalTransaction) {<br \/>\n            if (LOGGER.isInfoEnabled()) {<br \/>\n                LOGGER.info(&#034;Global transaction is disabled.&#034;);<br \/>\n            }<br \/>\n            ConfigurationCache.addConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, (ConfigurationChangeListener)this);<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/\u901a\u8fc7CAS\u64cd\u4f5c\u786e\u4fddinitClient()\u521d\u59cb\u5316\u52a8\u4f5c\u4ec5\u4ec5\u6267\u884c\u4e00\u6b21<br \/>\n        if (initialized.compareAndSet(false, true)) {<br \/>\n            \/\/initClient()\u65b9\u6cd5\u4f1a\u5bf9Seata Client\u8fdb\u884c\u521d\u59cb\u5316&#xff0c;\u6bd4\u5982\u548cSeata Server\u5efa\u7acb\u957f\u8fde\u63a5<br \/>\n            \/\/seata-samples\u7684tcc\u6a21\u5757\u7684seata-tcc.xml\u914d\u7f6e\u6587\u4ef6\u91cc\u90fd\u914d\u7f6e\u4e86GlobalTransactionScanner\u8fd9\u4e2aBean<br \/>\n            \/\/\u800cGlobalTransactionScanner\u8fd9\u4e2aBean\u4f34\u968f\u7740Spring\u5bb9\u5668\u7684\u521d\u59cb\u5316\u5b8c\u6bd5&#xff0c;\u90fd\u4f1a\u56de\u8c03\u5176\u521d\u59cb\u5316\u903b\u8f91initClient()<br \/>\n            initClient();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/initClient()\u662f\u6838\u5fc3\u65b9\u6cd5&#xff0c;\u8d1f\u8d23\u5bf9Seata Client\u5ba2\u6237\u7aef\u8fdb\u884c\u521d\u59cb\u5316<br \/>\n    private void initClient() {<br \/>\n        if (LOGGER.isInfoEnabled()) {<br \/>\n            LOGGER.info(&#034;Initializing Global Transaction Clients &#8230; &#034;);<br \/>\n        }<br \/>\n        if (DEFAULT_TX_GROUP_OLD.equals(txServiceGroup)) {<br \/>\n            LOGGER.warn(&#034;&#8230;&#034;, DEFAULT_TX_GROUP_OLD, DEFAULT_TX_GROUP);<br \/>\n        }<br \/>\n        if (StringUtils.isNullOrEmpty(applicationId) || StringUtils.isNullOrEmpty(txServiceGroup)) {<br \/>\n            throw new IllegalArgumentException(String.format(&#034;applicationId: %s, txServiceGroup: %s&#034;, applicationId, txServiceGroup));<br \/>\n        }<\/p>\n<p>        \/\/\u5bf9\u4e8eSeata Client\u6765\u8bf4&#xff0c;\u6700\u91cd\u8981\u7684\u7ec4\u4ef6\u6709\u4e24\u4e2a&#xff1a;<br \/>\n        \/\/\u4e00\u4e2a\u662fTM&#xff0c;\u5373Transaction Manager&#xff0c;\u7528\u6765\u7ba1\u7406\u5168\u5c40\u4e8b\u52a1<br \/>\n        \/\/\u4e00\u4e2a\u662fRM&#xff0c;\u5373Resource Manager&#xff0c;\u7528\u6765\u7ba1\u7406\u5404\u5206\u652f\u4e8b\u52a1\u7684\u6570\u636e\u6e90<\/p>\n<p>        \/\/init TM<br \/>\n        \/\/TMClient.init()\u4f1a\u5bf9\u5ba2\u6237\u7aef\u7684TM\u5168\u5c40\u4e8b\u52a1\u7ba1\u7406\u5668\u8fdb\u884c\u521d\u59cb\u5316<br \/>\n        TMClient.init(applicationId, txServiceGroup, accessKey, secretKey);<br \/>\n        if (LOGGER.isInfoEnabled()) {<br \/>\n            LOGGER.info(&#034;Transaction Manager Client is initialized. applicationId[{}] txServiceGroup[{}]&#034;, applicationId, txServiceGroup);<br \/>\n        }<\/p>\n<p>        \/\/init RM<br \/>\n        \/\/RMClient.init()\u4f1a\u5bf9\u5ba2\u6237\u7aef\u7684RM\u5206\u652f\u4e8b\u52a1\u8d44\u6e90\u7ba1\u7406\u5668\u8fdb\u884c\u521d\u59cb\u5316<br \/>\n        RMClient.init(applicationId, txServiceGroup);<br \/>\n        if (LOGGER.isInfoEnabled()) {<br \/>\n            LOGGER.info(&#034;Resource Manager is initialized. applicationId[{}] txServiceGroup[{}]&#034;, applicationId, txServiceGroup);<br \/>\n        }<\/p>\n<p>        if (LOGGER.isInfoEnabled()) {<br \/>\n            LOGGER.info(&#034;Global Transaction Clients are initialized. &#034;);<br \/>\n        }<\/p>\n<p>        \/\/\u6ce8\u518cSpring\u5bb9\u5668\u88ab\u9500\u6bc1\u65f6\u7684\u56de\u8c03\u94a9\u5b50&#xff0c;\u91ca\u653eTM\u548cRM\u4e24\u4e2a\u7ec4\u4ef6\u7684\u4e00\u4e9b\u8d44\u6e90<br \/>\n        registerSpringShutdownHook();<br \/>\n    }<\/p>\n<p>    \/\/The following will be scanned, and added corresponding interceptor:<br \/>\n    \/\/\u6dfb\u52a0\u4e86\u5982\u4e0b\u6ce8\u89e3\u7684\u65b9\u6cd5\u4f1a\u88ab\u626b\u63cf\u5230&#xff0c;\u7136\u540e\u65b9\u6cd5\u4f1a\u6dfb\u52a0\u76f8\u5e94\u7684\u62e6\u622a\u5668\u8fdb\u884c\u62e6\u622a<\/p>\n<p>    \/\/TM:<br \/>\n    \/\/&#064;see io.seata.spring.annotation.GlobalTransactional \/\/ TM annotation<br \/>\n    \/\/Corresponding interceptor:<br \/>\n    \/\/&#064;see io.seata.spring.annotation.GlobalTransactionalInterceptor#handleGlobalTransaction(MethodInvocation, AspectTransactional) \/\/ TM handler<\/p>\n<p>    \/\/GlobalLock:<br \/>\n    \/\/&#064;see io.seata.spring.annotation.GlobalLock \/\/ GlobalLock annotation<br \/>\n    \/\/Corresponding interceptor:<br \/>\n    \/\/&#064;see io.seata.spring.annotation.GlobalTransactionalInterceptor# handleGlobalLock(MethodInvocation, GlobalLock)  \/\/ GlobalLock handler<\/p>\n<p>    \/\/TCC mode:<br \/>\n    \/\/&#064;see io.seata.rm.tcc.api.LocalTCC \/\/ TCC annotation on interface<br \/>\n    \/\/&#064;see io.seata.rm.tcc.api.TwoPhaseBusinessAction \/\/ TCC annotation on try method<br \/>\n    \/\/&#064;see io.seata.rm.tcc.remoting.RemotingParser \/\/ Remote TCC service parser<br \/>\n    \/\/Corresponding interceptor:<br \/>\n    \/\/&#064;see io.seata.spring.tcc.TccActionInterceptor \/\/ the interceptor of TCC mode<\/p>\n<p>    &#064;Override<br \/>\n    \/\/\u7531\u4e8eGlobalTransactionScanner\u7ee7\u627f\u4e86Spring\u63d0\u4f9b\u7684AbstractAutoProxyCreator&#xff0c;<br \/>\n    \/\/\u6240\u4ee5Spring\u4f1a\u628aSpring Bean\u4f20\u9012\u7ed9GlobalTransactionScanner.wrapIfNecessary()\u8fdb\u884c\u5224\u65ad&#xff1b;<br \/>\n    \/\/\u8ba9GlobalTransactionScanner\u6765\u51b3\u5b9a\u662f\u5426\u8981\u6839\u636eBean\u7684Class\u3001\u6216\u8005Bean\u7684\u65b9\u6cd5\u662f\u5426\u6709\u4e0a\u8ff0\u6ce8\u89e3&#xff0c;<br \/>\n    \/\/\u4ece\u800c\u51b3\u5b9a\u662f\u5426\u9700\u8981\u9488\u5bf9Bean\u7684Class\u521b\u5efa\u52a8\u6001\u4ee3\u7406&#xff0c;\u6765\u5bf9\u6dfb\u52a0\u4e86\u6ce8\u89e3\u7684\u65b9\u6cd5\u8fdb\u884c\u62e6\u622a&#xff1b;<br \/>\n    protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {<br \/>\n        \/\/do checkers<br \/>\n        if (!doCheckers(bean, beanName)) {<br \/>\n            return bean;<br \/>\n        }<\/p>\n<p>        try {<br \/>\n            synchronized (PROXYED_SET) {<br \/>\n                if (PROXYED_SET.contains(beanName)) {<br \/>\n                    return bean;<br \/>\n                }<br \/>\n                interceptor &#061; null;<\/p>\n<p>                \/\/check TCC proxy<br \/>\n                \/\/\u5224\u65ad\u4f20\u9012\u8fdb\u6765\u7684Bean\u662f\u5426\u662fTCC\u52a8\u6001\u4ee3\u7406<br \/>\n                \/\/\u670d\u52a1\u542f\u52a8\u65f6\u4f1a\u628a\u6240\u6709\u7684Bean\u4f20\u9012\u8fdb\u6765\u8fd9\u4e2awrapIfNecessary()&#xff0c;\u68c0\u67e5\u8fd9\u4e2aBean\u662f\u5426\u9700\u8981\u521b\u5efaTCC\u52a8\u6001\u4ee3\u7406<br \/>\n                if (TCCBeanParserUtils.isTccAutoProxy(bean, beanName, applicationContext)) {<br \/>\n                    \/\/init tcc fence clean task if enable useTccFence<br \/>\n                    TCCBeanParserUtils.initTccFenceCleanTask(TCCBeanParserUtils.getRemotingDesc(beanName), applicationContext);<br \/>\n                    \/\/TCC interceptor, proxy bean of sofa:reference\/dubbo:reference, and LocalTCC<br \/>\n                    interceptor &#061; new TccActionInterceptor(TCCBeanParserUtils.getRemotingDesc(beanName));<br \/>\n                    ConfigurationCache.addConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, (ConfigurationChangeListener)interceptor);<br \/>\n                } else {<br \/>\n                    \/\/\u83b7\u53d6\u76ee\u6807class\u7684\u63a5\u53e3<br \/>\n                    Class&lt;?&gt; serviceInterface &#061; SpringProxyUtils.findTargetClass(bean);<br \/>\n                    Class&lt;?&gt;[] interfacesIfJdk &#061; SpringProxyUtils.findInterfaces(bean);<\/p>\n<p>                    \/\/existsAnnotation()\u65b9\u6cd5\u4f1a\u5224\u65adBean\u7684Class\u6216Method\u662f\u5426\u6dfb\u52a0\u4e86&#064;GlobalTransactional\u7b49\u6ce8\u89e3<br \/>\n                    if (!existsAnnotation(new Class[]{serviceInterface}) &amp;&amp; !existsAnnotation(interfacesIfJdk)) {<br \/>\n                        return bean;<br \/>\n                    }<\/p>\n<p>                    if (globalTransactionalInterceptor &#061;&#061; null) {<br \/>\n                        \/\/\u6784\u5efa\u4e00\u4e2aGlobalTransactionalInterceptor&#xff0c;\u5373\u5168\u5c40\u4e8b\u52a1\u6ce8\u89e3\u7684\u62e6\u622a\u5668<br \/>\n                        globalTransactionalInterceptor &#061; new GlobalTransactionalInterceptor(failureHandlerHook);<br \/>\n                        ConfigurationCache.addConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, (ConfigurationChangeListener)globalTransactionalInterceptor);<br \/>\n                    }<br \/>\n                    interceptor &#061; globalTransactionalInterceptor;<br \/>\n                }<\/p>\n<p>                LOGGER.info(&#034;Bean[{}] with name [{}] would use interceptor [{}]&#034;, bean.getClass().getName(), beanName, interceptor.getClass().getName());<br \/>\n                if (!AopUtils.isAopProxy(bean)) {\/\/\u5982\u679c\u8fd9\u4e2aBean\u5e76\u4e0d\u662fAOP\u4ee3\u7406<br \/>\n                    \/\/\u63a5\u4e0b\u6765\u4f1a\u57fa\u4e8eSpring\u7684AbstractAutoProxyCreator\u521b\u5efa\u9488\u5bf9\u76ee\u6807Bean\u63a5\u53e3\u7684\u52a8\u6001\u4ee3\u7406<br \/>\n                    \/\/\u8fd9\u6837\u540e\u7eed\u8c03\u7528\u5230\u76ee\u6807Bean\u7684\u65b9\u6cd5&#xff0c;\u5c31\u4f1a\u8c03\u7528\u5230TccActionInterceptor\u62e6\u622a\u5668<br \/>\n                    bean &#061; super.wrapIfNecessary(bean, beanName, cacheKey);<br \/>\n                } else {<br \/>\n                    AdvisedSupport advised &#061; SpringProxyUtils.getAdvisedSupport(bean);<br \/>\n                    Advisor[] advisor &#061; buildAdvisors(beanName, getAdvicesAndAdvisorsForBean(null, null, null));<br \/>\n                    int pos;<br \/>\n                    for (Advisor avr : advisor) {<br \/>\n                        \/\/Find the position based on the advisor&#039;s order, and add to advisors by pos<br \/>\n                        pos &#061; findAddSeataAdvisorPosition(advised, avr);<br \/>\n                        advised.addAdvisor(pos, avr);<br \/>\n                    }<br \/>\n                }<br \/>\n                PROXYED_SET.add(beanName);<br \/>\n                return bean;<br \/>\n            }<br \/>\n        } catch (Exception exx) {<br \/>\n            throw new RuntimeException(exx);<br \/>\n        }<br \/>\n    }<br \/>\n    &#8230;<br \/>\n} <\/p>\n<p>(2)TCCBeanParserUtils\u7684isTccAutoProxy()\u65b9\u6cd5\u5224\u65ad\u662f\u5426\u8981\u521b\u5efaTCC\u52a8\u6001\u4ee3\u7406<\/p>\n<p>public class TCCBeanParserUtils {<br \/>\n    private TCCBeanParserUtils() {<br \/>\n    }<\/p>\n<p>    \/\/is auto proxy TCC bean<br \/>\n    public static boolean isTccAutoProxy(Object bean, String beanName, ApplicationContext applicationContext) {<br \/>\n        boolean isRemotingBean &#061; parserRemotingServiceInfo(bean, beanName);<br \/>\n        \/\/get RemotingBean description<br \/>\n        RemotingDesc remotingDesc &#061; DefaultRemotingParser.get().getRemotingBeanDesc(beanName);<br \/>\n        \/\/is remoting bean<br \/>\n        if (isRemotingBean) {<br \/>\n            if (remotingDesc !&#061; null &amp;&amp; remotingDesc.getProtocol() &#061;&#061; Protocols.IN_JVM) {<br \/>\n                \/\/LocalTCC<br \/>\n                \/\/\u521b\u5efa\u4e00\u4e2alocal tcc\u4ee3\u7406<br \/>\n                return isTccProxyTargetBean(remotingDesc);<br \/>\n            } else {<br \/>\n                \/\/sofa:reference \/ dubbo:reference, factory bean<br \/>\n                return false;<br \/>\n            }<br \/>\n        } else {<br \/>\n            if (remotingDesc &#061;&#061; null) {<br \/>\n                \/\/check FactoryBean<br \/>\n                if (isRemotingFactoryBean(bean, beanName, applicationContext)) {<br \/>\n                    remotingDesc &#061; DefaultRemotingParser.get().getRemotingBeanDesc(beanName);<br \/>\n                    return isTccProxyTargetBean(remotingDesc);<br \/>\n                } else {<br \/>\n                    return false;<br \/>\n                }<br \/>\n            } else {<br \/>\n                return isTccProxyTargetBean(remotingDesc);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    &#8230;<\/p>\n<p>    \/\/is TCC proxy-bean\/target-bean: LocalTCC , the proxy bean of sofa:reference\/dubbo:reference<br \/>\n    public static boolean isTccProxyTargetBean(RemotingDesc remotingDesc) {<br \/>\n        if (remotingDesc &#061;&#061; null) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        \/\/check if it is TCC bean<br \/>\n        boolean isTccClazz &#061; false;<br \/>\n        \/\/\u9488\u5bf9\u6211\u4eec\u7684class\u62ff\u5230\u4e00\u4e2a\u63a5\u53e3class<br \/>\n        Class&lt;?&gt; tccInterfaceClazz &#061; remotingDesc.getInterfaceClass();<br \/>\n        \/\/\u83b7\u53d6\u6211\u4eec\u7684\u63a5\u53e3\u91cc\u5b9a\u4e49\u7684\u6240\u6709\u7684\u65b9\u6cd5<br \/>\n        Method[] methods &#061; tccInterfaceClazz.getMethods();<\/p>\n<p>        TwoPhaseBusinessAction twoPhaseBusinessAction;<\/p>\n<p>        \/\/\u904d\u5386\u6240\u6709\u7684\u65b9\u6cd5<br \/>\n        for (Method method : methods) {<br \/>\n            \/\/\u83b7\u53d6\u7684\u65b9\u6cd5\u662f\u5426\u52a0\u4e86&#064;TwoPhaseBusinessAction\u6ce8\u89e3<br \/>\n            twoPhaseBusinessAction &#061; method.getAnnotation(TwoPhaseBusinessAction.class);<br \/>\n            if (twoPhaseBusinessAction !&#061; null) {<br \/>\n                isTccClazz &#061; true;<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n        if (!isTccClazz) {<br \/>\n            return false;<br \/>\n        }<br \/>\n        short protocols &#061; remotingDesc.getProtocol();<br \/>\n        \/\/LocalTCC<br \/>\n        if (Protocols.IN_JVM &#061;&#061; protocols) {<br \/>\n            \/\/in jvm TCC bean , AOP<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/sofa:reference \/  dubbo:reference, AOP<br \/>\n        return remotingDesc.isReference();<br \/>\n    }<br \/>\n    &#8230;<br \/>\n} <\/p>\n<p>4.Seata TCC\u6848\u4f8b\u5206\u5e03\u5f0f\u4e8b\u52a1\u5165\u53e3\u5206\u6790<\/p>\n<p>TccTransactionService\u4f5c\u4e3a\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u5165\u53e3&#xff0c;\u5176\u63d0\u4ea4\u4e8b\u52a1\u548c\u56de\u6eda\u4e8b\u52a1\u7684\u63a5\u53e3\u90fd\u4f1a\u88ab\u6dfb\u52a0\u4e0a&#064;GlobalTransactional\u6ce8\u89e3\u3002<\/p>\n<\/p>\n<p>\u6240\u4ee5\u5e94\u7528\u542f\u52a8\u65f6&#xff0c;TccTransactionService\u7684Bean\u5c31\u4f1a\u88abGlobalTransactionScanner\u626b\u63cf&#xff0c;\u7136\u540e\u5176\u4e0b\u6dfb\u52a0\u4e86&#064;GlobalTransactional\u6ce8\u89e3\u7684\u63a5\u53e3\u5c31\u4f1a\u88ab\u521b\u5efa\u52a8\u6001\u4ee3\u7406\u3002<\/p>\n<\/p>\n<p>\u5728TccTransactionService\u7684\u63d0\u4ea4\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u63a5\u53e3\u4e2d&#xff0c;\u4f1a\u5148\u540e\u8c03\u7528TccActionOne\u548cTccActionTwo\u4e24\u4e2aDubbo\u670d\u52a1\u3002\u5e76\u4e14\u5728\u8c03\u7528\u4e24\u4e2aDubbo\u670d\u52a1\u65f6&#xff0c;\u4f1a\u901a\u8fc7ApacheDubboTransactionPropagationFilter\u4f20\u9012xid\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"770\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523101331-68304a4b16ab5.png\" width=\"1080\" \/><\/p>\n<p>public class TccTransactionService {<br \/>\n    private TccActionOne tccActionOne;<br \/>\n    private TccActionTwo tccActionTwo;<\/p>\n<p>    \/\/\u63d0\u4ea4\u5206\u5e03\u5f0f\u4e8b\u52a1<br \/>\n    &#064;GlobalTransactional<br \/>\n    public String doTransactionCommit() {<br \/>\n        \/\/\u7b2c\u4e00\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        boolean result &#061; tccActionOne.prepare(null, 1);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionOne failed.&#034;);<br \/>\n        }<br \/>\n        List list &#061; new ArrayList();<br \/>\n        list.add(&#034;c1&#034;);<br \/>\n        list.add(&#034;c2&#034;);<br \/>\n        \/\/\u7b2c\u4e8c\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        result &#061; tccActionTwo.prepare(null, &#034;two&#034;, list);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionTwo failed.&#034;);<br \/>\n        }<br \/>\n        return RootContext.getXID();<br \/>\n    }<\/p>\n<p>    \/\/\u56de\u6eda\u5206\u5e03\u5f0f\u4e8b\u52a1<br \/>\n    &#064;GlobalTransactional<br \/>\n    public String doTransactionRollback(Map map) {<br \/>\n        \/\/\u7b2c\u4e00\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        boolean result &#061; tccActionOne.prepare(null, 1);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionOne failed.&#034;);<br \/>\n        }<br \/>\n        List list &#061; new ArrayList();<br \/>\n        list.add(&#034;c1&#034;);<br \/>\n        list.add(&#034;c2&#034;);<br \/>\n        \/\/\u7b2c\u4e8c\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005<br \/>\n        result &#061; tccActionTwo.prepare(null, &#034;two&#034;, list);<br \/>\n        if (!result) {<br \/>\n            throw new RuntimeException(&#034;TccActionTwo failed.&#034;);<br \/>\n        }<br \/>\n        map.put(&#034;xid&#034;, RootContext.getXID());<br \/>\n        throw new RuntimeException(&#034;transacton rollback&#034;);<br \/>\n    }<\/p>\n<p>    public void setTccActionOne(TccActionOne tccActionOne) {<br \/>\n        this.tccActionOne &#061; tccActionOne;<br \/>\n    }<\/p>\n<p>    public void setTccActionTwo(TccActionTwo tccActionTwo) {<br \/>\n        this.tccActionTwo &#061; tccActionTwo;<br \/>\n    }<br \/>\n} <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb534\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf16\u6b21\u3002\u5728TccTransactionService\u7684\u63d0\u4ea4\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u63a5\u53e3\u4e2d\uff0c\u4f1a\u5148\u540e\u8c03\u7528TccActionOne\u548cTccActionTwo\u4e24\u4e2aDubbo\u670d\u52a1\u3002\u8be5Demo\u4e2d\u4e00\u4e2a\u5206\u5e03\u5f0f\u4e8b\u52a1\u5185\u4f1a\u6709\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\uff0c\u8fd9\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\u5206\u522b\u662fTccActionOne\u548cTccActionTwo\u3002\u53e6\u5916\u4e00\u4e2a\u5e94\u7528\u4f5c\u4e3a\u4e8b\u52a1\u53d1\u8d77\u65b9\uff0c\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1\uff0c\u7136\u540e\u8c03\u7528\u7f16\u6392TCC\u53c2\u4e0e\u8005\uff0c\u6267\u884c\u8fdc\u7a0bDubbo\u670d\u52a1\u3002\u8be5\u5e94\u7528\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1\uff0c\u53d1\u8d77\u5206\u5e03\u5f0f\u4e8b\u52a1\uff0c\u8c03\u7528\u4e0a\u8ff0\u4e24\u4e2aTCC\u53c2\u4e0e\u8005\uff0c\u5185\u542bTCC\u4e8b\u52a1\u63d0\u4ea4\u573a\u666f\u548cTCC\u4e8b\u52a1\u56de\u6eda\u573a\u666f\u7684\u6f14\u793a\u3002<\/p>\n","protected":false},"author":2,"featured_media":39441,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3535,3534,664,3536],"topic":[],"class_list":{"0":"post-39444","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-seata","10":"tag-664","11":"tag-3536"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/39444.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb534\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf16\u6b21\u3002\u5728TccTransactionService\u7684\u63d0\u4ea4\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u63a5\u53e3\u4e2d\uff0c\u4f1a\u5148\u540e\u8c03\u7528TccActionOne\u548cTccActionTwo\u4e24\u4e2aDubbo\u670d\u52a1\u3002\u8be5Demo\u4e2d\u4e00\u4e2a\u5206\u5e03\u5f0f\u4e8b\u52a1\u5185\u4f1a\u6709\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\uff0c\u8fd9\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\u5206\u522b\u662fTccActionOne\u548cTccActionTwo\u3002\u53e6\u5916\u4e00\u4e2a\u5e94\u7528\u4f5c\u4e3a\u4e8b\u52a1\u53d1\u8d77\u65b9\uff0c\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1\uff0c\u7136\u540e\u8c03\u7528\u7f16\u6392TCC\u53c2\u4e0e\u8005\uff0c\u6267\u884c\u8fdc\u7a0bDubbo\u670d\u52a1\u3002\u8be5\u5e94\u7528\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1\uff0c\u53d1\u8d77\u5206\u5e03\u5f0f\u4e8b\u52a1\uff0c\u8c03\u7528\u4e0a\u8ff0\u4e24\u4e2aTCC\u53c2\u4e0e\u8005\uff0c\u5185\u542bTCC\u4e8b\u52a1\u63d0\u4ea4\u573a\u666f\u548cTCC\u4e8b\u52a1\u56de\u6eda\u573a\u666f\u7684\u6f14\u793a\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/39444.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-23T10:13:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523101330-68304a4a6248d.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39444.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/39444.html\",\"name\":\"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-23T10:13:32+00:00\",\"dateModified\":\"2025-05-23T10:13:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/39444.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/39444.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39444.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/39444.html","og_locale":"zh_CN","og_type":"article","og_title":"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb534\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf16\u6b21\u3002\u5728TccTransactionService\u7684\u63d0\u4ea4\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u63a5\u53e3\u4e2d\uff0c\u4f1a\u5148\u540e\u8c03\u7528TccActionOne\u548cTccActionTwo\u4e24\u4e2aDubbo\u670d\u52a1\u3002\u8be5Demo\u4e2d\u4e00\u4e2a\u5206\u5e03\u5f0f\u4e8b\u52a1\u5185\u4f1a\u6709\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\uff0c\u8fd9\u4e24\u4e2aTCC\u4e8b\u52a1\u53c2\u4e0e\u8005\u5206\u522b\u662fTccActionOne\u548cTccActionTwo\u3002\u53e6\u5916\u4e00\u4e2a\u5e94\u7528\u4f5c\u4e3a\u4e8b\u52a1\u53d1\u8d77\u65b9\uff0c\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1\uff0c\u7136\u540e\u8c03\u7528\u7f16\u6392TCC\u53c2\u4e0e\u8005\uff0c\u6267\u884c\u8fdc\u7a0bDubbo\u670d\u52a1\u3002\u8be5\u5e94\u7528\u4f1a\u8ba2\u9605Dubbo\u670d\u52a1\uff0c\u53d1\u8d77\u5206\u5e03\u5f0f\u4e8b\u52a1\uff0c\u8c03\u7528\u4e0a\u8ff0\u4e24\u4e2aTCC\u53c2\u4e0e\u8005\uff0c\u5185\u542bTCC\u4e8b\u52a1\u63d0\u4ea4\u573a\u666f\u548cTCC\u4e8b\u52a1\u56de\u6eda\u573a\u666f\u7684\u6f14\u793a\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/39444.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-23T10:13:32+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523101330-68304a4a6248d.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/39444.html","url":"https:\/\/www.wsisp.com\/helps\/39444.html","name":"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-23T10:13:32+00:00","dateModified":"2025-05-23T10:13:32+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/39444.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/39444.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/39444.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Seata\u6e90\u7801\u20147.Seata TCC\u6a21\u5f0f\u7684\u4e8b\u52a1\u5904\u7406\u4e00"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=39444"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/39441"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=39444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=39444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=39444"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=39444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}