當您在使用 GTS 時,需要關注相關的注意事項。

GTS 事務分組應與業務系統在同一個地域

GTS 不支持跨地域訪問。創建事務分組時,需要選擇和業務系統相同的地域,否則使用時會顯示連不上 GTS Server。

GTS 注解方法的調用

建議在當前方法中直接實例化 @TxcTransaction 注解的方法類,并直接調用,保證 GTS 事務生效,且代碼直觀。

GTS 注解方法的調用包含以下三點注意事項:

  • 帶有 @TxcTransaction 注解的方法可以在其他的類中被調用,但需要顯式的指定的 Spring bean 實例。
  • 如果 @TxcTransaction 注解方法在本類的其他方法中被調用,需要顯式指定實例。
  • 開啟事務只能直接調用使用 Spring bean 實例化的類的@TxcTransaction注解方法。

示例如下。

class SampleClient { /* 此類已經被聲明為Spring bean */
    //此方法被聲明為GTS事務
    @TxcTransaction(timeout = 60000)
    void dataUpdate(Connection con1, Connection con2) {        
        // 操作數據源con
        update1(con1);
        update2(con2);
    }
    public void callUpdate1(Connection con1, Connection con2){
        dataUpdate(con1,con2);
    }
    public void callUpdate2(Connection con1, Connection con2, SampleClient clienttest){
        clienttest.dataUpdate(con1,con2);
    }
    public static void main(String[] args){
        SampleClient clienttest1 = (SampleClient)context.getBean("clientTest");
        Connection con1=getCon1();
        Connection con2=getCon2();
        clienttest1.dataUpdate(con1,con2);// 這個是正確的調用,最佳實踐
        SampleClient clienttest2 = new SampleClient();
        clienttest2.dataUpdate(con1,con2);// 這個是錯誤的調用,不能開啟GTS事務
        clienttest1.callUpdate1(con1,con2); // 這個是錯誤的調用,不能開啟GTS事務
        clienttest1.callUpdate2(con1,con2, clienttest1);// 這個是正確的調用,但是不推薦使用
    }
}
class UseSampleClient {
    void updateSampleClient(){
        SampleClient clienttest1 = (SampleClient)context.getBean("clientTest");
        clienttest1.dataUpdate(con1,con2);// 這個是正確的調用,最佳實踐
        SampleClient clienttest2 = new SampleClient();
        clienttest2.dataUpdate(con1,con2);// 這個是錯誤的調用,不能開啟GTS事務
    }
}           

AT 模式注意事項

AT 模式注意事項請參見AT 模式接入注意事項。

TCC 模式注意事項

TCC 模式注意事項請參見TCC 模式接入注意事項

SAE 環境注意事項

非公網測試環境(線上正式環境)需要配置 URL 參數 https://cs2.gts.aliyuncs.com。

  • 使用 txc-client.jar
    <bean class="com.taobao.txc.client.aop.TxcTransactionScaner">
            <constructor-arg value="myapp"/><!-- 應用自定義的標識 -->
            <constructor-arg value="mygroup.xxxx.xx"/><!-- 事務分組(全名) -->
            <constructor-arg type="int" value="1"/>
            <constructor-arg value="https://cs2.gts.aliyuncs.com"/>
            <property name="accessKey" value="xxx"/>
            <property name="secretKey" value="xxx"/>
    </bean>
  • 使用 txc-client-springcloud.jar

    在 properties 配置文件添加 spring.cloud.txc.url=https://cs2.gts.aliyuncs.com。