8.6.1 Configure JAX-RS App as Transaction Initiator

Before you begin, ensure that you have configured the property values for the MicroTx library.

  1. Include the MicroTx Java library file as a maven dependency in the 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.
    • In Jakarta EE8 environments, such as Helidon 2.x, use the TmmLib file.

      <dependency>
           <groupId>com.oracle.tmm.jta</groupId>
           <artifactId>TmmLib</artifactId>
           <version>24.2</version>
      </dependency>
    • In Jakarta EE9 environments, such as Helidon 3.x applications, use the TmmLib-jakarta file.

      <dependency>
           <groupId>com.oracle.tmm.jta</groupId>
           <artifactId>TmmLib-jakarta</artifactId>
           <version>24.2</version>
      </dependency>
  2. Import the following packages.
    import oracle.tmm.tcc.TccClientService;
    import oracle.tmm.tcc.TccParticipantStatus;
    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;
  3. 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.
Source code of a sample JAX-RS transaction initiator application which uses the MicroTx library is available in the BookingResource.java file which is located in the microtx-samples GitHub repository. You can use this as a reference while integrating the MicroTx libraries with your application.