Q
qiubole
Unregistered / Unconfirmed
GUEST, unregistred user!
想问一下,ADOQUERY是不是线程安全的,如果不是,我用Synchronize封装之后呢.
我用的是同一个连接,连接ACCESS数据库.
但有时会出现,数据库文件被独占.
代码如下.
TUpdateThread = class(TThread)
private
FADOQuery: TADOQuery;
FExecADO: TADOQuery;
TableA, TableB, TableC: string;
SaveADOConnection: TADOConnection;
LocalADOConnection: TADOConnection;
proceduredo
UpdateSale;
public
constructor Create(ATable, BTable, CTable: string;
LocalADO, ADOConnection: TADOConnection;
ASusped: Boolean);
destructor Destroy;
override;
procedure Execute;
override;
end;
constructor TUpdateThread.Create(ATable, BTable, CTable: string;
LocalADO, ADOConnection: TADOConnection;
ASusped: Boolean);
begin
TableA := ATable;
TableB := BTable;
TableC := CTable;
SaveADOConnection := ADOConnection;
LocalADOConnection := LocalADO;
FADOQuery := TADOQuery.Create(nil);
FExecADO := TADOQuery.Create(nil);
FADOQuery.Connection := LocalADOConnection;
FExecADO.Connection := SaveADOConnection;
Inherited Create(ASusped);
end;
destructor TUpdateThread.Destroy;
begin
FADOQuery.Free;
FExecADO.Free;
inherited;
end;
procedure TUpdateThread.DoUpdateSale;
var
tmpSQLStr: string;
begin
FADOQuery.Close;
FADOQuery.SQL.Text := 'select * from hs_000a';
FADOQuery.Open;
FExecADO.Close;
FExecADO.SQL.Text :=
'insert into ' + TableA + ' (did, dinsetcode, dno, ddate, dtime, dnum, dprice, '
+ ' dmoney_sale, dmoney_ss, dworker, dcard_no, dsale_state, '
+ ' ddiskname, dsubshop, djf) values('
+ ' id, Barcode, No, Date, time, Num, price, '
+ ' Money_Sale, Money_SS, Worker, Card_No, Sale_State, '
+ ' DiskName, SubShop, JF)';
FExecADO.Prepared := True;
while FADOQuery.RecordCount > 0do
begin
if Terminated then
Exit;
with FExecADO.Parameters, FADOQuerydo
begin
ParamByName('did').Value := FieldByName('did').AsString;
ParamByName('dbarcode').Value := FieldByName('dbarcode').AsString;
ParamByName('dNo').Value := FieldByName('dNo').AsString;
ParamByName('dDate').Value := FieldByName('DDate').AsDateTime;
ParamByName('dtime').Value := FieldByName('dtime').AsString;
ParamByName('dnum').Value := FieldByName('dnum').AsVariant;
ParamByName('dprice').Value := FieldByName('dprice').AsVariant;
ParamByName('dmoney_sale').Value := FieldByName('dmoney_sale').AsVariant;
ParamByName('dmoney_ss').Value := FieldByName('dmoney_ss').AsVariant;
ParamByName('dworker').Value := FieldByName('dworker').AsString;
ParamByName('dcard_no').Value := FieldByName('dcard_no').AsString;
ParamByName('dsale_state').Value := FieldByName('dsale_state').AsString;
ParamByName('DDiskName').Value := FieldByName('DDiskName').AsString;
ParamByName('dsale_state').Value := FieldByName('dsale_state').AsString;
ParamByName('DSubShop').Value := '0001';
ParamByName('DJF').Value := FieldByName('djf').AsFloat;
end;
// with
FExecADO.ExecSQL;
FADOQuery.Delete;
end;
end;
procedure TUpdateThread.Execute;
label Loop1;
var
tmpSQLStr: string;
tmpUpdateCount: Integer;
tmpUpdateLast: Boolean;
begin
CoInitialize(nil);
Priority := tpLowest;
FreeOnTerminate := True;
if not LocalADOConnection.Connected then
LocalADOConnection.Connected := True;
if not SaveADOConnection.Connected then
LocalADOConnection.Connected := True;
tmpUpdateCount := 0;
while Truedo
begin
if Terminated then
begin
Break;
end;
try
EnterCriticalSection(SystemSetup.FSaleUpLock);
Self.Synchronize(DoUpdateSale);
finally
LeaveCriticalSection(SystemSetup.FSaleUpLock);
end;
//如果出错,表明网络有问题,需要进行挂起。
Self.Suspend;
end;
end;
我用的是同一个连接,连接ACCESS数据库.
但有时会出现,数据库文件被独占.
代码如下.
TUpdateThread = class(TThread)
private
FADOQuery: TADOQuery;
FExecADO: TADOQuery;
TableA, TableB, TableC: string;
SaveADOConnection: TADOConnection;
LocalADOConnection: TADOConnection;
proceduredo
UpdateSale;
public
constructor Create(ATable, BTable, CTable: string;
LocalADO, ADOConnection: TADOConnection;
ASusped: Boolean);
destructor Destroy;
override;
procedure Execute;
override;
end;
constructor TUpdateThread.Create(ATable, BTable, CTable: string;
LocalADO, ADOConnection: TADOConnection;
ASusped: Boolean);
begin
TableA := ATable;
TableB := BTable;
TableC := CTable;
SaveADOConnection := ADOConnection;
LocalADOConnection := LocalADO;
FADOQuery := TADOQuery.Create(nil);
FExecADO := TADOQuery.Create(nil);
FADOQuery.Connection := LocalADOConnection;
FExecADO.Connection := SaveADOConnection;
Inherited Create(ASusped);
end;
destructor TUpdateThread.Destroy;
begin
FADOQuery.Free;
FExecADO.Free;
inherited;
end;
procedure TUpdateThread.DoUpdateSale;
var
tmpSQLStr: string;
begin
FADOQuery.Close;
FADOQuery.SQL.Text := 'select * from hs_000a';
FADOQuery.Open;
FExecADO.Close;
FExecADO.SQL.Text :=
'insert into ' + TableA + ' (did, dinsetcode, dno, ddate, dtime, dnum, dprice, '
+ ' dmoney_sale, dmoney_ss, dworker, dcard_no, dsale_state, '
+ ' ddiskname, dsubshop, djf) values('
+ ' id, Barcode, No, Date, time, Num, price, '
+ ' Money_Sale, Money_SS, Worker, Card_No, Sale_State, '
+ ' DiskName, SubShop, JF)';
FExecADO.Prepared := True;
while FADOQuery.RecordCount > 0do
begin
if Terminated then
Exit;
with FExecADO.Parameters, FADOQuerydo
begin
ParamByName('did').Value := FieldByName('did').AsString;
ParamByName('dbarcode').Value := FieldByName('dbarcode').AsString;
ParamByName('dNo').Value := FieldByName('dNo').AsString;
ParamByName('dDate').Value := FieldByName('DDate').AsDateTime;
ParamByName('dtime').Value := FieldByName('dtime').AsString;
ParamByName('dnum').Value := FieldByName('dnum').AsVariant;
ParamByName('dprice').Value := FieldByName('dprice').AsVariant;
ParamByName('dmoney_sale').Value := FieldByName('dmoney_sale').AsVariant;
ParamByName('dmoney_ss').Value := FieldByName('dmoney_ss').AsVariant;
ParamByName('dworker').Value := FieldByName('dworker').AsString;
ParamByName('dcard_no').Value := FieldByName('dcard_no').AsString;
ParamByName('dsale_state').Value := FieldByName('dsale_state').AsString;
ParamByName('DDiskName').Value := FieldByName('DDiskName').AsString;
ParamByName('dsale_state').Value := FieldByName('dsale_state').AsString;
ParamByName('DSubShop').Value := '0001';
ParamByName('DJF').Value := FieldByName('djf').AsFloat;
end;
// with
FExecADO.ExecSQL;
FADOQuery.Delete;
end;
end;
procedure TUpdateThread.Execute;
label Loop1;
var
tmpSQLStr: string;
tmpUpdateCount: Integer;
tmpUpdateLast: Boolean;
begin
CoInitialize(nil);
Priority := tpLowest;
FreeOnTerminate := True;
if not LocalADOConnection.Connected then
LocalADOConnection.Connected := True;
if not SaveADOConnection.Connected then
LocalADOConnection.Connected := True;
tmpUpdateCount := 0;
while Truedo
begin
if Terminated then
begin
Break;
end;
try
EnterCriticalSection(SystemSetup.FSaleUpLock);
Self.Synchronize(DoUpdateSale);
finally
LeaveCriticalSection(SystemSetup.FSaleUpLock);
end;
//如果出错,表明网络有问题,需要进行挂起。
Self.Suspend;
end;
end;