8.6.1 Configure Node.js App as Transaction Initiator

  1. Add the MicroTx library for Node.js as a dependency in the package.json file. The library file is located in the installation_directory/otmm-RELEASE/otmm/nodejs folder.
    "dependencies": {
        "tmmlib-node": "file:oracle-microtx-1.0.0.tgz"
      }
  2. Configure the property values for the MicroTx library. See Configure Library Properties. To enable logging for Node.js applications, you must set additional properties. See Enable Logs for MicroTx Node.js Library.
  3. Configure the MicroTx library properties for the microservice by passing the tmm.properties file in which you have defined the values.
    TrmConfig.init('./tmm.properties');
  4. Import the MicroTx libraries and the express module files.
    import {HttpMethod, TrmConfig} from "tmmlib-node/util/trmutils";
    import {TCCConfig} from "tmmlib-node/tcc/tcc";
    import {NextFunction, request, Request, Response, Router} from 'express';
  5. Create a router object.
    Use the following code to create a router object named svcRouter.
    const svcRouter = Router();
  6. Add the following code to initialize the TCCConfig object for the REST endpoints of the transaction initiator service. The transaction initiator may expose many REST API endpoints, but you have to initialize TCCConfig object only for the REST API endpoints which need to participate in the TCC transaction.

    In the following code sample, the transaction initiator application exposes the /bookings REST API endpoint.

    // Initialize TCCConfig object for all the endpoints which need to participant in the TCC transaction.
    const tccConfig: TCCConfig = new TCCConfig("/bookings", svcRouter, HttpMethod.POST, 30);

    Where,

    • svcRouter is the router object that you have created in the previous step.
    • 30 is the time limit in seconds for the transaction initiator application to reserve the resources. Specify the time period as a whole number. It is the responsibility of the application developer to provide the required code to release the resources and cancel the their part of the TCC transaction after the time limit expires. Decide the time limit based on your business requirement.

    Replace these values with the values specific to your environment.

    When this code is executed, TCC transaction is initiated and MicroTx adds a header for all the outgoing requests from the REST API endpoint that you have specified.