Difference between revisions of "TFBEventMonitor"

From Free Pascal wiki
Jump to navigationJump to search
(moved from firebird)
 
m
Line 37: Line 37:
  
 
{{Warning|TFBEventMonitor creates a thread to monitor incoming events. On Unix include cthreads or any other thread manager.}}
 
{{Warning|TFBEventMonitor creates a thread to monitor incoming events. On Unix include cthreads or any other thread manager.}}
 +
 +
== See also ==
 +
* [[firebird]] Using Firebird with FPC/Lazarus sqldb.
  
 
[[Category:Databases]]
 
[[Category:Databases]]

Revision as of 15:09, 24 May 2014

Overview

Warning-icon.png

Warning: TFBEventMonitor requires FPC version 2.6.3 or higher

TFBEventMonitor in the FBEventMonitor unit is used to monitor named events sent from the database server. Events are typically generated by triggers on certain database operations.

Using events means you can avoid regularly polling the database for status changes and can be useful in cases of high load.

Example

Updating a memo with events received. Monitoring events E1 through E7.

procedure TForm1.StartMonitor;
begin
  EventsM:=TFBEventMonitor.create(Self);
  EventsM.Connection:=IBConnection1;
  EventsM.Events.CommaText:='"E1",E2,E3,E4,"E5","E6","E7"'; //quotes are ignored
  EventsM.OnEventAlert:=OnFBEvent;
  EventsM.RegisterEvents;   // will activate IBConnection1 if not connected
end;

procedure TForm1.StopMonitor;
begin
  EventsM.UnRegisterEvents;  
  EventsM.Free;
end;

procedure TForm1.OnFBEvent(Sender: TObject; EventName: string;
  EventCount: longint; var CancelAlerts: boolean);
begin
  Memo1.Lines.Add(EventName+' occurred '+IntToStr(EventCount)+' times.');
end;

The event monitor stops monitoring when the linked TIBConnection is disconnected and will not restart automatically. Use the TIBConnection.AfterConnect and TIBConnection.BeforeDisconnect event handlers to start and stop the monitoring automatically.

The database server sends events to all connections registered for these events except for those created by the connection itself. Events are sent after a transaction is committed. Rolling back a transaction will cancel all events created in that transaction. fbeventstest.pp in the fcl packages/fcl-db/examples/ directory is a test/demo program that creates a stored procedure to send an event with an arbitrary name and uses a second connection to monitor the events sent.

Warning-icon.png

Warning: TFBEventMonitor creates a thread to monitor incoming events. On Unix include cthreads or any other thread manager.

See also

  • firebird Using Firebird with FPC/Lazarus sqldb.