How to receive a notification whenever there is a new record in a database table?

Asked

Viewed 3,389 times

10

I need to receive a notification whenever there is a new record in a particular database table, which I can do to receive ?

  1. If I use one Trigger Mysql will not work when I change database.

  2. If I make an Ajax request at each given interval, I end up making unnecessary requests.

  3. Any other idea?

  • 2

    Obviously such a solution is not part of any SQL standard, so there is no reason to discard a specific solution using a Trigger.

  • 4

    Who needs to be notified, everyone? And is it okay if you have a delay in notification? Sounds like a task for cron.

  • bfavaretto, only 10% of all registrants (500 people) will be notified, no problems if there are delays.

  • 2

    @user737, do not forget to accept an answer if it is the "right answer". So the person who answered also receives some points.

1 answer

5


Define an architecture

If you want something portable between banks you should do in your system. Create a project pattern like: VIEW ---> Service ----> DAO (or Repository).

The DAO would be responsible for knowing whether or not a particular record was updated, either by cache that could be used with Ajax or by Websocket where a notification would be sent.


Showing in View : Websocket

You could use HTML5 + Websocket to send the values to the screen. Every time a record is updated on the DAO (or Repository), a notification would be sent to the view.

The Websocket approach works with the server sending the notification to the page, inverse of an ajax notification where the page searches the value.


Showing in View : Ajax

When using Ajax you do not need to go to the database all the time to know if there has been a change.

You could have this value curled in memory, if there were any changes, the new value would be returned. If there is no change, you do not return anything. It would be something like, you send the date of the last query: 10/10/10 10h10min. If the cache value was the same, you do not need to go to the bank. If the cache value indicated that the time of the newest record was 10/10/10 10h15min then you would search for new information.

An Ajax call would not cause this server overhead since we would be messing with cached value.


Completion

Each approach has its advantage and disadvantage. Just choose the one that best fits your project profile.

Attention: If the value in question is constantly updated, regardless of the approach, there will be a lot of data traffic on the network.

  • Thank you uaiHebert. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.