6.9.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.

Your application can connect to multiple XA-compliant resource managers. Additionally, your application can connect to a single non-XA resource.
  1. 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 to false and oracle.tmm.xa.LLRSupport or oracle.tmm.xa.LRCSupport is set to true.

    • 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
  2. 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.
    1. 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 and creditDataSource are names of the XA data source that you have provided in your application's YAML file.
      • ORCL1-8976-9776-9873 and ORCL2-2134-5668-8672 are the resource manager IDs that you have provided respectively for departmentDataSource and creditDataSource in your application's YAML file.

      Later, you will use the @Inject annotation to ensure that your application uses these data sources.

    2. 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 the creditDataSource 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();.
  3. 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>
  4. Enable session affinity. See Enable Session Affinity.
  5. 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.

  6. 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.

  7. 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.

  8. 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;
  9. 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 injected connection 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 or stmt2 for every DML operation, but use the same connection object that's created by the MicroTx library.

  10. 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.

  11. Save the changes.