本课假定您已完成前面的所有课程。 请参见第 1 课:建立 MobiLink 统一数据库。
在本课中,您将添加以下脚本以检测和解决冲突:
upload_fetch 可使用此脚本从统一数据库的表中读取行,以进行冲突检测。
upload_update 可使用此脚本决定应如何将插入到远程数据库的数据应用到统一数据库。也可以使用 upload_update 的扩展原型检测更新冲突。 请参见upload_update 表事件。
upload_old_row_insert 使用此脚本处理远程数据库在上一次同步时获取的旧行值。 请参见upload_old_row_insert 表事件。
upload_new_row_insert 使用此脚本处理新行值(远程数据库上的更新值)。请参见upload_new_row_insert 表事件。
upload_delete 可使用此脚本处理从远程数据库删除的行。对于本教程,需将 MobiLink 服务器设置为忽略此事件。
resolve_conflict 解决冲突脚本应用业务逻辑来解决冲突。 请参见resolve_conflict 表事件。
安装冲突检测和解决脚本。
执行以下 SQL 语句:
/* upload_fetch */ CALL ml_add_table_script( 'sync_mlintro_scriptversion', 'Product', 'upload_fetch', 'SELECT name, quantity FROM Product WHERE name = {ml r.name}' ); /* upload_update */ CALL ml_add_table_script( 'sync_mlintro_scriptversion', '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( 'sync_mlintro_scriptversion', '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( 'sync_mlintro_scriptversion', '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( 'sync_mlintro_scriptversion', 'Product', 'upload_delete', '--{ml_ignore}'); /* resolve_conflict */ CALL ml_add_table_script( 'sync_mlintro_scriptversion', '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; |
在本教程中,MobiLink 服务器通过指定的 -zf 选项运行,使得服务器能够在同步期间检测到任何添加到统一数据库的新脚本。除非指定此选项,否则必须停止 MobiLink 服务器,才能将新脚本添加到统一数据库;在添加新脚本后,请重新启动服务器。
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |