Initiator configuration

Updated at:

If the initiator and participant are in different applications, the initiator application must subscribe to the participant's RPC service.

Cross-service FMT participant service subscription

  • Subscribe to a service published by SOFARPC

    <sofa:reference id="dataServiceSofaRpc" interface=com.xxx.DataService" >
    <sofa:binding.tr/>
    </sofa:reference>
  • Subscribe to a service published by Dubbo

    <dubbo:reference id="dataServiceDubbo" interface=com.xxx.DataService" />

Enable distributed transactions

  • Add the @DtxTransaction (com.alipay.sofa.dtx.client.aop.annotation.DtxTransaction) annotation to the method that requires a distributed transaction. This indicates that a distributed transaction starts within this method.

    Important

    Place the annotation before the method implementation.

  • The method can contain the following transaction operations in any number or order:

    • Perform DAO operations on local FMT participants.

    • Call a regular service published by SOFARPC or Dubbo. The service can then perform operations on a cross-service FMT participant.

If the business method returns normally, the distributed transaction is committed. If the business method throws an exception, the distributed transaction is rolled back.

Example:

public class YourClass{
    @DtxTransaction(bizType="yourbizType")
    public void yourMethod(yourParams){
        try{
            // An operation on an FMT participant data source
            DAO1;

            DAO2;

            ......

            DAO N;

            // Call the phase-one try method of a TCC participant. Pass 'null' as the first parameter.
            TCC.try();

            // A regular RPC call. The called method can access an FMT participant data source.
            dataServiceSofaRpc.method();

            // If the method returns normally, the transaction is committed.
        }catch(Throwable t){
            // If an exception occurs, the transaction is rolled back.
            throw t;
        }
    }
}
Important

The class that contains this method must be configured as a Spring Bean to allow the distributed transaction scanner to detect it.

The following table describes the properties of the @DtxTransaction annotation.

Parameter

Description

bizType

Required. This property specifies the business type. Define a custom value based on your business scenario. The length cannot exceed 30 characters.

timeout

Specifies the transaction timeout period in seconds. The default is 30 seconds. A timeout automatically triggers a transaction rollback.

isolationLevel

Specifies the isolation level for the distributed transaction. The default is READ_UNCOMMITTED. Only READ_UNCOMMITTED and READ_COMMITTED are supported.

Configure the initiator class as a bean

To allow the Spring framework to scan for the initiator's annotations, configure the initiator's class as a Spring bean. The following example uses the YourClass from the previous section:

<bean id="yourClassBean" class="com.xxx.xxx.YourClass"/>