7.13.2.1 XA準拠リソース・マネージャを使用するNode.jsアプリケーションの構成

XA準拠のリソース・マネージャを使用する場合は、この項に記載されている情報を使用してNode.jsトランザクション参加側アプリケーションを構成します。

  1. Node.jsのMicroTxライブラリをpackage.jsonファイルに依存関係として追加します。
    "dependencies": {
        "@oracle/microtx": "<version>"
      }

    ここで、<version>は、Node.jsのMicroTxライブラリの最新バージョンです。

  2. MicroTxライブラリのプロパティ値を構成します。「JAX-RSおよびNode.jsアプリケーションのライブラリ・プロパティの構成」を参照してください。Node.jsアプリケーションのロギングを有効にするには、追加のプロパティを設定する必要があります。「MicroTx Node.jsライブラリのログの有効化」を参照してください。
    oracle.tmm.xa.XaSupportの値がtrueoracle.tmm.xa.LLRSupportの値がfalseに設定されていることを確認します。
    oracle.tmm.xa.XaSupport = true
    oracle.tmm.xa.LLRSupport = false
  3. プロパティ値を定義したtmm.propertiesファイルを渡して、マイクロサービスのMicroTxライブラリ・プロパティを構成します。
    TrmConfig.init('./tmm.properties');
  4. MicroTxライブラリをインポートします。
    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. リソース・マネージャとしてOracle Databaseを使用している場合は、さらに次のライブラリをインポートします。
    import {OracleXADataSource} from "@oracle/microtx/xa/oraxa";
  6. ルーター・オブジェクトを作成します。
    たとえば、次のコードでは、bankSvcRouterという名前のルーター・オブジェクトが作成されます。一意のルーター名を指定します。
    const bankSvcRouter = Router();
  7. 次の形式を使用して、データベース接続の詳細をパラメータに指定します。
    dbConfig = export default {
    user : "database_user",
    password : "database_password",
    connectString : "database_connection_string"
    };

    説明

    • dbConfigは、作成するパラメータの名前です。
    • database_userおよびdatabase_passwordは、XA準拠のリソース・マネージャにアクセスするためのユーザー名とパスワードです。
    • connectionString: Oracle Databaseのデータ・ストアの接続文字列を入力します。
      • 非自律型Oracle Database (資格証明ウォレットを使用しないデータベース)を使用している場合は、次の形式を使用して接続文字列を入力します:
        jdbc:oracle:thin:@<publicIP>:<portNumber>/<database unique name>.<host domain name>
        たとえば:
        jdbc:oracle:thin:@123.213.85.123:1521/CustDB_iad1vm.sub05031027070.customervcnwith.oraclevcn.com
      • Oracle Database Cloud ServiceとOracle Cloud Infrastructureを一緒に使用している場合は、『Oracle Blockchain Platformの使用』Oracle Database Classic Cloud Service接続文字列の作成を参照してください。
      • Oracle Autonomous Transaction Processingを使用している場合は、次の形式を使用して接続文字列を入力します:
        jdbc:oracle:thin:@tcps://<host>:<port>/<service_name>?wallet_location=<wallet_dir>

        必要な詳細(ホスト、ポート、サービス名など)は、ウォレットを抽出したフォルダにあるtnsnames.oraファイルで確認できます。

        たとえば:

        jdbc:oracle:thin:@tcps://adb.us-phoenix-1.oraclecloud.com:7777/unique_connection_string_low.adb.oraclecloud.com?wallet_location=Database_Wallet
  8. データベース接続の詳細を含むパラメータを渡し、OracleXADataSourceオブジェクトを作成します。
    const xaPds: XADataSource = new OracleXADataSource(dbConfig);
  9. 作成したOracleXADataSourceオブジェクトをTrmXAResource.initメソッドに渡します。
    TrmXAResource.init(xaPds);
  10. getXaConnectionメソッドをコールして、データベース接続を初期化します。
    xaPds.getXAConnection();
  11. XAトランザクションに参加できる参加側サービス内のすべてのREST APIエンドポイントでXAConfigを初期化します。XAトランザクションに参加できるエンドポイント・メソッドは複数存在できます。エンドポイントごとにXATransactionMethodのインスタンスを作成し、XATransactionMethodの配列をXAConfigオブジェクトに渡します。

    次のコード・サンプルでは、/depositエンド・ポイントのオブジェクトを初期化する方法を示しています。

    
    const xaTransactionDeposit : XATransactionMethod = new XATransactionMethod("/deposit");
    const xaTransactionMethods : XATransactionMethod[] = [xaTransactionDeposit];
    const xaConfig: XAConfig = new XAConfig(bankSvcRouter, '/', xaTransactionMethods);
  12. ここではインターセプタを設定して、現在のグローバル・トランザクションで、これらのエンドポイントにコールを関与させるようにします。次のコード・サンプルでは、Express.jsルーターbankSvcRouterが、指定したエンドポイント/depositの受信リクエストを、指定した関数にルーティングする方法を示しています。
    //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();
        }
    }

MicroTxライブラリおよびXAトランザクション・プロトコルを使用するサンプルSpring RESTトランザクション参加側アプリケーションのソース・コードは、microtx-samples GitHubリポジトリにあるdepartmentフォルダにあります。これは、MicroTxライブラリとアプリケーションの統合時に参照として使用できます。