- 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 an XA-Compliant Resource Manager
6.9.3.1 Configure JAX-RS App with an XA-Compliant Resource Manager
Use the information provided in this section to configure your JAX-RS 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.
oracle.tmm.TcsConnPoolSize = 15 oracle.tmm.CallbackUrl = https://bookTicket-app:8081 oracle.tmm.PropagateTraceHeaders = true oracle.tmm.TransactionTimeout = 60000 oracle.tmm.xa.XaSupport = true
Ensure that
oracle.tmm.xa.XaSupport
is set totrue
.For details about each property and other optional properties, see Configure Library Properties.
- Include the MicroTx Java library file as a maven dependency in the 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.-
In Jakarta EE8 environments, such as Helidon 2.x, use the
TmmLib
file.<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-jakarta
file.<dependency> <groupId>com.oracle.tmm.jta</groupId> <artifactId>TmmLib-jakarta</artifactId> <version>24.2</version> </dependency>
-
- Initialize an
XADatasource
object.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.TrmConfig 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
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 TrmConfig.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
XADatasource
object to create database connections. - 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.TrmConfig.initXaDataSource((XADataSource)xaDs);
XADatasource
is 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
connection
object that is created by the MicroTx client library.@Inject @TrmSQLConnection private Connection connection;
- 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 = connection.createStatement(); stmt1.execute(query); stmt1.close();
Where,
connection
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 sameconnection
object that is created by the MicroTx client library. - 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
XAResourceCallbacks
for 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 ... } }
-
If you are using a single resource manager with your Spring Boot application, bind the
TrmXAConnectionFactory
object to anXAConnection
. Later, you will use theTrmXAConnectionFactory
object, a MicroTx connection factory object, so that MicroTx handles the connection.@Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { // Register the filters as shown in the previous step .... register(new AbstractBinder() { @Override protected void configure() { //Bind the TrmXAConnectionFactory object to an XAConnection object bindFactory(TrmXAConnectionFactory.class).to(XAConnection.class); } }); } }
-
- Save the changes.
Parent topic: Configure JAX-RS App as Transaction Participant