Programming with Database Events
Go Up to Developer's Guide
Use the TIBEvents
component in your IBX-based application to register interest in and asynchronously handle InterBase server events. The InterBase event mechanism enables applications to respond to action and database changes made by other, concurrently running applications without the need for those applications to communicate directly with each other, and without incurring the expense of CPU time required for period polling to determine if an event has occurred.
Use the TIBEvents
component in your application to register an event (or a list of events) with the event manager. The event manager maintains a list of events posted to it by triggers and stored procedures. It also maintains a list of applications that have registered an interest in events. Each time a new event is posted to it, the event manager notifies interested applications that the event has occurred.
To use TIBEvents
in your application:
- Create a trigger or stored procedure on the InterBase server which will post an event.
- Add a
TIBDatabase
and aTIBEvents
component to your form. - Add the events to the
Events
list and register them with the event manager. - Write an
OnEventAlert
event handler for each event.
Events are passed by triggers or stored procedures only when the transaction under which they occur is posted. Also, InterBase consolidates events before posting them. For example, if an InterBase trigger posts 20 x STOCK_LOW events within a transaction, when the transaction is committed these will be consolidated into a single STOCK_LOW event, and the client will only receive one event notification.
For more information on events, refer to Working with Events in the Embedded SQL Guide.
Setting up Event Alerts
Double click on the ellipsis button (...) of the Events
property add an event to the Events
list. Each TIBEvents
component can handle up to 15 events. If you need to respond to more that 15 events, use more than one TIBEvents
component. If you attempt to add too many events at runtime, an exception will be raised.
To add an event to the Events
list use the following code:
TIBEvents.Events.Add( 'STOCK_LOW')
Writing an Event Handler
OnEventAlert
is called everytime an InterBase event is received by an IBEvents
component. The EventName
variable contains the name of the event that has just been received. The EventCount
variable contains the number of EventName
events that have been received since OnEventAlert
was last called.
OnEventAlert
runs as a separate thread to allow for true asynchronous event processing, however, the IBEvents
component provides synchronization code to ensure that only one OnEventAlert
event handler executes at any one time.