简体   繁体   中英

How to receive MySQL database notifications in Delphi?

I am developing a Delphi XE7 application with data stored in an online Mysql database. For the database access I use FireDAC. Because the application can be used on more than one computer simultaneously I need to be notified when a table is changed, so I can update the displayed information on each computer. FireDAC has a component called TFDEventAlerted which sounded like exactly what I need for this. But this component gives an error when activating (calling Register): [FireDAC][Phys][MySQL]-303. Capability is not supported. I am not sure what this means, but after reading more about the component it seems Mysql does not support this type of events? If so: can anyone tell me whether there is another solution to accomplish the same?

Any help would be appreciated as I cannot seem to find a good solution.

Native MySQL doesn't have the push-notification feature you're hoping to use. To make this work you'll need to poll (to regularly run a query) to look for changes.

There are some ways to overcome this limitation if the scale of your system makes polling infeasible. You could add a user-defined function to your MySQL server, like this one to send messages: https://github.com/mysqludf/lib_mysqludf_stomp#readme

This won't work if you don't own the MySQL server; most hosting services won't allow you to install UDFs.

Or, you could build a message publish/subscribe app. This is pretty easy to do with the Amazon simple queuing service or with rabbitmq. But it's a different kind of system design from what you are probably used to.

In my article series about Firebird Database Events I proposed a solution based on message-oriented middleware. The middle tier of your application then would notify all interested parties about certain database events. Middle tier code would be database independent, all you need is a message broker who is specialized in reliable message delivery. An imaginary example for a 'after post' event handler is shown below:

procedure TAppDataModule.PurchaseOrderAfterPost(DataSet: TDataSet);
var
  Notification: INotification;
begin
  Notification := NotificationService.CreateNotification(PURCHASE_ORDER_TABLE_UPDATED);
  Notification.SetIntProperty(PURCHASE_ORDER_ID, PurchaseOrderID.AsInteger);
  NotificationService.Send(Notification);
end;

Popular free/libre open source message brokers are for example Apache ActiveMQ and RabbitMQ .

TFDEventAlerted control is not for MySQL database. That database doesn't support event model. If you want update data in "real time" then you must add manual request for changed data

Here are steps:

  1. Add new field to your database table like "last_updated";

  2. Fill that field by now() value on update or insert actions (by trigger or sql);

  3. Add timer to delphi app and add request by SELECT MAX(last_updated) AS last_updated FROM my_table for last updated time;

  4. If that time is new then request updated data by SELECT * FROM my_table WHERE last_updated >= :need_last_updated .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM