8.7.2 Configure Python App as Transaction Participant

You can select Flask or Django as the framework for your Python application. This section provides instructions to integrate the MicroTx library with the application code of your Python application with Flask framework.
  1. Open a terminal in the virtual environment that you have created for your Python application, and then run the following command to install the MicroTx library file for Python which is available in the installation_directory/otmm-<version>/lib/python folder.
    pip3 install tmmpy-<version>.whl
  2. Configure the property values for the MicroTx library. Create a new file and save it as tmm.properties. You must provide values for the following properties.

    The following example provides sample values for the properties. Provide the values based on your environment.

    oracle.tmm.PropagateTraceHeaders = true
    server.port = 8080
    oracle.tmm.CallbackUrl = http://localhost:{server.port}

    For details about each property, see Configure Library Properties.

    Note down the name of this file as you will have to provide this later.

  3. Import the MicroTx libraries and exceptions. You can use tcclib.exception to handle exceptions.
    from tcclib.tcc import TCCClient, Middleware, http_request, TCCConfig
    import tcclib.exception as ex
  4. Create a Flask application and a middleware object.
    The following sample code creates a Flask application named app and a middleware object. The middleware object wraps around the Flask application and intercepts all the incoming requests received by the Flask application.
    # Create an instance of the Flask class with the name of the current module.
    app = Flask(__name__)
    # Create a middleware object to wrap around the Flask application that you have created.
    # The middleware object intercepts all the incoming requests received by the Flask application.
    app.wsgi_app = middleware(app.wsgi_app)
  5. Add the following code to initialize the tccConfig object for the microservice.

    Syntax

    tccConfig = TCCConfig(filePath=<application_properties_file_path>, timeLimitInSeconds=<integer>)

    Sample

    tccConfig = TCCConfig(filePath="./tmm.properties", timeLimitInSeconds=300)

    Where,

    • ./tmm.properties is the location of the file in which you have defined values for the MicroTx library properties for the transaction participant service.
    • 300 is the time limit in seconds for the transaction participant service 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.

  6. Use the TCCConfig object that you have created earlier to register participants (reserved resource URI) to an existing TCC transaction by calling the addTccParticipant 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.