Collection Contents 上一页 下一页 PDF

MobiLink 教程

教程:编写 MobiLink 脚本和监控同步

第 6 课:为冲突检测和解决创建脚本


在向统一数据库中上载行时可能会发生冲突。如果两个用户修改不同远程数据库中相同的行,则当这两行中的第二个到达 MobiLink 同步服务器时将出现冲突。使用同步脚本可以检测和解决冲突。

有关 MobiLink 冲突解决的详细信息,请参见冲突处理

库存示例 

考虑两名销售员竞争的情况。Salesman1 开始有 10 件库存,后来卖出 3 件。他将他的远程数据库 remote1 上的库存更新为 7。Salesman2 卖出 4 件并将她的库存(在 Remote2 上)更新为 6。

当 remote1 使用 MobiLink 同步客户端实用程序进行同步时,统一数据库更新为 7。当 remote2 同步时会检测到冲突,因为统一数据库中的库存值已更改。

若要以编程方式解决这一冲突,需要三个行值:

  1. 统一数据库中的当前值。

    在 remote1 同步之后,统一数据库中的值为 7。

  2. Remote2 上载的新行值。

  3. Remote2 在上一次同步期间获取的旧行值。

在这种情况下,您可以使用业务逻辑来计算新的库存值,并解决冲突:

current consolidated - (old remote - new remote)
that is, 7 - (10-6) = 3

表达式 (old remote - new remote) 提供的是 Salesman2 卖出的件数,而不是库存的绝对值。

用于冲突检测和解决的同步脚本 

对于冲突检测和解决,请添加以下脚本:

安装用于冲突检测和解决的脚本

  1. 启动 Interactive SQL。

  2. 安装冲突检测和解决脚本。

    在 Interactive SQL 中执行以下语句:

    /* upload_update */
    call ml_add_table_script( 'ver1', 'Product',
     'upload_update',
     'UPDATE Product
       SET quantity = ?, last_modified = ?
        WHERE name = ?
        AND quantity=? AND last_modified=?' )
    go
    /* upload_old_row_insert */
    call ml_add_table_script( 'ver1', 'Product',
     'upload_old_row_insert',
     'INSERT INTO Product_old (name,quantity,last_modified)
       values (?,?,?)')
    go
    /* upload_new_row_insert */
    call ml_add_table_script( 'ver1', 'Product',
     'upload_new_row_insert',
     'INSERT INTO Product_new (name,quantity,last_modified)
       values (?,?,?)')
    go
    /* 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
     ')

完成设置 Adaptive Server Anywhere 统一数据库。

进一步阅读 

有关 MobiLink 冲突检测和解决的详细信息,请参见冲突处理

有关 MobiLink 统一数据库的详细信息,请参见 MobiLink 统一数据库


Collection Contents 上一页 下一页 PDF