- 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 a Non-XA JDBC Resource
7.11.3.3 Configure JAX-RS App with a Non-XA JDBC Resource
Use the information provided in this section to configure your JAX-RS participant applications when you use a JDBC resource that does not support XA.
- When you use a single resource manager, provide values for all the MicroTx client library properties in a single file, such as
tmm.properties
file. Skip this step if you are using multiple resource managers for your application.Ensure that
oracle.tmm.xa.XaSupport
is set tofalse
andoracle.tmm.xa.LLRSupport
ororacle.tmm.xa.LRCSupport
is set totrue
.- To enable the Logging Last Resource (LLR) optimization, set the following values for the environment variables.
oracle.tmm.xa.XaSupport = false oracle.tmm.xa.LLRSupport = true oracle.tmm.xa.LRCSupport = false oracle.tmm.TcsConnPoolSize = 15 oracle.tmm.CallbackUrl = https://bookHotel-app:8081 oracle.tmm.PropagateTraceHeaders = true oracle.tmm.TransactionTimeout = 60000
- To enable the Last Resource Commit (LRC) optimization, set the following values for the environment variables.
oracle.tmm.xa.XaSupport = false oracle.tmm.xa.LLRSupport = false oracle.tmm.xa.LRCSupport = true oracle.tmm.TcsConnPoolSize = 15 oracle.tmm.CallbackUrl = https://bookHotel-app:8081 oracle.tmm.PropagateTraceHeaders = true oracle.tmm.TransactionTimeout = 60000
- To enable the Logging Last Resource (LLR) optimization, set the following values for the environment variables.
- If you are using a multiple resource managers with your application, complete the following steps to configure property values for the MicroTx client library. Skip this step if you are using a single resource manager.
- 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 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
- Create a
- 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.4 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.4</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.4</version> </dependency>
-
- Enable session affinity. See Enable Session Affinity.
- Initialize a
Datasource
object.The MicroTx library needs to access a data source object. It uses the data source object to create
java.sql.Connection
objects to connect with a resource manager. The following code describes how you can define a data source object.You must provide this code at the start of the application, so that the
initNonXaDataSource
method is called immediately after the server starts and before any other requests are served.-
If you are using a single resource manager with your application, initialize a data source in the following way.
class oracle.tmm.jta.TrmConfig static void initNonXaDataSource(DataSource NonXaDs)
-
If you are using multiple resource managers with your application, initialize the data source object in the following way for the Non-XA JDBC resource. A participant service can connect to multiple XA-compliant resource managers, but only one non-XA resource is supported in a transaction.
class oracle.tmm.jta.TrmConfig static void initNonXaDataSource(DataSource departmentDataSource, DataSourceInfo departmentDataSourceInfo)
Where,
dataSourceInfo
is the object that you have created in the step 2.
-
- In the transaction participant function or block, specify the
DataSource
object which is used by the MicroTx library. Provide the credentials and database driver details to connect to the resource manager. The following example shows the details that you must provide when you use MySQL database as an LLR. Similarly, you can provide credentials and database driver information for other databases.//Example for a participant using a MySQL database as resource manager this.dataSource = PoolDataSourceFactory.getPoolDataSource(); this.dataSource.setURL(url); //Database connection string this.dataSource.setUser(user); //User name to access the database this.dataSource.setPassword(password); //Password to access the database //Database driver information for the MySQL database. //Provide the JDBC driver information that is specific to your database. this.dataSource.setConnectionFactoryClassName("com.mysql.cj.jdbc.MysqlDataSource"); this.dataSource.setMaxPoolSize(15);
It is the application developer's responsibility to ensure that a database-specific JDBC driver and required parameters are set up while allocating
DataSource
.MicroTx library uses the
DataSource
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
Datasource
object. The MicroTx library uses this object to start a database transaction. The MicroTx library also provides a SQL connection object to the application code to execute DML using dependency injection.oracle.tmm.jta.TrmConfig.initNonXaDataSource((DataSource) NonXaDs);
Where,
Datasource
is an interface defined in JTA whose implementation is provided by the JDBC driver. - Insert the following line in the code of the participant service so that the application uses the connection passed by the MicroTx library. The following code in the participant application injects the
connection
object that is created by the MicroTx library.@Inject @TrmNonXASQLConnection private Connection connection;
- Insert code in the participant service so that the service uses the injected
connection
object whenever the participant service performs a DML operation. You can create code to use the injectedconnection
object based on your business scenario. Here's an example code snippet.Statement stmt1 = connection.createStatement(); stmt1.execute(query); stmt1.close();
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's created by the MicroTx library. - Only for Spring Boot applications that use the JAX-RS API, register the
XAResource
callbacks, such as prepare, commit, rollback, and various filters as shown in the following sample code snippet.@Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(XAResourceCallbacks.class); register(TrmTransactionResponseFilter.class); register(TrmTransactionRequestFilter.class); register(new AbstractBinder() { @Override protected void configure() { bindFactory(TrmXAConnectionFactory.class).to(XAConnection.class); } }); } }
This is in addition to registering the resource endpoint that participates in the XA transaction.
- 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-nonxa-ds
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