- Transaction Manager for Microservices Developer Guide
- Develop Applications with XA
- Develop Spring Boot Apps with XA
- Configure Spring Boot App as Transaction Participant
- Configure Spring Boot Apps with an XA-Compliant Resource Manager
7.10.3.1 Configure Spring Boot Apps with an XA-Compliant Resource Manager
Use the information provided in this section to configure your Spring Boot participant applications when you use an XA-compliant resource manager.
- Configure property values for the MicroTx client library.
The following example provides sample values for the properties. Provide the values based on your environment.
spring: microtx: participant-url: https://bookTicket-app:8081 propagation-active: true http-client-connection-pool-size: 15 xa-transaction-timeout: 60000 xa-resource-manager-id: 174A5FF2-D8B1-47B0-AF09-DA5AFECA2F61 xa-xa-support: trueEnsure that
xa-xa-supportis set totrue.For details about each property and other optional properties, see Configure Library Properties for Spring Boot Apps.
- Include MicroTx library files as a maven dependency in the Spring Boot 3.x application's
pom.xmlfile. 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.-
For JDBC applications, use the
microtx-spring-boot-starterfile.<dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter</artifactId> <version>24.4</version> </dependency> -
When Hibernate is the JPA provider, use the
microtx-spring-boot-starterandmicrotx-spring-boot-starter-hibernatefiles.<dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter</artifactId> <version>24.4</version> </dependency> <dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter-hibernate</artifactId> <version>24.4</version> </dependency> -
When EclipseLink is the JPA provider, use the
microtx-spring-boot-starterandmicrotx-spring-boot-starter-eclipselinkfiles.<dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter</artifactId> <version>24.4</version> </dependency> <dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter-eclipselink</artifactId> <version>24.4</version> </dependency>
-
For JDBC applications, use the
microtx-spring-boot-jaxrs-starterfile.<dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-jaxrs-starter</artifactId> <version>24.4</version> </dependency> -
When Hibernate is the JPA provider, use the
microtx-spring-boot-jaxrs-starterandmicrotx-spring-boot-starter-hibernatefiles.<dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-jaxrs-starter</artifactId> <version>24.4</version> </dependency> <dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter-hibernate</artifactId> <version>24.4</version> </dependency> -
When EclipseLink is the JPA provider, use the
microtx-spring-boot-jaxrs-starterandmicrotx-spring-boot-starter-eclipselinkfiles.<dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-jaxrs-starter</artifactId> <version>24.4</version> </dependency> <dependency> <groupId>com.oracle.microtx</groupId> <artifactId>microtx-spring-boot-starter-eclipselink</artifactId> <version>24.4</version> </dependency>
-
- Only for Spring JAX-RS applications, you must register the following filters so that the MicroTx library can intercept the JAX-RS calls and manage the transaction. You can skip this step for Spring REST applications.
import org.glassfish.jersey.server.ResourceConfig; ... @Component public class Configuration extends ResourceConfig { public Configuration() { ... register(TrmTransactionRequestFilter.class); register(TrmTransactionResponseFilter.class); register(JaxRsXACallbackResource.class); } - Import the
com.oracle.microtx.common.MicroTxConfigpackage.import com.oracle.microtx.common.MicroTxConfig; - Initialize an
XADatasourceobject.The MicroTx client library needs to access an
XADatasourceobject. It uses this object to createXAConnectionandXAResourceobjects to connect with a resource manager or database server. The following code describes how you can define theXADatasourceobject at the beginning of the application code when you create the connection object.class oracle.tmm.jta.MicroTxConfig static void initXaDataSource(XADataSource xaDs)For more information about
XADataSource, see https://docs.oracle.com/javase/8/docs/api/javax/sql/XADataSource.html. - In the transaction participant function or block, specify the
XADatasourceobject which is used by the MicroTx client library. Provide the credentials and other details to connect to the resource manager.//Example for a participant using an Oracle Database: OracleXADataSource dataSource = new oracle.jdbc.xa.client.OracleXADataSource(); dataSource.setURL(url); //database connection string dataSource.setUser(user); //username to access database dataSource.setPassword(password); //password to access database MicroTxConfig.initXaDataSource((XADataSource)dataSource);It is the responsibility of the application developer to ensure that an XA-compliant JDBC driver and required parameters are set up while allocating
XADataSource.The MicroTx client library uses the
XADatasourceobject to create database connections. - In the transaction participant function or block, add the following line of code only once after you have initialized the
XADatasourceobject.oracle.tmm.jta.MicroTxConfig.initXaDataSource((XADataSource)xaDs);XADatasourceis an interface defined in JTA whose implementation is provided by the JDBC driver.The MicroTx client library uses this object to connect to database to start XA transactions and perform various operations such as prepare, commit, and rollback. The MicroTx library also provides a SQL connection object to the application code to execute DML using dependency injection.
- Insert the following line in the code of the participant service so that the application uses the connection passed by the MicroTx client library. The following code in the participant application injects the
connectionobject that is created by the MicroTx client library.@Autowired @Qualifier("microTxSqlConnection") @Lazy private Connection connection; - Insert the following lines in the code of the participant service so that the service uses the injected
connectionobject whenever the participant service performs a DML operation.Statement stmt1 = connection.createStatement(); stmt1.execute(query); stmt1.close();Where,
connectionis the name of theConnectionobject that you have injected in the previous step.Insert these lines of code for every DML operation that your participant service performs. Create a new statement object, such as
stmt1orstmt2for every DML operation, but use the sameconnectionobject that is created by the MicroTx client library. - Save the changes.
Source code of a sample Spring REST transaction participant application which uses the MicroTx library and XA transaction protocol is available in the department-spring 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: Configure Spring Boot App as Transaction Participant