Page 1 of 1

Detect : Session is in Use

Posted: Fri 02 Mar 2007 23:00
by jfudickar
I have a multi-threaded environment and want to detect if the session is in use.

Example : I have a background thread which gets some data. The vcl thread has a timer which checks if the connection is valid. THis check is only allowed to run when the background thread is not running.

So i need someting like Session.IsInUse.

If it does not exists, it should be easy to realize :-)

Without having the source, i would say that there should be only some base components which execute any type of sql statements again the session.
And this code should include an threadsafe call "IncIsInUse" and "DecIsInUse" on the connected session.
This should be easy to realize, but would greatly increase my app, without doing it for each sql-call myself.

Greetings and thanks
Jens

Posted: Sat 03 Mar 2007 11:10
by Plash
We will investigate possibility to add such functionality, but that is hardly to happen in the nearest future.
If you connect with the Net option set to False, it is safe to execute the query that checks the connection at the same time when another thread executes some other query.

Posted: Sun 04 Mar 2007 18:59
by jfudickar
Hi,

sadly my code to check the connection looks like:

Code: Select all

function txOraSession.CheckConnection(Reconnect: Boolean):
    TCheckConnectionResult;
var Q: TOraQuery;
begin
  Result := ccOK;
  if not Connected then
  begin
    try
      Result := ccError;
      if Reconnect then
      begin
        Connect;
        Result := ccReconnected
      end;
    except
    end;
  end else begin
   Q := TOraQuery.Create(nil);
   try
     Q.Name := Self.Name+'_CheckConnection_OraQuery';
     Q.Session := Self;
     //Q.SQL.Text := 'select ''x'' from dual';
     Q.SQL.Text := 'BEGIN NULL; END;';
     Q.Execute;
   except
     on E: EOraError do
     begin
       if (E.ErrorCode  1000) and // ORA-00942: table or view does not exist
          (E.ErrorCode  942) then // ORA-01000: maximum open cursors exceeded
       begin                        
         Result := ccError;
         try
           Disconnect;
         except
         end;
         if Reconnect then
         try
           Connect;
           Result := ccReconnected;
         except
         end;
       end;
     end;
   end;
   Q.Free;
  end;
end;
And then it doesn't work to check the connection without problems.

If you need help, i'm willing to implement the SessionInUse for you and send it back to you :-)

Greetings
Jens