- Transaction Manager for Microservices Developer Guide
- Develop Applications with TCC
- Develop Node.js Apps with TCC
- Configure Node.js App as Transaction Participant
8.6.2 Configure Node.js App as Transaction Participant
Before you begin, ensure that you have configured the property values for the MicroTx library.
- Add the MicroTx library for Node.js as a dependency in the
package.json
file. The library file is located in theinstallation_directory/otmm-RELEASE/otmm/nodejs
folder."dependencies": { "tmmlib-node": "file:oracle-microtx-1.0.0.tgz" }
- 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.
- 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');
- 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';
- Create a router object.Use the following code to create a router object named
svcRouter2
.const svcRouter2 = Router();
- Add the following code to initialize the
TCCConfig
object for the confirm and cancel REST API endpoints of the transaction participant service.In the following code sample, the transaction participant application exposes the
/bookings
REST API endpoint. ThesvcRouter2
is the router object that you have created in the previous step. Replace these values with the values specific to your environment.//Initialize TCCConfig object for all the endpoints which need to participant in the TCC transaction const tccConfig: TCCConfig = new TCCConfig("/bookings", svcRouter2, HttpMethod.POST, 30);
Where,
-
/bookings
is the REST API endpoint that the transaction participant service exposes. -
svcRouter2
is the router object that you have created previously.
-
- In the following code sample, the transaction participant service exposes the
/bookings/:bookingId
REST API endpoint to confirm or cancel the transaction. Replace these values with the values specific to your environment. Also ensure that these endpoints are present in the transaction participant service and the confirm and cancel logic is implemented in the code. ThedohotelBooking()
,doConfirmBooking()
, anddoCancelBooking()
methods contain the business logic for creating a resource, confirming the transaction, and canceling the transaction respectively. Ensure that the business logic is implemented in the code of the transaction participant service and the endpoints are present.You'll also mention the HTTP method that the REST API endpoint uses. MicroTx uses the
PUT
method to confirm the transaction and theDELETE
method to cancel the transaction and release the resources that were reserved for the specified resource URI.svcRouter.post('/bookings', asyncHandler(async (req: Request, res: Response) => { dohotelBooking(req, res); //app-specific code to create a resource })); svcRouter.put('/bookings/:bookingId', asyncHandler(async (req: Request, res: Response) => { doConfirmBooking(req, res); //app-specific code to confirm the transaction })); svcRouter.delete('/bookings/:bookingId', asyncHandler(async (req: Request, res: Response) => { doCancelBooking(req, res); //app-specific code to cancel the transaction }));
- Use the
TCCConfig
object that you have created earlier to register participants (reserved resource URI) to an existing TCC transaction by calling theaddTccParticipant
method with the resource URI.const bookingUri; tccConfig.addTccParticipant(bookingUri);
When this code is executed, the participant service joins an existing TCC transaction when the initiator service calls the participant service. Also the MicroTx library enlists the participant service with the URIs you provide for the confirm and cancel endpoints.
Parent topic: Develop Node.js Apps with TCC