多線程數據庫間Tabel資料抄寫問題(100分)

  • 主题发起人 主题发起人 tobyliu
  • 开始时间 开始时间
T

tobyliu

Unregistered / Unconfirmed
GUEST, unregistred user!
使用ODAC 4.0連接兩個Oracle數據庫,抄寫當日某時段資料到另一資料庫,做成NT Service啟動後沒反應,
試了很多方法還是不行,請問是何問題,請高手幫忙 ! 程式代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
DB, MemDS, DBAccess, Ora,Unit2;
type
TFloorstock = class(TService)
OraSession1: TOraSession;
OraQuery1: TOraQuery;
OraSession2: TOraSession;
OraQuery2: TOraQuery;
procedure ServiceStart(Sender: TService;
var Started: Boolean);
procedure ServiceContinue(Sender: TService;
var Continued: Boolean);
procedure ServicePause(Sender: TService;
var Paused: Boolean);
procedure ServiceStop(Sender: TService;
var Stopped: Boolean);
procedure ServiceCreate(Sender: TObject);
private
{ Private declarations }
public
function GetServiceController: TServiceController;
override;
end;
type
TFloorStockThread = class(TThread)
private
protected
procedure Execute;
override;
end;
type
TDataWriteThread = class(TThread)
private
protected
procedure Execute;
override;
end;

var
Floorstock: TFloorstock;
implementation
var FSThread:TFloorStockThread;
FSDataThread:TDataWriteThread;
GetDataFlag:Boolean;
WriteDataFlag:Boolean;
l_last_date,l_work_date:string;
data_count:integer;
hMutex:THandle=0;
{$R *.DFM}
//Synchronize(TFloorStockThread.Execute);
procedure TFloorStockThread.Execute;
begin
if Terminated then
exit;
if ( WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 ) then
begin
Floorstock.OraQuery1.close;
Floorstock.OraQuery1.sql.Clear;
Floorstock.OraQuery1.sql.Add('select to_char(sysdate,''yyyymmdd'') as todaystr from dual');
Floorstock.OraQuery1.prepare;
Floorstock.OraQuery1.open;
l_work_date:=Floorstock.OraQuery1.FieldByname('todaystr').AsString;
Floorstock.OraQuery1.close;
Floorstock.OraQuery1.sql.Clear;
Floorstock.OraQuery1.sql.Add('select WORK_DATE,WO_NUMBER,MODEL_NAME,LINE_NAME,PASS_QTY,FAIL_QTY,');
Floorstock.OraQuery1.sql.Add('from x1.a where WORK_DATE=:workdate1 ');
Floorstock.OraQuery1.Params.ParamByName('workdate1').Value:=l_work_date;
Floorstock.OraQuery1.prepare;
Floorstock.OraQuery1.Open;
Floorstock.OraQuery1.First;
data_count:= Floorstock.OraQuery1.RecordCount;
beep;
if data_count>0 then
begin
WriteDataFlag:=true;
GetdataFlag:=false;
FSThread.Suspend;
FSDataThread.resume;
end;
sleep(500);
end;
end;
procedure TDataWriteThread.Execute;
var i:integer;
l_wo_number,l_model_name,l_line_name:string;
l_pass_qty,l_fail_qty:integer;
begin
if Terminated then
exit;
if (WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0) then
begin
Floorstock.OraQuery2.close;
//delete old data first
Floorstock.OraQuery2.sql.Clear;
Floorstock.OraQuery2.sql.Add('delete from x2.b ');
Floorstock.OraQuery2.sql.Add('where WORK_DATE>=:workdate1');
Floorstock.OraQuery2.Params.ParamByName('workdate1').Value:=l_work_date;
Floorstock.OraQuery2.prepare;
Floorstock.OraQuery2.execsql;
Floorstock.oraQuery1.First;
l_last_date:=l_work_date;
for i:=1 to data_countdo
begin
l_work_date:=Floorstock.OraQuery1.FieldByName('WORK_DATE').Value ;
l_wo_number:=Floorstock.OraQuery1.FieldByName('WO_NUMBER').AsString ;
l_model_name:=Floorstock.OraQuery1.FieldByName('MODEL_NAME').AsString ;
l_line_name:=Floorstock.OraQuery1.FieldByName('LINE_NAME').AsString ;
l_pass_qty:=Floorstock.OraQuery1.FieldByName('PASS_QTY').Asinteger ;
l_fail_qty:=Floorstock.OraQuery1.FieldByName('FAIL_QTY').Asinteger ;
Floorstock.OraQuery2.close;
//insert new data x2
Floorstock.OraQuery2.sql.Clear;
Floorstock.OraQuery2.sql.Add('insert into x2.b(WORK_DATE,WO_NUMBER,MODEL_NAME,LINE_NAME,PASS_QTY,FAIL_QTY) ');
Floorstock.OraQuery2.sql.Add('values(:workdate,:wo,:model,:line,:pass,:fail) ');
Floorstock.OraQuery2.Params.ParamByName('workdate').Value:=l_work_date;
Floorstock.OraQuery2.Params.ParamByName('wo').Value:=l_mo_number;
Floorstock.OraQuery2.Params.ParamByName('model').Value:=l_model_name;
Floorstock.OraQuery2.Params.ParamByName('line').Value:=l_line_name;
Floorstock.OraQuery2.Params.ParamByName('pass').Value:=l_pass_qty;
Floorstock.OraQuery2.Params.ParamByName('fail').Value:=l_fail_qty;
Floorstock.OraQuery2.prepare;
Floorstock.OraQuery2.execsql;
Floorstock.OraQuery1.Next;
end;
WriteDataFlag:=False;
GetdataFlag:=True;
FSThread.Resume;
FSDataThread.Suspend;
sleep(500);
end;

end;

procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
Floorstock.Controller(CtrlCode);
end;

function TFloorstock.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;

procedure TFloorstock.ServiceStart(Sender: TService;
var Started: Boolean);
begin
hMutex:=CreateMutex(nil,false,nil);
FSThread := TFloorStockThread.Create(False);
FSDataThread:=TDataWriteThread.Create(False);
WriteDataFlag:=False;
GetdataFlag:=True;
FSDataThread.Suspend;
Started := True;
end;

procedure TFloorstock.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
FSThread.Resume;
FSDataThread.Resume;
Continued := True;
end;

procedure TFloorstock.ServicePause(Sender: TService;
var Paused: Boolean);
begin
FSThread.Suspend;
FSDataThread.Suspend;
Paused := True;
end;

procedure TFloorstock.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
CloseHandle(hMutex);
FSThread.Terminate;
FSDataThread.Terminate;
Stopped := True;
end;

procedure TFloorstock.ServiceCreate(Sender: TObject);
begin
OraSession1.Options.Net := True;
OraSession1.Username := 'x1';
OraSession1.Password := 'x1';
OraSession1.Server := '10.181.9.200:1521:xx';
OraSession1.Connect;
OraSession2.Options.Net := True;
OraSession2.Username := 'x2';
OraSession2.Password := 'x2';
OraSession2.Server := '10.181.9.201:1521:xxx';
OraSession2.Connect;
end;

end.
 
后退
顶部