求助,为何数据库Transaction功能未能实现(DEPHI6.0+MYSQL)?(100分)

  • 主题发起人 主题发起人 XJYIN
  • 开始时间 开始时间
X

XJYIN

Unregistered / Unconfirmed
GUEST, unregistred user!
MySql版本为MySQl 3.23.41(For Linux),用dbexpress 连接mysql数据库.
所有Mysql中table type= BDB (支持Transaction功能);


dbconn:TSqlConnection ;
cdsUser :TSqlClientDataSet;
(CommandText='select * from h_user_table');
table fields include :
usr_id varchar(15)
usr_password varchar(32)
usr_desc varchar(40)
ds:TDataSource ;
dbgrid:TDbGrid ;


我的做法是 Mysql资料<== dbconn <== cdsUser <== DS <== dbgrid
在dbgrid 中修改资料后,按button
存盘,发现资料还是被update ,并未 roolback,
Transaction功能未能实现。


其Button code 如下:
procedure TForm1.Button1Click(Sender: TObject);
var
TD: TTransactionDesc;
int:integer;
begin
if not dbconn.InTransaction then
begin
TD.TransactionID := 1;
TD.IsolationLevel := xilREADCOMMITTED;
dbconn.StartTransaction(TD);
end;
try
if cdsuser.ChangeCount>0 then
cdsuser.ApplyUpdates(-1);
i:=strtoint('abc'); //产生异常事件,激活dbconn.roolback .
dbconn.Commit(TD);
except
dbconn.Rollback(TD);
end;
end;


但我用MySQl-Front 下如下Sql :


SET AUTOCOMMIT=0 ;
BEGIN WORK ;
update h_user_table set usr_id = 'test test'
where
usr_id = 'test'
ROLLBACK ;
SET AUTOCOMMIT=1 ;


查看资料发现却未被Update.这说明mysql数据库支持Transaction
功能。


哪位能告诉我为什么吗?[blue][/blue][black][/black]
 
dbexpress 單向的,你想roolback 就要用Tclientdataset,通過TDATAPROVIDE來做!
 
我用过Tclientdataset通過TDATAPROVIDE,Transaction功能未能实现.
怎样才能实现Transaction功能?
 
dbconn.StartTransaction(TD);
try
cdsuser.ApplyUpdates(-1); //如果这句改为SQL语句应该是可以的!
i:=strtoint('abc'); //产生异常事件,激活dbconn.roolback .
dbconn.Commit(TD);
except
dbconn.Rollback(TD);
end;
//这样可以吗? 如果把 i := strtoint('abc'); 这行去掉数据可以正常写入吗?
 
数据可以正常写入,i := strtoint('abc'); 此句是我用来测试的。
 
你把 cdsuser.ApplyUpdates(-1); //如果这句改为SQL语句
看看是否可以RollBack;
 
此问题主要是将用户修改的资料UPDATE,
如何用SQL语言将DBGRID上用户修改的资料取到?
 
最好不要在DBGrid上面直接编辑, 我感觉不好, 看来只好ApplyUpdates了!
以下是Delphi自带的例子 但是他用的是Query直接用Table, ApplyUpdates真的是不太好!
procedure TForm1.ApplyButtonClick(Sender: TObject);
begin
with CustomerQuery do
begin
Database1.StartTransaction;
try
ApplyUpdates; {try to write the updates to the database};
Database1.Commit; {on success, commit the changes};
except
Database1.Rollback; {on failure, undo the changes};
raise; {raise the exception to prevent a call to CommitUpdates!}
end;
CommitUpdates; {on success, clear the cache}
end;
end;
 
数据可以UPDATE进数据库,但是在发生异常时不能ROOLBACK.
 
DataBase.StartTransaction;
try
ApplyUpdates(-1):
DataBase.Commit;
except
CancelUpdates;
DataBase.RollBack;
end;
//这样可以吗? Except当中的代码执行了吗?

 
试过了,不行,调试时dbconn.Rollback(TD);也执行,
但是ROLLBACK并没有发生作用。
 
MySQL 本身不支持事务处理。
 
Mysql中table type= BDB (支持Transaction功能);
但我用MySQl-Front 下如下Sql :


SET AUTOCOMMIT=0 ;
BEGIN WORK ;
update h_user_table set usr_id = 'test test'
where
usr_id = 'test'
ROLLBACK ;
SET AUTOCOMMIT=1 ;


查看资料发现却未被Update.这说明mysql数据库支持Transaction
功能。
 
不是MYSQL的問題,Mysql中table type= BDB (支持Transaction功能)
我現在用BDE就可以實現.我想可能是DEPHI6中dbExpress不支持或者是個BUG.
 
多人接受答案了。
 
后退
顶部