8.4.1 Configure Spring REST App as Transaction Initiator

  1. Specify property values for the MicroTx library. See Configure Library Properties.
  2. Include the following MicroTx library file as a maven dependency in the JDBC application's pom.xml file. The following sample code is for the 24.2 release. Provide the correct version, based on the release version that you want to use. Spring Boot 3.x applications work with Java 17.
    <dependency>
         <groupId>com.oracle.microtx</groupId>
         <artifactId>microtx-spring-boot-starter</artifactId>
         <version>24.2</version>
    </dependency>
  3. Import the following packages.
    import com.oracle.microtx.tcc.MicroTxTccClient;
    import com.oracle.microtx.tcc.annotation.TCC;
    import oracle.tmm.tcc.TccParticipantStatus;
    import oracle.tmm.tcc.exception.TccException;
    import oracle.tmm.tcc.exception.TccHeuristicException;
    import oracle.tmm.tcc.exception.TccUnknownTransactionException;
    import oracle.tmm.tcc.vo.CancelResponse;
    import oracle.tmm.tcc.vo.ConfirmResponse;
    import oracle.tmm.tcc.vo.TccParticipant;
  4. Add @TCC annotation before the initiator application resource class. This initiates a new TCC transaction and adds a header for all the outgoing REST API requests from the transaction initiator.

    Use the following code to initiate a new TCC transaction when a call is made to the transaction initiator service. In the following example, the class myTransactionInitiatorApp contains the code that initiates the service. Replace the name of the class based on your environment.

    @TCC(timeLimit = 120, timeUnit = ChronoUnit.SECONDS)  //Add @TCC annotation before the initiator application resource class to start a TCC transaction
    public class myTransactionInitiatorApp {
        // Service code that is specific to the transaction initiator service.
    }

    You can specify the following optional parameters with the @TCC annotation.

    • timeLimit: Specify the time period, as a whole number, for which you want the transaction initiator service to reserve the resources. It is the responsibility of the application developer to provide the required code to release the resources and cancel the their part of the TCC transaction after the time limit expires. Decide the time limit based on your business requirement.
    • timeUnit: Specify the unit in which you have mentioned the time limit, such asChronoUnit.SECONDS and ChronoUnit.MINUTES. Permissible values are all the enum values from the java.time.temporal.ChronoUnit class. See https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html.
  5. Autowire the Spring Boot RestTemplate, provided by the MicroTx library. Use this object to call the endpoints of other TCC transaction participant services. The transaction initiator service begins the transaction. To complete the transaction, the initiator service may have to make calls to one or more participant services. While calling the participant services, use the object that you create.
    @Autowired
    @Qualifier("MicroTxTccRestTemplate")
    RestTemplate restTemplate;
  6. In the transaction initiator application code, inject a MicroTxTccClient object, and then use the injected object to confirm or cancel the transaction.

    The following code example describes the changes that you need to make to the initiator application code.

    
    @TCC
    public class myTransactionParticipantApp {
        // Service code that is specific to the transaction participant service.
    
        @Autowired
        @Qualifier("MicroTxTccRestTemplate")
        RestTemplate restTemplate;
    
        @Autowired
        MicroTxTccClient tccClientService;
        ...          
    }
  7. Save the changes.