8.4.2 Configure Spring REST App as Transaction Participant

  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;
  4. Inject TCC annotation in the transaction participant application code.
    To enable participant services join an existing TCC transaction, add @TCC annotation before the resource class of the transaction participant service.

    Insert the following code in the transaction participant code. In the following example, the myTransactionParticipantApp class contains code for the transaction participant service. Replace the name of the class based on your environment.

    @Path("/")
    @TCC(timeLimit = 120, timeUnit = ChronoUnit.SECONDS)
    //Add @TCC annotation so that the transaction participant service joins an existing TCC transaction
    //The transaction initiator service passes the TCC context in the request header.
    public class myTransactionParticipantApp {
        // Service code that is specific to the transaction participant service. 
    }
  5. Autowire the Spring Boot RestTemplate, provided by the MicroTx library. Use this object to call the endpoints of other TCC transaction participant services.
    @Autowired
    @Qualifier("MicroTxTccRestTemplate")
    RestTemplate restTemplate;
  6. In the transaction participant application code, inject a MicroTxTccClient object and then call the addTccParticipant(String uri) method to register a participant service with the TCC transaction. The participant service exposes a URI which MicroTx uses to confirm or cancel the transaction. MicroTx calls the PUT method to confirm the transaction and the DELETE method to cancel the transaction and release the reserved resource. Ensure that these methods are present and the confirm and cancel logic is implemented. To confirm or cancel the transaction, MicroTx sends a call to the exposed URI of all the participant services.

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

    
    @TCC
    public class myTransactionParticipantApp {
        // Service code that is specific to the transaction participant service.
    
        @Autowired
        @Qualifier("MicroTxTccRestTemplate")
        RestTemplate restTemplate;
    
        @Autowired
        MicroTxTccClient tccClientService;
    
        @POST
        //The REST endpoint of the transaction participant service.
        @Path("bookings")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response create() throws TccUnknownTransactionException
        // Business logic to create a booking.
          String bookingUri;
          // Register participant service with the TCC transaction
          tccClientService.addTccParticipant(bookingUri.toString());
        }
    
        @PUT
        @Path("bookings/{bookingId}")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response confirm() throws TccUnknownTransactionException {
        //Application-specific code to confirm the booking.
        }
     
        @DELETE
        @Path("bookings/{bookingId}")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response cancel() throws TccUnknownTransactionException {
        //Application-specific code to cancel the booking.
        }          
    }

    Where,

    • myTransactionParticipantApp is a class that contains code for the transaction participant service. This class already contains user-defined methods that the participant service uses to confirm or cancel a transaction.
    • bookings is the REST endpoint of the transaction participant service. The transaction initiator service calls this endpoint to perform a task, such as creating a hotel booking.
    • bookingUri contains the resource URI that the participant service exposes and which MicroTx uses to confirm or cancel the transaction.
    • bookingId is the unique ID of the booking that you want to confirm or cancel.
  7. Save the changes.
Ensure that you make these changes in the code of all transaction participant services.