Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuously fetch data from database using Java

Tags:

java

database

I have a scenario where my Java program has to continuously communicate with the database table, for example my Java program has to get the data of my table when new rows are added to it at runtime. There should be continuous communication between my program and database.

If the table has 10 rows initially and 2 rows are added by the user, it must detect this and return the rows.

My program shouldn't use AJAX and timers.

like image 369
deathcaller Avatar asked May 26 '10 10:05

deathcaller


2 Answers

If the database you are using is Oracle, consider using triggers, that call java stored procedure, that notifies your client of changes in the db (using JMS, RMI or whatever you want).

like image 57
George Avatar answered Oct 20 '22 08:10

George


without Ajax and timers, it not seems to do this task.

I have also faced the same issue, where i need to push some data from server to client when it changes.

For this, you can user Server push AKA "Comet" programming.

In coment

  • we make a channel between client and server, where client subscribes for particular channel.
  • Server puts its data in the channel when it has it.
  • when client reads the channel, it gets all the data in the channel and channel is emptied.
  • so every time client reads from channel, it will get new data only.

Also to monitor DB changes, you can have two things,

  1. Some trigger/timer (Check out Quartz Scheduler)
  2. Event base mechanism, which pushes data in the channel on particular events.

basically, client can't know anything happening on server side, so you must push some data or event to tell client that, i have some new data, please call some method. Its kind of notification. So please check in comet/server push with event notification.

hope this helps.

thanks.

like image 38
Parth Avatar answered Oct 20 '22 07:10

Parth