Enabling replication within a grid transaction

You can enable replication within a transaction that is run in the context of the grid.

About this task

By default, the results of transactions run in the context of the grid are not also replicated by Enterprise Replication. In certain situations you might want to both propagate a transaction to the servers in the grid and replicate the results of the transaction.

Procedure

To enable replication within a transaction:
  1. Connect to the grid with the ifx_grid_connect() procedure.
  2. Create a procedure that performs the following tasks:
    1. Defines a data variable for the Enterprise Replication state information.
    2. Runs the ifx_get_erstate() function and save its result in the data variable.
    3. Enables replication by running the ifx_set_erstate() procedure with an argument of 1.
    4. Runs the statements that you want to replicate.
    5. Resets the replication state to the previous value by running the ifx_set_erstate() procedure with the name of the data variable.
  3. Disconnect from the grid with the ifx_grid_disconnect() procedure.
  4. Run the newly-defined procedure by using the ifx_grid_procedure() procedure.

Example

Suppose that a retail chain wants to run a procedure to create a report that populates a summary table of each store's current inventory and then replicates that summary information to a central server. A stored procedure named low_inventory() that creates a low inventory report exists on all the servers in the grid named grid1. The following example creates a new procedure named xqt_low_inventory() that enables replication for the low_inventory() procedure, and then runs the low_inventory() procedure:

EXECUTE PROCEDURE ifx_grid_connect('grid1');
CREATE PROCEDURE xqt_low_inventory()
	DEFINE curstate integer;
	EXECUTE FUNCTION ifx_get_erstate() INTO curstate;
	EXECUTE PROCEDURE ifx_set_erstate(1);
	EXECUTE PROCEDURE low_inventory();
	EXECUTE PROCEDURE ifx_set_erstate(curstate);
END PROCEDURE;
EXECUTE PROCEDURE ifx_grid_disconnect();
EXECUTE PROCEDURE ifx_grid_procedure('grid1', 'xqt_low_inventory()');

The following events occur in this example:

  1. The ifx_grid_connect() procedure connects to the grid1 grid so that the xqt_low_inventory() procedure is propagated to all the servers in the grid1 grid.
  2. The xqt_low_inventory() procedure defines a data variable called curstate to hold the Enterprise Replication state information.
  3. The ifx_get_erstate() function obtains the Enterprise Replication state and stores it in the curstate variable. The ifx_set_state() procedure enables replication.
  4. The low_inventory() procedure is run.
  5. The replication state is reset back to its original value.
  6. The connection to the grid is closed by the ifx_grid_disconnect() procedure.
  7. The ifx_grid_procedure() procedure runs the xqt_low_inventory() procedure on all the servers in the grid and the result of the low_inventory() procedure is replicated like any normal updating activity.