Subject | Sharing TIB_Connection,session, Transaction accross threads |
---|---|
Author | williamvdw2004 |
Post date | 2004-09-20T10:25:06Z |
I am using IB objects 4.3 Aa (Firebird 1.5). It it possible to create
a connection, session, transaction and Query in the main thread and
use it in another thread?
I have created an application in delphi where the connection, etc was
made in the main thread and the other thread would use this
connection for DB access.
I would just like to know if this would cause any unforseen problems?
see code below:
(The Thread)
unit Unit2;
interface
uses
Classes;
type
TOne = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only
be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TOne.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TOne }
procedure TOne.Execute;
var
k : integer;
begin
{ Place thread code here }
for k := 1 to 10000 do
begin
Form1.FTest2.insert;
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs,IB_Components,IB_Session, StdCtrls,unit2;
type
TTest = Class
private
FQuery : TIB_Query;
FTransaction : TIB_Transaction;
FSession : TIB_Session;
FConnection : TIB_Connection;
Public
constructor create;
procedure insert;
destructor destroy;override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FTest2 : TTest;
Fone : TOne;
end;
var
Form1: TForm1;
implementation
uses uThreadsafeDBCon;
{$R *.dfm}
{ TTest }
destructor TTest.destroy;
begin
destroyThreadsafeDBConnection
(FQuery,FConnection,FSession,FTransaction);
inherited;
end;
constructor TTest.create;
begin
inherited create;
makeThreadsafeDBConnection
(Fquery,Fconnection,Fsession,Ftransaction,'TEST_1.GDB');
with FQuery do
begin
sql.Add('INSERT INTO TESTTABLE
(TRANSACTIONKEY,MSGID,SOURCE,SMSNAME,ADDRESS,ID,RESPONSE,DEST,CELLNO,C
ELLMSG,FRAGMENT,USED,SMSTIMESTAMP) '+
'VALUES
(:TRANSACTIONKEY,:MSGID,:SOURCE,:SMSNAME,:ADDRESS,:ID,:RESPONSE,:DEST,
:CELLNO,:CELLMSG,:FRAGMENT,:USED,:SMSTIMESTAMP)');
Prepare;
end;
end;
procedure TTest.insert;
var
k : integer;
begin
if not FTransaction.Started then
FTransaction.StartTransaction;
with FQuery do
begin
ParamByName('TRANSACTIONKEY').AsString := IntToStr(k);
ParamByName('MSGID').AsString := IntToStr(k);
ParamByName('SOURCE').AsString := IntToStr(k);
ParamByName('SMSNAME').AsString := IntToStr(k);
ParamByName('ADDRESS').AsString := IntToStr(k);
ParamByName('ID').AsString := IntToStr(k);
ParamByName('RESPONSE').AsString := IntToStr(k);
ParamByName('DEST').AsString := IntToStr(k);
ParamByName('CELLNO').AsString := IntToStr(k);
ParamByName('CELLMSG').AsString := IntToStr(k);
ParamByName('FRAGMENT').AsString := IntToStr(k);
ParamByName('USED').AsString := IntToStr(k);
ParamByName('SMSTIMESTAMP').AsString := IntToStr(k);
Execute;
end;
if FTransaction.Started then
FTransaction.Commit;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FTest2 := TTest.create;
Fone := TOne.Create(False);
end;
end.
Thanks
William
a connection, session, transaction and Query in the main thread and
use it in another thread?
I have created an application in delphi where the connection, etc was
made in the main thread and the other thread would use this
connection for DB access.
I would just like to know if this would cause any unforseen problems?
see code below:
(The Thread)
unit Unit2;
interface
uses
Classes;
type
TOne = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only
be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TOne.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TOne }
procedure TOne.Execute;
var
k : integer;
begin
{ Place thread code here }
for k := 1 to 10000 do
begin
Form1.FTest2.insert;
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs,IB_Components,IB_Session, StdCtrls,unit2;
type
TTest = Class
private
FQuery : TIB_Query;
FTransaction : TIB_Transaction;
FSession : TIB_Session;
FConnection : TIB_Connection;
Public
constructor create;
procedure insert;
destructor destroy;override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FTest2 : TTest;
Fone : TOne;
end;
var
Form1: TForm1;
implementation
uses uThreadsafeDBCon;
{$R *.dfm}
{ TTest }
destructor TTest.destroy;
begin
destroyThreadsafeDBConnection
(FQuery,FConnection,FSession,FTransaction);
inherited;
end;
constructor TTest.create;
begin
inherited create;
makeThreadsafeDBConnection
(Fquery,Fconnection,Fsession,Ftransaction,'TEST_1.GDB');
with FQuery do
begin
sql.Add('INSERT INTO TESTTABLE
(TRANSACTIONKEY,MSGID,SOURCE,SMSNAME,ADDRESS,ID,RESPONSE,DEST,CELLNO,C
ELLMSG,FRAGMENT,USED,SMSTIMESTAMP) '+
'VALUES
(:TRANSACTIONKEY,:MSGID,:SOURCE,:SMSNAME,:ADDRESS,:ID,:RESPONSE,:DEST,
:CELLNO,:CELLMSG,:FRAGMENT,:USED,:SMSTIMESTAMP)');
Prepare;
end;
end;
procedure TTest.insert;
var
k : integer;
begin
if not FTransaction.Started then
FTransaction.StartTransaction;
with FQuery do
begin
ParamByName('TRANSACTIONKEY').AsString := IntToStr(k);
ParamByName('MSGID').AsString := IntToStr(k);
ParamByName('SOURCE').AsString := IntToStr(k);
ParamByName('SMSNAME').AsString := IntToStr(k);
ParamByName('ADDRESS').AsString := IntToStr(k);
ParamByName('ID').AsString := IntToStr(k);
ParamByName('RESPONSE').AsString := IntToStr(k);
ParamByName('DEST').AsString := IntToStr(k);
ParamByName('CELLNO').AsString := IntToStr(k);
ParamByName('CELLMSG').AsString := IntToStr(k);
ParamByName('FRAGMENT').AsString := IntToStr(k);
ParamByName('USED').AsString := IntToStr(k);
ParamByName('SMSTIMESTAMP').AsString := IntToStr(k);
Execute;
end;
if FTransaction.Started then
FTransaction.Commit;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FTest2 := TTest.create;
Fone := TOne.Create(False);
end;
end.
Thanks
William