- Transaction Manager for Microservices Developer Guide
- Develop Applications with TCC
- Develop Spring Boot Apps with TCC
- Configure Spring REST App as Transaction Initiator
9.5.1 Configure Spring REST App as Transaction Initiator
- Specify property values for the MicroTx library. See Configure Library Properties.
- 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.4 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.4</version> </dependency>
- 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;
- 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
andChronoUnit.MINUTES
. Permissible values are all the enum values from thejava.time.temporal.ChronoUnit
class. See https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html.
- Create an object, of a class in the MicroTx library, 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.
Autowire the Spring Boot
RestTemplate
, provided by the MicroTx library.@Autowired @Qualifier("MicroTxTccRestTemplate") RestTemplate restTemplate;
Configure an interceptor, provided by the MicroTx library, in one of the following ways. This is required if an outgoing request is a part of a transaction or if you want to intercept an outgoing request.
- Configure an interceptor in the configuration property of the
@FeignClient
annotation as shown in the following example.@FeignClient(value = "<feign1.name>", url = "<feign1.url>", configuration = MicroTxTccFeignInterceptorConfig.class) public interface <interface_name> { // ... }
Where, you replace
<feign1.name>
and<feign1.url>
with values specific to your environment. Note down the name of the interface as you will use it later in your application code. - Configure interceptors by specifying property values as shown in the following example. Provide these values in the same file that contains values for the MicroTx client library properties.
spring.cloud.openfeign.client.config.<feign1_name>.request-interceptors[0]= com.oracle.microtx.springboot.tcc.clientinterceptor.MicroTxTccFeignInterceptorConfig
Where,
<feign1_name>
is the data for thevalue
property of the@FeignClient
annotation.
- Configure an interceptor in the configuration property of the
- 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; ... }
@TCC public class myTransactionParticipantApp { // Service code that is specific to the transaction participant service. @Autowired MicroTxTccClient tccClientService; ... }
- Save the changes.
travel-agent-springboot
folder which is located in the microtx-samples
GitHub repository. You can use this as a reference while integrating the MicroTx libraries with your application.
Parent topic: Develop Spring Boot Apps with TCC