客户端应用程序对数据库中的更新做出立即反应的最佳方式是什么?[英] What's the best way for the client app to immediately react to an update in the database?

本文是小编为大家收集整理的关于客户端应用程序对数据库中的更新做出立即反应的最佳方式是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

数据库中数据更新的直接反应进行编程的最佳方法是什么?

我可以想到的最简单的方法是一个线程,该线程检查数据库是否针对某些数据进行特定更改,并不断等待再次检查它的预定时间长度.这个解决方案似乎对我来说是浪费且优越的,所以我想知道是否有更好的方法.

我认为必须有某种方法,毕竟,像Gmail这样的Web应用程序似乎能够在发送给我的新电子邮件后几乎立即更新我的收件箱.当然,我的客户不会一直在不断检查更新.我认为他们这样做的方式是Ajax,但是Ajax如何表现得像我不知道的远程功能.我很想知道Gmail是如何做到这一点的,但是我最想知道的是在数据库中如何做到这一点.

编辑: 请注意,我想立即对客户端代码中的更新做出反应,而不是在数据库本身中,因此据我所知,触发器无法做到这一点.基本上,我希望用户收到通知或一旦进行数据库的更改,就会更新他的屏幕.

推荐答案

您基本上有两个问题:

  1. 您希望浏览器能够从Web应用程序服务器中接收异步事件,而无需在紧密循环中进行轮询.

  2. 您希望Web应用程序能够从数据库中接收异步事件,而无需在紧密的循环中进行轮询.

问题#1

请参阅我认为您正在寻找的技术类型的这些Wikipedia链接:

编辑:2009年3月19日 - 刚刚遇到 reververshttp 这可能是问题#1的兴趣.

问题#2

该解决方案将特定于您使用的数据库以及服务器使用的数据库驱动程序.例如,使用 postgresql 您将使用 listan 和 notify . (并冒着投票的风险,您可能会使用数据库触发器在更改表数据时调用Notify命令.)

另一种可能的方法是,如果数据库具有 a>创建链接到动态库(即dll或.so文件)的存储过程或触发器.然后,您可以在中编写服务器信号代码./p>

在同一主题上,某些数据库允许您以语言编写存储过程,例如 java,ruby 其他.您可能可以将其中一种用于信号机制.

希望您能为您提供足够的想法.

其他推荐答案

我认为必须有某种方式,之后 所有,像Gmail这样的Web应用程序似乎 几乎立即更新我的收件箱 将新电子邮件发送给我之后. 我的客户肯定不是不断的 一直检查更新.我 认为他们这样做的方式与 Ajax,但是Ajax如何表现 远程功能调用我不知道. ID 好奇地知道Gmail如何做 这,但是我最想知道的 是在一般情况下如何做到这一点 使用数据库.

有时会与Wireshark一起窥视...很定期进行一些Google流量.

.

根据您的数据库,触发器可能会有所帮助.我写的一个应用程序依赖于触发器,但我使用一种投票机制实际上"知道"了某些事情发生了变化.除非您可以从数据库中传达更改,否则我会说一些投票机制.

.

只是我的两分钱.

其他推荐答案

好吧, best 方法是数据库 a>.取决于您尚未指定的DBM的能力支持它们.

您的编辑:Gmail之类的应用程序实际上是Ajax投票的方式.安装 tamper data Firefox扩展名在作用中看到它.在"无新闻"案件中,您的投票量很快保持盲目查询的诀窍.

本文地址:https://www.itbaoku.cn/post/597733.html

问题描述

What is the best way to program an immediate reaction to an update to data in a database?

The simplest method I could think of offhand is a thread that checks the database for a particular change to some data and continually waits to check it again for some predefined length of time. This solution seems to be wasteful and suboptimal to me, so I was wondering if there is a better way.

I figure there must be some way, after all, a web application like gmail seems to be able to update my inbox almost immediately after a new email was sent to me. Surely my client isn't continually checking for updates all the time. I think the way they do this is with AJAX, but how AJAX can behave like a remote function call I don't know. I'd be curious to know how gmail does this, but what I'd most like to know is how to do this in the general case with a database.

Edit: Please note I want to immediately react to the update in the client code, not in the database itself, so as far as I know triggers can't do this. Basically I want the USER to get a notification or have his screen updated once the change in the database has been made.

推荐答案

You basically have two issues here:

  1. You want a browser to be able to receive asynchronous events from the web application server without polling in a tight loop.

  2. You want the web application to be able to receive asynchronous events from the database without polling in a tight loop.

For Problem #1

See these wikipedia links for the type of techniques I think you are looking for:

EDIT: 19 Mar 2009 - Just came across ReverseHTTP which might be of interest for Problem #1.

For Problem #2

The solution is going to be specific to which database you are using and probably the database driver your server uses too. For instance, with PostgreSQL you would use LISTEN and NOTIFY. (And at the risk of being down-voted, you'd probably use database triggers to call the NOTIFY command upon changes to the table's data.)

Another possible way to do this is if the database has an interface to create stored procedures or triggers that link to a dynamic library (i.e., a DLL or .so file). Then you could write the server signalling code in C or whatever.

On the same theme, some databases allow you to write stored procedures in languages such as Java, Ruby, Python and others. You might be able to use one of these (instead of something that compiles to a machine code DLL like C does) for the signalling mechanism.

Hope that gives you enough ideas to get started.

其他推荐答案

I figure there must be some way, after all, web application like gmail seem to update my inbox almost immediately after a new email was sent to me. Surely my client isn't continually checking for updates all the time. I think the way they do this is with AJAX, but how AJAX can behave like a remote function call I don't know. I'd be curious to know how gmail does this, but what I'd most like to know is how to do this in the general case with a database.

Take a peek with wireshark sometime... there's some google traffic going on there quite regularly, it appears.

Depending on your DB, triggers might help. An app I wrote relies on triggers but I use a polling mechanism to actually 'know' that something has changed. Unless you can communicate the change out of the DB, some polling mechanism is necessary, I would say.

Just my two cents.

其他推荐答案

Well, the best way is a database trigger. Depends on the ability of your DBMS, which you haven't specified, to support them.

Re your edit: The way applications like Gmail do it is, in fact, with AJAX polling. Install the Tamper Data Firefox extension to see it in action. The trick there is to keep your polling query blindingly fast in the "no news" case.