6.8.3.4 Configure Spring REST App with a Non-XA and Non-JDBC Resource

Use the information provided in this section to configure your Spring REST 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 REST Apps.

  2. 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>
  3. Enable session affinity. See Enable Session Affinity.
  4. 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.

  5. 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();
        }
    }
  6. Save the changes.