在向统一数据库中上载行时可能会发生冲突。如果两个用户修改不同远程数据库中相同的行,则当这两行中的第二个到达 MobiLink 同步服务器时将出现冲突。使用同步脚本可以检测和解决冲突。
有关 MobiLink 冲突解决的详细信息,请参见冲突处理。
考虑两名销售员竞争的情况。Salesman1 开始有 10 件库存,后来卖出 3 件。他将他的远程数据库 remote1 上的库存更新为 7。Salesman2 卖出 4 件并将她的库存(在 Remote2 上)更新为 6。
当 remote1 使用 MobiLink 同步客户端实用程序进行同步时,统一数据库更新为 7。当 remote2 同步时会检测到冲突,因为统一数据库中的库存值已更改。
若要以编程方式解决这一冲突,需要三个行值:
统一数据库中的当前值。
在 remote1 同步之后,统一数据库中的值为 7。
Remote2 上载的新行值。
Remote2 在上一次同步期间获取的旧行值。
在这种情况下,您可以使用业务逻辑来计算新的库存值,并解决冲突:
current consolidated - (old remote - new remote) that is, 7 - (10-6) = 3
表达式 (old remote - new remote) 提供的是 Salesman2 卖出的件数,而不是库存的绝对值。
对于冲突检测和解决,请添加以下脚本:
upload_update upload_update 事件确定插入远程数据库的数据如何应用到统一数据库。您还可以使用 upload_update 的扩展原型检测更新冲突。
有关使用 upload_update 检测冲突的详细信息,请参见检测冲突。
有关 upload_update 的详细信息,请参见 upload_update 表事件。
upload_old_row_insert 使用此脚本处理远程数据库在上一次同步期间获取的旧行值。
有关 upload_old_row_insert 的详细信息,请参见 upload_old_row_insert 表事件。
upload_new_row_insert 使用此脚本处理新行值(远程数据库上的更新值)。
有关 upload_new_row_insert 的详细信息,请参见 upload_new_row_insert 表事件。
resolve_conflict 解决冲突脚本应用业务逻辑来解决冲突。
有关 resolve_conflict 的详细信息,请参见 resolve_conflict 表事件。
安装用于冲突检测和解决的脚本
启动 Interactive SQL。
在命令提示符下输入:
dbisql
即会出现 [连接] 对话框。
在 [标识] 选项卡上,在 [用户 ID] 中输入 DBA,在 [口令] 中输入 SQL。在 [数据库] 选项卡上,在 [服务器名] 中输入 cons。
安装冲突检测和解决脚本。
在 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 统一数据库。
SQL Anywhere Studio 9.0.2
版权所有 © 1989–2005 Sybase, Inc. 部分版权所有 © 2001–2005 iAnywhere Solutions, Inc. 保留所有权利。