Lalit Rao
Lalit Rao

Reputation: 551

Listening to the changes made in MySQL database and updating front end automatically without explicitly refreshing the front end

I am working on Marksheet Generation system. D system will be installed on multiple computers with a centralized db server on a separate dedicated machine.

What I need is, if User#1 is inserting a data from PC1, the updated record should reflect on d JavaFX TableView on d system installed on, say, PC2. Same should goes d other way. But how should I achieve it?

Setting a timer and firing a SQL query isn't a good option I feel.

Am using Hibernate and JavaFx for presentation. I have listed MySQL db records on th JavaFX TableView, the TableView doesn't update itself unless I fire a Select query..

But i need to find a way were my System, or particularly that TableView should listen or Observe d changes in database and it should automatically update itself if d records in database table are updated by d same system or by d other system installed on different machine.

Upvotes: 1

Views: 1680

Answers (1)

StanislavL
StanislavL

Reputation: 57421

You can use the open replicator library. You have to configure your MySQL to allow replication for the specified DB and write java code using the library to listen database changes.

It's kind of trigger calling java code.

final OpenReplicator or = new OpenReplicator();
or.setUser("root");
or.setPassword("123456");
or.setHost("localhost");
or.setPort(3306);
or.setServerId(6789);
or.setBinlogPosition(4);
or.setBinlogFileName("mysql_bin.000001");
or.setBinlogEventListener(new BinlogEventListener() {
    public void onEvents(BinlogEventV4 event) {
        // your code goes here
    }
});
or.start();

Thus you catch insert/update/delete. Check which table was chnaged and reflect the changes in your model.

Upvotes: 1

Related Questions