客户端应用程序立即对数据库中的更新做出反应的最佳方式是什么?
对数据库中的数据更新做出立即反应的最佳方法是什么?
我能立即想到的最简单的方法是一个线程,它检查数据库中某些数据的特定更改,并持续等待在某个预定义的时间长度内再次检查它。 这个解决方案对我来说似乎是浪费和次优的,所以我想知道是否有更好的方法。
我认为一定有某种方法,毕竟像 gmail 这样的网络应用程序似乎能够在新电子邮件发送给我后几乎立即更新我的收件箱。 当然,我的客户不会一直不断地检查更新。 我认为他们这样做的方式是使用 AJAX,但我不知道 AJAX 如何表现得像远程函数调用。 我很想知道 gmail 是如何做到这一点的,但我最想知道的是如何在一般情况下使用数据库来做到这一点。
编辑: 请注意,我想立即对客户端代码中的更新做出反应,而不是数据库本身的更新,因此据我所知触发器无法做到这一点。 基本上,我希望用户在数据库发生更改后收到通知或更新屏幕。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里基本上有两个问题:
您希望浏览器能够从 Web 应用程序服务器接收异步事件,而无需在紧密循环中进行轮询。
您希望 Web 应用程序能够从数据库接收异步事件,而无需在紧密循环中进行轮询。
您
对于问题 #1
请参阅这些维基百科链接,了解我认为您正在寻找的技术类型:
编辑:2009 年 3 月 19 日 - 刚刚遇到 ReverseHTTP 这可能对问题 #1 感兴趣。
对于问题 #2
解决方案将特定于您正在使用的数据库以及您的服务器也可能使用的数据库驱动程序。 例如,对于 PostgreSQL 您将使用 听 和 通知。 (并且冒着被否决的风险,您可能会使用数据库触发器在表数据更改时调用 NOTIFY 命令。)
另一种可能的方法是如果数据库具有 用于创建链接到动态库(即 DLL 或 .so)的存储过程或触发器的接口文件)。 然后你可以用 C 或其他语言编写服务器信号代码。
同样,某些数据库允许您使用 Java、Ruby、Python 和 其他。 您也许可以使用其中之一(而不是像 C 那样编译为机器代码 DLL 的东西)作为信号机制。
希望这能给您足够的想法来开始。
You basically have two issues here:
You want a browser to be able to receive asynchronous events from the web application server without polling in a tight loop.
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.
找个时间用wireshark看一下……看起来那里经常有一些谷歌流量。
根据您的数据库,触发器可能会有所帮助。 我编写的应用程序依赖于触发器,但我使用轮询机制来实际“知道”某些内容发生了变化。 我想说,除非您可以将更改传达到数据库之外,否则需要某种轮询机制。
只是我的两分钱。
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.
嗯,最好的方法是数据库触发器。 取决于您尚未指定的 DBMS 支持它们的能力。
重新编辑:事实上,Gmail 等应用程序的做法是使用 AJAX 轮询。 安装 Tamper Data Firefox 扩展程序以查看其实际效果。 诀窍是在“没有新闻”的情况下保持轮询查询的速度快得令人眼花缭乱。
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.
不幸的是,没有办法将数据推送到 Web 浏览器 - 您只能发送数据作为对请求的响应 - 这就是 HTTP 的工作方式。
不过,AJAX 正是您想要使用的:每秒调用一次 Web 服务并不过分,前提是您设计的 Web 服务能够确保它接收少量数据、发回少量数据,并且可以非常快速地运行以生成数据。那个回应。
Unfortunately there's no way to push data to a web browser - you can only ever send data as a response to a request - that's just the way HTTP works.
AJAX is what you want to use though: calling a web service once a second isn't excessive, provided you design the web service to ensure it receives a small amount of data, sends a small amount back, and can run very quickly to generate that response.