IBO thread safe

Hi Experts,
 
Please I need a help,
 
I need make a application with TIDTcpServer, TIDTcpClient and IBO, but a have a question.
 
I need do this:
 
1- I have to connect to server and pass a user name
2- With this user name, using TIB_Cursor to find the full name of user in my table
3- Return the name to the client program.
 
I dont know how do this whit IBO because OnConnect event in TIDTcpServer is mult-thread. The access to the TIB_Cursor must be thread-safe
 
How do This
 
Exemple:
 
 
  TMyContext = class(TIdContext)
  public
     username: string;
  end;
 
OnConnect event:
 
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
 
  with TMyContext(AContext) do
  begin
    username:= strtoint(Connection.IOHandler.ReadLn);
    if (username<> '') then
    begin
      ////////////////////////
 
     I need make the select here with TIB_Cursor. 'Select fullname from mytable where user=username'
 
     but I dont know how make this access being thread-safe for each connection...please help me
 
      /////////////////////
      Connection.IOHandler.WriteLn(fullname);
    end
    else
    begin
      Connection.IOHandler.WriteLn('error');
      Connection.Disconnect;
    end;
  end;
end;
 
Thx for some help
 
Alexandre
Brazil
alpiresAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheRealLokiSenior DeveloperCommented:
well, the simplest way would be to use a TCriticalSection
uses SyncObjs;

crit: TCriticalSection;

procedure TForm1.FormCreate(Sender: TObject);
begin
  crit := TCriticalSection.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  crit.Free;
end;
...
...
now make sure that EVERY call to your IBO uses the criticalsection as shown below.
...
...
if (username<> '') then
    begin
      ////////////////////////
      crit.Enter;
      try
        // do sql query here: TIB_Cursor. 'Select fullname from mytable where user=username'
     finally
       crit.Leave;
     end;
      /////////////////////
      Connection.IOHandler.WriteLn(fullname);
    end

with TcriticalSection, you need to keep the code inside as short and fast as possible.
This will likely not impact your app very much, but if you require further performance, you can create the objects inside the Indy routines, and free them after, so that they are only available for the particular thread.
I don't use IBI here so I do not know all the code for creating a query and calling it.
Try the method above, and if you want to try the dynamically created IBO method, paste me all teh code you would do to create the IBO and call it, and I will show you how to do it (basically as easy as just putting that into the indy method...assuming it is inherently thread safe that is)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alpiresAuthor Commented:
one think, can I use the same variable TCriticalSection in event ? some think like this:

      crit.Enter;
      try
        // do sql query here: TIB_Cursor. 'Select fullname from mytable where user=username'
     finally
       crit.Leave;
     end;
     ..
     ..
     crit.Enter;
      try
        // do update sql query here
     finally
       crit.Leave;
     end;

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.