1

I'm new to Java and just started writing some JavaFX applications.

My current project is to write an application for a consulting company that store a list of customers, add them to a queue and serve them one by one. There are a few staffs and they will running a copy of the application I write on their PC.

What I've done so far:

  1. create Customer.class to handle personal info and store them in a MySQL db

  2. create Staff.class to handle staff info

  3. create Service.class to handle kind of services are available for the customers

  4. create Consultation.class to handle info of a particular consultation such as date of consultation, customer being served, which staff is providing service, the services offered and the outcome

  5. create an ObservableArrayList, store the data in the MySQL db, and display the data on a TableView of each client PC

What I want to do is, after a staff editing the data in the list, the changes will be updated on the TableView of other client PCs automatically.

The possible solutions I can think of includes:

Option 1

Program the application to query the db regularly for an update. This method is more simple to implement, but I don't want to keep the MySQL server busy by non-stop querys from a number of clients. I do not want any delay between data write and update on other clients. There are more than 10 clients. If each client update once a second, that will mean at least 10 queries per second and the server will never rest. I don't want to put any stress on the server's harddisk.

Option 2

Program the application to broadcast a message every time after they write data to the db and other clients query the database every time they receive a broadcast. I prefer do it this way but I'm not familiar with network programming. That will mean I'll have to spend some time on it before I can continue the project.

Which of the above is a better choice? Is there other way to keep the TableView on the clients keep synchronized?

katana
  • 51
  • 4

1 Answers1

1

Which of the above is a better choice? Is there another way to keep the TableView on the clients keep synchronized?

Before choosing - you may consider optimizing them,

  1. Option 1 seems quite expensive as it has to request frequently. But you can optimize it using connection-pool and specifying certain time-interval(minimum 10 sec) to fetch the data.

  2. Option2 is much more convincing as it applies the lazy-loading concept. You may consider looking socket programming to notify all clients to fetch data.

It's quite hard to say which one is the better option - somehow, I prefer to go with the first approach if your application may insert data frequently, otherwise go with the second one.

An alternative solution - listening to the data changes

Here are some QA, these solutions may help you to implement your requirement.

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • There is no Java DB listener for SQL DBs as far as I know – katana Jan 04 '20 at 02:15
  • @katana Yes you're correct(except `orcale` does, right?), As you are using `mysql` - you may need to build your own which is tough work, I suggest you to go with one of your options. – Shekhar Rai Jan 04 '20 at 02:19