使用Java不断地从数据库中获取数据

时间:2021-07-29 07:57:30

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.

我有一个场景,我的Java程序必须不断地与数据库表通信,例如,当运行时向表添加新行时,我的Java程序必须获得表的数据。我的程序和数据库之间应该有连续的通信。

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

如果表最初有10行,用户添加了2行,那么它必须检测到这一点并返回行。

My program shouldn't use AJAX and timers.

我的程序不应该使用AJAX和计时器。

7 个解决方案

#1


3  

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).

如果您正在使用的数据库是Oracle,请考虑使用触发器,该触发器调用java存储过程,它通知您的客户机db中的更改(使用JMS、RMI或任何您想要的)。

#2


2  

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

没有Ajax和计时器,它似乎不能完成这个任务。

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.

为此,您可以使用用户服务器推送即“Comet”编程。

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,

还要监控DB的变化,你可以有两件事,

  1. Some trigger/timer (Check out Quartz Scheduler)
  2. 一些触发器/计时器(查看Quartz调度器)
  3. Event base mechanism, which pushes data in the channel on particular events.
  4. 事件库机制,将特定事件的数据推送到通道中。

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.

基本上,客户端不知道服务器端发生了什么,所以您必须按一些数据或事件告诉客户,我有一些新数据,请调用一些方法。同类的通知。所以请检查comet/服务器推送和事件通知。

hope this helps.

希望这个有帮助。

thanks.

谢谢。

#3


0  

Not the simplest problem, really.

这不是最简单的问题。

Let's divide it into 2 smaller problems:

我们把它分成两个小问题:

1) how to enable reloading without timers and ajax

1)如何在没有计时器和ajax的情况下重新加载。

2) how to implement server side

2)如何实现服务器端

  1. There is no way to notify clients from the server. So, you need to use flash or silverlight or JavaFX or Applets to create a thick client. If the problem with Ajax is that you don't know how to use it for this problem then you can investigate some ready-to-use libraries of jsp tags or jsf components with ajax support.

    无法从服务器通知客户端。因此,您需要使用flash或silverlight、JavaFX或applet来创建一个厚客户机。如果Ajax的问题是您不知道如何使用它来解决这个问题,那么您可以使用Ajax支持来研究jsp标记或jsf组件的一些现成库。

  2. If you have only 1 server then just add a cache. If there are several servers then consider using distributed caches.

    如果只有一个服务器,那么只需添加一个缓存。如果有几个服务器,那么考虑使用分布式缓存。

#4


0  

If you have a low-traffic database you could implement a thread that rapidly checks for updates to the DB (polling).

如果您有一个低通信量的数据库,您可以实现一个线程,该线程可以快速检查数据库的更新(轮询)。

If you have a high-traffic DB i wouldn't recommend that, 'cause polling creates much additional traffic.

如果你有一个高流量的DB,我不建议这样做,因为轮询会产生更多的额外流量。

#5


0  

server notifying client is not a good idea (consider a scenario with a 1000 clients). Do u use some persistence layer or u have to stick to pure JDBC?

服务器通知客户机不是一个好主意(考虑一个有1000个客户机的场景)。你是使用一些持久性层还是必须使用纯JDBC?

#6


0  

If you have binary logs turned on in MYSQL , you can see all of the transactions that occur in the database.

如果在MYSQL中打开了二进制日志,则可以看到数据库中发生的所有事务。

#7


0  

A portable way to do this, is adding a column time stamp (create date) which indicates when the row was added to the table. After initial loading of the content you simply poll for new content which a where clause current_time >= create_date. In case that rows could have identical timestamps you need to filter duplicates before adding them.

这样做的一个可移植的方法是添加一个列时间戳(创建日期),它指示何时将行添加到表中。在初始加载内容之后,您只需轮询一个where子句current_time >= create_date的新内容。如果行可能有相同的时间戳,您需要在添加重复的时间戳之前过滤它们。

#1


3  

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).

如果您正在使用的数据库是Oracle,请考虑使用触发器,该触发器调用java存储过程,它通知您的客户机db中的更改(使用JMS、RMI或任何您想要的)。

#2


2  

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

没有Ajax和计时器,它似乎不能完成这个任务。

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.

为此,您可以使用用户服务器推送即“Comet”编程。

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,

还要监控DB的变化,你可以有两件事,

  1. Some trigger/timer (Check out Quartz Scheduler)
  2. 一些触发器/计时器(查看Quartz调度器)
  3. Event base mechanism, which pushes data in the channel on particular events.
  4. 事件库机制,将特定事件的数据推送到通道中。

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.

基本上,客户端不知道服务器端发生了什么,所以您必须按一些数据或事件告诉客户,我有一些新数据,请调用一些方法。同类的通知。所以请检查comet/服务器推送和事件通知。

hope this helps.

希望这个有帮助。

thanks.

谢谢。

#3


0  

Not the simplest problem, really.

这不是最简单的问题。

Let's divide it into 2 smaller problems:

我们把它分成两个小问题:

1) how to enable reloading without timers and ajax

1)如何在没有计时器和ajax的情况下重新加载。

2) how to implement server side

2)如何实现服务器端

  1. There is no way to notify clients from the server. So, you need to use flash or silverlight or JavaFX or Applets to create a thick client. If the problem with Ajax is that you don't know how to use it for this problem then you can investigate some ready-to-use libraries of jsp tags or jsf components with ajax support.

    无法从服务器通知客户端。因此,您需要使用flash或silverlight、JavaFX或applet来创建一个厚客户机。如果Ajax的问题是您不知道如何使用它来解决这个问题,那么您可以使用Ajax支持来研究jsp标记或jsf组件的一些现成库。

  2. If you have only 1 server then just add a cache. If there are several servers then consider using distributed caches.

    如果只有一个服务器,那么只需添加一个缓存。如果有几个服务器,那么考虑使用分布式缓存。

#4


0  

If you have a low-traffic database you could implement a thread that rapidly checks for updates to the DB (polling).

如果您有一个低通信量的数据库,您可以实现一个线程,该线程可以快速检查数据库的更新(轮询)。

If you have a high-traffic DB i wouldn't recommend that, 'cause polling creates much additional traffic.

如果你有一个高流量的DB,我不建议这样做,因为轮询会产生更多的额外流量。

#5


0  

server notifying client is not a good idea (consider a scenario with a 1000 clients). Do u use some persistence layer or u have to stick to pure JDBC?

服务器通知客户机不是一个好主意(考虑一个有1000个客户机的场景)。你是使用一些持久性层还是必须使用纯JDBC?

#6


0  

If you have binary logs turned on in MYSQL , you can see all of the transactions that occur in the database.

如果在MYSQL中打开了二进制日志,则可以看到数据库中发生的所有事务。

#7


0  

A portable way to do this, is adding a column time stamp (create date) which indicates when the row was added to the table. After initial loading of the content you simply poll for new content which a where clause current_time >= create_date. In case that rows could have identical timestamps you need to filter duplicates before adding them.

这样做的一个可移植的方法是添加一个列时间戳(创建日期),它指示何时将行添加到表中。在初始加载内容之后,您只需轮询一个where子句current_time >= create_date的新内容。如果行可能有相同的时间戳,您需要在添加重复的时间戳之前过滤它们。