Conflicts arise during the upload of rows to the consolidated database. If two users modify the same row on different remote databases, a conflict is detected when the second row arrives at the MobiLink server. Using synchronization scripts you can detect and resolve conflicts.
Consider a scenario where two clients are selling up to ten items simultaneously. The first client sells three items, so they update the inventory on their client database, remote1, to indicate seven remaining items. Their database then synchronizes with the consolidated database, mlmon_db. The second client then sells four items, so they update the inventory on their remote database, remote2, to indicate six remaining items. A conflict is detected when they attempt to synchronize with mlmon_db because the inventory value has changed.
You must obtain the following row values to programmatically resolve this conflict:
The current value in the consolidated database.
The value in the consolidated database is 7 after remote1 synchronizes.
The new row value uploaded by the remote2 database.
The old row value obtained by the remote2 database during the previous synchronization.
You can use the following business logic to calculate the new inventory value and resolve the conflict:
current consolidated - (old remote - new remote) that is, 7 - (10-6) = 3 |
The (old remote - new remote) expression provides the number of items sold by second client rather than the absolute inventory value.
You add the following scripts to detect and resolve conflicts:
upload_fetch You use this script to fetch rows from a table in the consolidated database for conflict detection.
upload_update You use this script to determine how data inserted into the remote database should be applied to the consolidated database. You can also use an extended prototype of upload_update to detect update conflicts.
For more information about upload_update, see upload_update table event.
upload_old_row_insert You use this script to handle old row values obtained by the remote database during its previous synchronization.
For more information about upload_old_row_insert, see upload_old_row_insert table event.
upload_new_row_insert You use this script to handle new row values (the updated values on the remote database).
For more information about upload_new_row_insert, see upload_new_row_insert table event.
upload_delete You use this script to handle rows that are deleted from the remote database. You set the MobiLink server to ignore this event for the purpose of this tutorial.
resolve_conflict The resolve conflict script applies business logic to resolve the conflict.
For more information about resolve_conflict, see resolve_conflict table event.
Connect to your database using Interactive SQL.
You can start Interactive SQL from Sybase Central or from a command prompt.
To start Interactive SQL from Sybase Central, right-click the MLconsolidated - DBA database and click Open Interactive SQL.
To start Interactive SQL at a command prompt, run the following command:
dbisql -c "dsn=mlmon_db" |
Install the conflict detection and resolution scripts.
Run the following SQL script:
/* upload_fetch */ CALL ml_add_table_script( 'ver1', 'Product', 'upload_fetch', 'SELECT name, quantity FROM Product WHERE name = {ml r.name}' ); /* upload_update */ CALL ml_add_table_script( 'ver1', 'Product', 'upload_update', 'UPDATE Product SET quantity = {ml r.quantity}, last_modified = now() WHERE name = {ml r.name}' ); /* upload_old_row_insert */ CALL ml_add_table_script( 'ver1', 'Product', 'upload_old_row_insert', 'INSERT INTO Product_old (name,quantity,last_modified) VALUES ({ml r.name}, {ml r.quantity}, now())'); /* upload_new_row_insert */ CALL ml_add_table_script( 'ver1', 'Product', 'upload_new_row_insert', 'INSERT INTO Product_new (name,quantity,last_modified) VALUES ({ml r.name}, {ml r.quantity}, now())'); /* upload_delete */ CALL ml_add_table_script( 'ver1', 'Product', 'upload_delete', '--{ml_ignore}'); /* resolve_conflict */ CALL ml_add_table_script( 'ver1', 'Product', 'resolve_conflict', 'DECLARE @product_name VARCHAR(128); DECLARE @old_rem_val INTEGER; DECLARE @new_rem_val INTEGER; DECLARE @curr_cons_val INTEGER; DECLARE @resolved_value INTEGER; // obtain the product name SELECT name INTO @product_name FROM Product_old; // obtain the old remote value SELECT quantity INTO @old_rem_val FROM Product_old; //obtain the new remote value SELECT quantity INTO @new_rem_val FROM Product_new; // obtain the current value in cons SELECT quantity INTO @curr_cons_val FROM Product WHERE name = @product_name; // determine the resolved value SET @resolved_value = @curr_cons_val- (@old_rem_val - @new_rem_val); // update cons with the resolved value UPDATE Product SET quantity = @resolved_value WHERE name = @product_name; // clear the old and new row tables DELETE FROM Product_new; DELETE FROM Product_old'); COMMIT |
In this tutorial, the MobiLink server runs with the -zf option specified, which allows the server to detect any new scripts added to the consolidated database during a synchronization. Unless this option is specified, you must stop the MobiLink server before adding new scripts to the consolidated database; restart the server after adding the new scripts.
For more information about MobiLink conflict detection and resolution, see Handling conflicts and Detecting conflicts.
For more information about MobiLink consolidated databases, see MobiLink consolidated databases.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |