- Transaction Manager for Microservices Developer Guide
- Develop Applications with XA
- Develop JAX-RS Apps with XA
- Configure JAX-RS App as Transaction Participant
- Configure JAX-RS App with Multiple XA-Compliant Resource Managers
6.11.3.2 Configure JAX-RS App with Multiple XA-Compliant Resource Managers
Use the information provided in this section to configure your JAX-RS participant applications when you use multiple XA-compliant resource managers.
- Include the MicroTx Java library file as a maven dependency in the application's
pom.xmlfile. 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
TmmLibfile.<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-jakartafile.<dependency> <groupId>com.oracle.tmm.jta</groupId> <artifactId>TmmLib-jakarta</artifactId> <version>24.2</version> </dependency>
-
- Create a
DataSourceInfoobject 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,
departmentDataSourceandcreditDataSourceare names of the XA data source that you have provided in your application's YAML file.ORCL1-8976-9776-9873andORCL2-2134-5668-8672are the resource manager IDs that you have provided respectively fordepartmentDataSourceandcreditDataSourcein your application's YAML file.
Later, you will use the
@Injectannotation to ensure that your application uses these data sources. - Enter only one of the following MicroTx client library properties for each
DataSourceInfoobject that you have created. The following example provides property value for thecreditDataSourceobject. 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
XADatasourceobject. If you are using multiple resource managers with your application, initialize theXADatasourceobject in the following way for every XA-compliant resource manager.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.TrmConfig static void initXaDataSource(XADataSource dataSource, DataSourceInfo creditDataSource)Where,
creditDataSourceis theDataSourceInfoobject 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
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 databaseIt 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.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
XADatasourceobject.oracle.tmm.jta.TrmConfig.initXaDataSource(dataSource, creditDataSource)Where,
creditDataSourceis theDataSourceInfoobject 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.
- Only for Spring Boot applications that use the JAX-RS API, perform the following tasks after registering the resource endpoint that participates in the XA transaction.
-
Register the filters and
XAResourceCallbacksfor prepare, commit, rollback as shown in the following sample code snippet.@Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { // Register the MicroTx XA resource callback which // coordinates with the transaction coordinator register(XAResourceCallbacks.class); // Register the filters for the MicroTx libraries that // intercept the JAX_RS calls and manage the XA transactions register(TrmTransactionResponseFilter.class); register(TrmTransactionRequestFilter.class); // Bind the connection ... } } - Initialize a Bean for each resource manager. The following sample code snippet describes how you can initialize two Beans, one for each resource manager that the application uses. In the following code sample,
departmentDataSourceandcreditDataSourceare names of the XA data source that you have provided in your application's YAML file. Note down the names of the data source as you will use the@Injectannotation to ensure that the participant application uses these connections.@Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { // Register the filters as shown in the previous step .... @Bean @TrmSQLConnection(name = "departmentDataSource") @Lazy @RequestScope public Connection departmentDSSqlConnectionBean(){ return new TrmConnectionFactory().getConnection("departmentDataSource"); } @Bean @TrmSQLConnection(name = "creditDataSource") @Lazy @RequestScope public Connection creditDSSqlConnectionBean(){ return new TrmConnectionFactory().getConnection("creditDataSource"); } } }
-
- 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.If you are using multiple resource managers with your application, inject a
connectionobject in the following way for every XA-compliant resource manager.@Inject @TrmSQLConnection(name = "creditDataSource") private Connection creditConnection;Where,
creditDataSourceis the value that you have provided for thedataSourceNamestring in theDataSourceInfoclass of theoracle.tmm.jta.common.DataSourceInfopackage.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
connectionobject whenever the participant service performs a DML operation.Statement stmt1 = creditConnection.createStatement(); stmt1.execute(query); stmt1.close();Where,
creditConnectionis 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 samecreditConnectionobject that is created by the MicroTx client library.Repeat this step for every resource manager.
- Save the changes.
Source code of a sample JAX-RS transaction participant application which uses the MicroTx library and XA transaction protocol is available in the department-helidon-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 JAX-RS App as Transaction Participant