6.10.3.3 Configure Spring REST App with a Non-XA JDBC Resource

Use the information provided in this section to configure your Spring REST 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 application.yaml file. Skip this step if you are using multiple resource managers for your application.

    Ensure that xa-xa-support is set to false and xa-llr-support or xa-lrc-support is set to true.

    • To enable the Logging Last Resource (LLR) optimization, set the following values for the following properties.
      spring:
        microtx:
          xa-xa-support = false
          xa-llr-support = true
          xa-lrc-support = false
          http-client-connection-pool-size = 15
          participant-url = https://bookHotel-app:8081
          propagation-active = true
          xa-transaction-timeout = 60000
    • To enable the Last Resource Commit (LRC) optimization, set the values for the following properties.
      spring:
        microtx:
          xa-xa-support = false
          xa-llr-support = false
          xa-lrc-support = true
          http-client-connection-pool-size = 15
          participant-url = https://bookHotel-app:8081
          propagation-active = true
          xa-transaction-timeout = 60000

    For details about each property and other optional properties, see Configure Library Properties for Spring REST Apps.

  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 only one of the following 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.2 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.2</version>
      </dependency>
    • For Java Apps that use Hibernate as JPA provider, use the microtx-spring-boot-starter-hibernate library file.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter-hibernate</artifactId>
           <version>24.2</version>
      </dependency>
    • For Java Apps that use EclipseLink as JPA provider, use the microtx-spring-boot-starter-eclipselink library file.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter-eclipselink</artifactId>
           <version>24.2</version>
      </dependency>
  4. Enable session affinity. See Enable Session Affinity.
  5. Import the com.oracle.microtx.common.MicroTxNonXAConfig package.
    import com.oracle.microtx.common.MicroTxNonXAConfig;
  6. 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.MicroTxNonXAConfig
      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.MicroTxNonXAConfig
      static void initNonXaDataSource(DataSource departmentDataSource, DataSourceInfo departmentDataSourceInfo)

      Where, dataSourceInfo is the object that you have created in the step 2.

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

  8. 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.MicroTxNonXAConfig.initNonXaDataSource((DataSource) NonXaDs);

    Where, Datasource is an interface defined in JTA whose implementation is provided by the JDBC driver.

  9. 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.
    @Autowired
    @Qualifier("microTxNonXASqlConnection")
    @Lazy
    private Connection connection;
  10. 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.

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