- 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 App with Multiple XA-Compliant Resource Managers
7.10.3.2 Configure Spring Boot App with Multiple XA-Compliant Resource Managers
Use the information provided in this section to configure your Spring Boot participant applications when you use multiple XA-compliant resource managers.
- Include MicroTx library files as a maven dependency in the Spring Boot 3.x 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.-
For JDBC applications, use the
microtx-spring-boot-starter
file.<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-starter
andmicrotx-spring-boot-starter-hibernate
files.<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-starter
andmicrotx-spring-boot-starter-eclipselink
files.<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-starter
file.<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-starter
andmicrotx-spring-boot-starter-hibernate
files.<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-starter
andmicrotx-spring-boot-starter-eclipselink
files.<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.MicroTxConfig
package.import com.oracle.microtx.common.MicroTxConfig;
- Create a
DataSourceInfo
object for each resource manager. Ensure that you provide the data source names and resource manager IDs that you have the specified in the your application's YAML file.Sample Command
DataSourceInfo departmentDataSourceInfo = new DataSourceInfo("ORCL1-8976-9776-9873"); departmentDataSourceInfo.setDataSourceName(departmentDataSource); DataSourceInfo creditDataSourceInfo = new DataSourceInfo("ORCL2-2134-5668-8672"); creditDataSourceInfo.setDataSourceName(creditDataSource);
Where,
departmentDataSource
andcreditDataSource
are names of the XA data source that you have provided in your application's YAML file.ORCL1-8976-9776-9873
andORCL2-2134-5668-8672
are the resource manager IDs that you have provided respectively fordepartmentDataSource
andcreditDataSource
in your application's YAML file.
Later, you will use the
@Inject
annotation to ensure that your application uses these data sources. - Enter only one of the following MicroTx client library properties for each
DataSourceInfo
object that you have created. The following example provides property value for thecreditDataSource
object. Similarly, you can provide property values for other resource managers. If you don't provide any value, by default the resource is considered to be XA-compliant.- For XA-compliant resources, enter:
creditDataSource.setXaSupport();
- For Oracle RAC database, enter:
creditDataSource.setRAC(true);
- For non-XA resources that use LLR optimization, enter:
creditDataSource.setLLRSupport();
- For non-XA resources that use LRC optimization, enter:
creditDataSource.setLRCSupport();
- For XA-compliant resources, enter:
- Initialize an
XADatasource
object. If you are using multiple resource managers with your application, initialize theXADatasource
object in the following way for every XA-compliant resource manager.The MicroTx client library needs to access an
XADatasource
object. It uses this object to createXAConnection
andXAResource
objects to connect with a resource manager or database server. The following code describes how you can define theXADatasource
object at the beginning of the application code when you create the connection object.class oracle.tmm.jta.MicroTxConfig static void initXaDataSource(XADataSource dataSource, DataSourceInfo creditDataSource)
Where,
creditDataSource
is theDataSourceInfo
object that you have previously created.Repeat this step for every resource manager.
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
XADatasource
object 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
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
XADatasource
object to create database connections.Repeat this step for every resource manager.
- In the transaction participant function or block, add the following line of code only once after you have initialized the
XADatasource
object.oracle.tmm.jta.MicroTxConfig.initXaDataSource(dataSource, creditDataSource)
Where,
creditDataSource
is theDataSourceInfo
object that you have previously created.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.
Repeat this step for every resource manager.
- 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
connection
object that is created by the MicroTx client library.If you are using multiple resource managers with your application, inject a
connection
object in the following way for every XA-compliant resource manager.The following example describes how you can add two connections, one for each XA-compliant resource manager. When you inject the connection, call
microTxSqlConnection
followed by the name of your data source.//Initialize the application context private ApplicationContext applicationContext; //Connection for XA-compliant resource manager 1 private Connection connection; //Connection for XA-compliant resource manager 2 private Connection creditConnection; @Autowired public AccountService(ApplicationContext applicationContext) { this.applicationContext = applicationContext; try { // Inject the connections for XA-compliant resource manager 1. // Specify the name of the data source associated with XA-compliant resource manager 1. connection = (Connection) applicationContext.getBean("microTxSqlConnection", "departmentDataSource"); // Inject the connections for XA-compliant resource manager 2. // Specify the name of the data source associated with XA-compliant resource manager 2. creditConnection = (Connection) applicationContext.getBean("microTxSqlConnection","creditDataSource"); } catch (ClassCastException ex) { LOG.info(ex.getMessage()); } }
Where,
creditDataSource
is the value that you have provided for thedataSourceName
string in theDataSourceInfo
class of theoracle.tmm.jta.common.DataSourceInfo
package.Repeat this step for every resource manager.
- Insert the following lines in the code of the participant service so that the service uses the injected
connection
object whenever the participant service performs a DML operation.Statement stmt1 = creditConnection.createStatement(); stmt1.execute(query); stmt1.close();
Where,
creditConnection
is the name of theConnection
object 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
stmt1
orstmt2
for every DML operation, but use the samecreditConnection
object that is created by the MicroTx client library.Repeat this step for every resource manager.
- 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-multiplerm
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