6.13.2.1 Configure Node.js Apps with an XA-Compliant Resource Manager

Use the information provided in this section to configure your Node.js transaction participant applications when you use an XA-compliant resource manager.

  1. Add the MicroTx library for Node.js as a dependency in the package.json file.
    "dependencies": {
        "@oracle/microtx": "<version>"
      }

    Where, <version> is the latest version of the MicroTx library for Node.js.

  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.
    Ensure that you set the value of oracle.tmm.xa.XaSupport as true and the value of oracle.tmm.xa.LLRSupport as false.
    oracle.tmm.xa.XaSupport = true
    oracle.tmm.xa.LLRSupport = false
  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.
    import {Request, Response, Router} from 'express';
    import {XATransactionMethod, XAConfig, XADataSource, TrmXAResource} from "@oracle/microtx/xa/xa";
    import {TrmConfig} from "@oracle/microtx/util/trmutils";
    import {asyncHandler} from "@oracle/microtx/util/asynchandler";
  5. If you are using Oracle Database as the resource manager, additionally import the following library.
    import {OracleXADataSource} from "@oracle/microtx/xa/oraxa";
  6. Create a router object.
    For example, the following code creates a router object named bankSvcRouter. Provide a unique name for the router.
    const bankSvcRouter = Router();
  7. Use the following format to provide the database connection details in a parameter.
    dbConfig = export default {
    user : "database_user",
    password : "database_password",
    connectString : "database_connection_string"
    };

    Where,

    • dbConfig is the name of the parameter that you want to create.
    • database_user and database_password are the username and password to access the XA-compliant resource manager.
    • connectionString: Enter the connection string to the data store in Oracle Database.
      • If you are using a non-autonomous Oracle Database (a database that does not use a credential wallet), use the following format to enter the connection string:
        jdbc:oracle:thin:@<publicIP>:<portNumber>/<database unique name>.<host domain name>
        For example:
        jdbc:oracle:thin:@123.213.85.123:1521/CustDB_iad1vm.sub05031027070.customervcnwith.oraclevcn.com
      • If you are using Oracle Database Cloud Service with Oracle Cloud Infrastructure, see Create the Oracle Database Classic Cloud Service Connection String in Using Oracle Blockchain Platform.
      • If you are using Oracle Autonomous Transaction Processing, use the following format to enter the connection string:
        jdbc:oracle:thin:@tcps://<host>:<port>/<service_name>?wallet_location=<wallet_dir>

        You can find the required details, such as host, port, and service name in the tnsnames.ora file, which is located in folder where you have extracted the wallet.

        For example:

        jdbc:oracle:thin:@tcps://adb.us-phoenix-1.oraclecloud.com:7777/unique_connection_string_low.adb.oraclecloud.com?wallet_location=Database_Wallet
  8. Pass the parameter that contains the database connection details and create a OracleXADataSource object.
    const xaPds: XADataSource = new OracleXADataSource(dbConfig);
  9. Pass the OracleXADataSource object that you have created to the TrmXAResource.init method.
    TrmXAResource.init(xaPds);
  10. Call the getXaConnection method to initialize the database connection.
    xaPds.getXAConnection();
  11. Initialize XAConfig for all the REST API endpoints in the participant service that can participate in an XA transaction. There can be more than one endpoint methods that can participate in an XA transaction. Create an instance of XATransactionMethod for each endpoint, and then pass an array of XATransactionMethod into the XAConfig object.

    The following code sample describes how you can initialize the objects for the /deposit end point.

    
    const xaTransactionDeposit : XATransactionMethod = new XATransactionMethod("/deposit");
    const xaTransactionMethods : XATransactionMethod[] = [xaTransactionDeposit];
    const xaConfig: XAConfig = new XAConfig(bankSvcRouter, '/', xaTransactionMethods);
  12. This is setting up our interceptors in order to infect calls to these endpoints with any current global transaction. The following code sample describes how the Express.js router, bankSvcRouter, routes incoming requests for the specified endpoint, /deposit to the functions you specify.
    //This is an endpoint that can participate in an XA transaction.
    bankSvcRouter.post('/deposit', (req, resp) => {
        doDeposit(req, resp); //business logic
    });
    
    async function doDeposit(req: Request, resp: Response) {
        console.log(`Nodejs department Service deposit() called`);
    //The following sample code demonstrates how you can use the connection object within your business logic. 
        let amount = 10;
        if (req.query.amount != null && typeof req.query.amount === 'string') {
            amount = parseInt(req.query.amount, 10);
        }
        // XA connection pool is created and managed by the MicroTx library
        // and is present in the context property of req object. 
        // This is available on endpoints that are part of a XA transaction.
        try {
            await req.context.xaConnection.connection.execute('UPDATE accounts SET amount = amount + :1 where account_id = :2', [amount, req.params.id]);
            resp.status(200).send();
        } catch (e: any) {
            resp.status(500).send();
        }
    }

Source code of a sample Spring REST transaction participant application which uses the MicroTx library and XA transaction protocol is available in the department 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.