TAstaUtilityEvent


 

Unit
AstaServer

 

Declaration
type TAstaUtilityEvent = procedure(Sender: TObject;ServerSocket: TAstaServerSocket;
  ClientSocket: TCustomWinSocket;Params: TAstaParamList)
of object;

 

Description
The AstaServerSocket is non-blocking and event driven and provides excellent performance without using threads. ASTA database activity can be threaded and to scale an ASTA server, the Pooled Threading Model is recommended. See the book COM + and the Battle for the Middle Tier by Roger Sessions (Wiley Press) for a description of scalable servers that applies quite accurately to ASTA servers running the Pooled Sessions threading model.

 

One of the design goals of ASTA was to provide scalable threaded servers without having ASTA developers to have to create any threads themselves. Since ASTA 2 there have been threaded messaging calls that allow for ASTA messaging to be used in threads on the server. Immediately ASTA users started to push the envelope on asynchronous database access. The problem arose that one remote client would want to be able to have multiple database queries executing on a server, and if left un-checked, this could result in one client using many, many database sessions.

 

In Asta 2.108 an attempt was made to cache asynchronous threaded database calls so that a limit could be set on the amount of concurrent database sessions used by any remote user. To send a message to an ASTA client, normally a ClientSocket (TCustomWinSocket) is used. Internally, ASTA uses the ClientSocket to lookup the UserRecord from the UserList of the TAstaServerSocket. In order to make this thread safe, instead of using the UserList, ASTAThreads now make a full copy of the UserRecord that can be used within threaded util calls in order to make it thread safe. In addition, critical sections have been added to code where it was not needed before when using non-blocking event driven sockets.

 

The prototypes of the ThreadedUtil events have changed. In the past, the Sender (TObject) parameter passed was the AstaServerSocket, and now it will be a TAstaThread. This will allow you to use the UserRecord of the AstaThread to send messages.

 

The following shows the use of a ThreadedDBUtility event from an ASTA server. Notice the ServerSocket.SendCodedParamListSocket 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. The one scenario where we still seem to have problems is when there are queued UtilityEvents that have not been launched, and the client disconnects.

 

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;

 



ASTA Overview TAstaUpdateSQL Threading ASTA Servers