7.10.3.4 Configure Spring Boot App with a Non-XA and Non-JDBC Resource

Use the information provided in this section to configure your Spring Boot participant applications when you use a resource that does not support XA and JDBC.

Your application can connect to multiple XA-compliant resource managers. However, only a single non-XA resource can participate in a transaction.
  1. Before you begin, ensure that you have configured property values for the MicroTx library.

    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
    • To enable the Last Resource Commit (LRC) optimization, set values for the following properties.
      spring:
        microtx:
          xa-xa-support = false
          xa-llr-support = false
          xa-lrc-support = true

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

  2. Include 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.4 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.4</version>
      </dependency>
    • When Hibernate is the JPA provider, use the microtx-spring-boot-starter and microtx-spring-boot-starter-hibernate files.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter</artifactId>
           <version>24.4</version>
      </dependency>
      
      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter-hibernate</artifactId>
           <version>24.4</version>
      </dependency>
    • When EclipseLink is the JPA provider, use the microtx-spring-boot-starter and microtx-spring-boot-starter-eclipselink files.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter</artifactId>
           <version>24.4</version>
      </dependency>
      
      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter-eclipselink</artifactId>
           <version>24.4</version>
      </dependency>
    • For JDBC applications, use the microtx-spring-boot-jaxrs-starter file.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-jaxrs-starter</artifactId>
           <version>24.4</version>
      </dependency>
    • When Hibernate is the JPA provider, use the microtx-spring-boot-jaxrs-starter and microtx-spring-boot-starter-hibernate files.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-jaxrs-starter</artifactId>
           <version>24.4</version>
      </dependency>
      
      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter-hibernate</artifactId>
           <version>24.4</version>
      </dependency>
    • When EclipseLink is the JPA provider, use the microtx-spring-boot-jaxrs-starter and microtx-spring-boot-starter-eclipselink files.

      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-jaxrs-starter</artifactId>
           <version>24.4</version>
      </dependency>
      
      <dependency>
           <groupId>com.oracle.microtx</groupId>
           <artifactId>microtx-spring-boot-starter-eclipselink</artifactId>
           <version>24.4</version>
      </dependency>
  3. Only for Spring JAX-RS applications, you must register the following filters so that the MicroTx library can intercept the JAX-RS calls and manage the transaction. You can skip this step for Spring REST applications.
    import org.glassfish.jersey.server.ResourceConfig;
    ...
    @Component
    public class Configuration extends ResourceConfig {
        public Configuration() {
    ...
          register(TrmTransactionRequestFilter.class);
          register(TrmTransactionResponseFilter.class);
          register(JaxRsXACallbackResource.class);
    }
  4. Enable session affinity. See Enable Session Affinity.
  5. Implement the NonXAResource interface.
    public class MongoDbNonXAResource implements NonXAResource {
    // Provide application-specific code for all the methods in the NonXAResource interface.
    }

    For information about the NonXAResource interface, see Transaction Manager for Microservices Java API Reference.

    If you have enabled the LRC optimization, you don't have to implement the recover() method in the NonXAResource interface as the commit() method returns NULL for commitRecord in LRC.

  6. After implementing the NonXAResource interface, import the MicroTx library files, and then produce a non-XA resource. Annotate the non-XA resource that you create with @NonXa annotation. The MicroTx library consumes the object that you annotate.

    The following example shows a sample implementation for a MongoDB resource. Create code for your application based on your business requirements. In this example, the NonXaResourceFactory class supplies the NonXAResource. It produces a non-XA resource, and then the MicroTx library consumes the non-XA resource.

    package com.oracle.mtm.sample.nonxa;
    
    import oracle.tmm.jta.nonxa.NonXAResource;
    import oracle.tmm.jta.nonxa.NonXa;
    
    import javax.enterprise.inject.Produces;
    import javax.inject.Inject;
    import javax.ws.rs.ext.Provider;
    import java.util.function.Supplier;
    
    @Provider
    public class NonXaResourceFactory implements Supplier<NonXAResource> {
    
    @Autowired(required = false)
    @Qualifier("NonXA")
    private NonXAResource trmNonXAInstance;
    
        @Produces    
        @NonXa
        public NonXAResource getNonXAResource() {
            return nonXAResource;
        }
    
      @Override
        public NonXAResource get() {
            return getNonXAResource();
        }
    }
  7. 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-mongo 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.