请看下面的数据同步及方案(50分)

  • 主题发起人 主题发起人 hongsen
  • 开始时间 开始时间
H

hongsen

Unregistered / Unconfirmed
GUEST, unregistred user!
数据同步
问题:
不同地方的数据库(库结构,表结构都一样)需要(最好实时)交换一些数据
方案:
(1)客户端在操纵本地数据库的同时,将需要同步的数据生成SQL语句,保存到一张专用表中。
系统设计成对本地的数据库操作都是通过SQL语句,所以保存需要同步的数据也就变成了保留SQL系列。
(2)另作一个程序,实时读取专用表中的SQL语句,发送到远程数据库中执行。
结构用MIDAS
Try
{
begin
Trans;//开始本地数据库一个事务
delete from synctable where transId= Id;//删除本地专用表中某个事务的所有SQL语句
if ClientDataSet.Execute failed;//将该事务的所有SQL语句拼成CommandText发送到远程APP SERVER执行
,如果此出异常,在App Server端已经回滚。
raise exception;
Commit;//提交本地事务
}
Except
RollBack;//回滚本地数据库操作。远程数据库操作已经回滚
end;

远程数据库操作的回滚:
OnBeforeExecute
begin
Trans;
OnAfterExecute
if success
commit;
else
rollback并填写错误标志返回给客户端。

procedure tform1.SyncTransaction;
var
seq_no:integer;
transID:string;
begin
ClientDataSet1.commandtext:='';//clear commandtext
ClientDataSet1.Open;//very important
seq_no:=getSmallestSeqNO();
while (seq_no<>-1)do
//syn table is not empty同步表不空
begin
transID:=getTransID(seq_no);
if (transID<>'')then
Form1.ClientDataSet1.commandtext:=getSqlsforTransID(transID)//取出某个事务的所有SQL
else
Form1.ClientDataSet1.commandtext:=getSqlforSeqNo(seq_no);//该事务只有一条SQL
if (trim(ClientDataSet1.commandtext)<>'')then
try
begin
Form1.ADOConnection1.begin
Trans;
//delete sqls in a transaction on local SQL SERVER
if (transID<>'')then
delLocalTransaction(transID)
else
delLocalTransaction(seq_no);
//execute sqls in a transaction on remote server
Form1.ClientDataSet1.execute;
if Form1.failed then
raise Exception.Create(errMsg);
Form1.ADOConnection1.CommitTrans;
end;
except
Form1.ADOConnection1.RollbackTrans;
showmessage('数据库操作异常');
raise ;
end;
seq_no:=getSmallestSeqNO();
end;
end;

tform1.SyncTransaction在一个记时器的OnTimer中执行,或者在一个线程对象中运行。

请大家评价一下以上的做法.
请考虑可能的问题:
(1)网络连接中断
(2)App Server关闭

同步表结构:
seq_no:顺序号(按先后执行顺序排列)
trans_id:事务标识
sqlstatement:SQL语句

 
后退
顶部