TAstaServerSocket.ThreadedDBUtilityEvent


 

Applies to
TAstaServerSocket

 

Declaration
procedure ThreadedDBUtilityEvent(ClientSocket: TCustomWinSocket;
  QueryToUse:
TThreadDBAction; CustomEvent: TAstaDBUtilityEvent;
  Params:
TAstaParamList);

 

type TAstaDBUtilityEvent = procedure(Sender: TObject;
  ServerSocket:TAstaServerSocket;

  ClientSocket: TCustomWinSocket; AQuery: TComponent;
  Params: TAstaParamList)
of object;

 

Description
Asta servers are non-blocking and event driven so normal messaging calls are not threaded. You can have ASTA launch a thread in response to a message from any ASTA client and ASTA will also make available some database components for you to use in the thread. If you do not need access to any database calls use ThreadedUtilityEvent. Set the QueryToUse parameter to the type of query component you need like ttSelect or ttExec.

 

The following shows the use of the ThreadedDBUtilityEvent event from an ASTA server. Notice the AstaServerSocketSendCodedParamListSocket call which uses the ThreadedClient copy of the UserRecord. Note: If a client is disconnected at this point the thread will be marked as terminated, so perhaps a call to if not TAstaThread(Sender).HasTerminted then Exit is needed before the actual send is made.

 

This example assumes that the TestUtilSQLEvent is being called from within OnCodedParamList on the server which gets called with a ClientSocket:TCustomWinSocket and an incoming Params:TAstaParamList. In this example a request is made for a component that can handle SQLSelects by passing in ttSelect. ThreadedDBSupplyQuery is called with ttSelect to return the Query:TComponent that can be used safely in the thread. The TestUtilSQLEvent must be of type TAstaDBUtilityEvent.

 

 

 

The call is initiated by calling ThreadedDBUtilityEvent(ClientSocket,ttSelect,TestUtilSQLEvent,Params);

 

procedure TForm1.TestUtilSQLEvent(Sender: TObject;

  ServerSocket: TAstaServerSocket; ClientSocket: TCustomWinSocket;
  Query: TComponent; Params: TAstaParamList);

var

  p: TAstaParamList;

  s: string;

begin

  //the Sender is now a TAstaThread

  try

    TQuery(Query).Close;

    TQuery(Query).SQL.Text := Params[0].AsString;

    TQuery(Query).Open;

  except

    ServerSocket.SendExceptionToClient(ClientSocket,

      Exception(exceptObject).Message);

    Exit;

  end;

  p := TAstaParamList.Create;

  p.FastAdd(IntToStr(TQuery(Query).RecordCount) + ' records retrieved.');

  p.FastAdd(Tquery(Query).SQL.Text);

  p.FastAdd(CloneDataSetToString(Query as TQuery));

  //don't call the AstaServerSocket1 as we are in a thread

  //use the passed in ServerSocket

  ServerSocket.SendCodedParamListSocket(TAstaThread(Sender).
    ThreadedClient, 1550, P);

  //Asta disposes of the query

  //if you want additional components you can call

  //ThreadedDBSupplyQuery since you now have the Sender TAstaThread
  //and the client socket

  p.Free;

end;

 

 

Threading Good Habits

 

See the AstaBDEServer for an example of how to use this method.



ASTA Overview TAstaServerSocket.ThreadedDBSupplySession TAstaServerSocket.ThreadedUtilityEvent