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
Procedure
To enable replication within a transaction:
- Connect to the grid with the ifx_grid_connect() procedure.
- Create a procedure that performs the following tasks:
- Defines a data variable for the Enterprise Replication state information.
- Runs the ifx_get_erstate() function and save its result in the data variable.
- Enables replication by running the ifx_set_erstate() procedure
with an argument of
1
. - Runs the statements that you want to replicate.
- Resets the replication state to the previous value by running the ifx_set_erstate() procedure with the name of the data variable.
- Disconnect from the grid with the ifx_grid_disconnect() procedure.
- 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:
- 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.
- The xqt_low_inventory() procedure defines a data variable called curstate to hold the Enterprise Replication state information.
- The ifx_get_erstate() function obtains the Enterprise Replication state and stores it in the curstate variable. The ifx_set_state() procedure enables replication.
- The low_inventory() procedure is run.
- The replication state is reset back to its original value.
- The connection to the grid is closed by the ifx_grid_disconnect() procedure.
- 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.