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

查看:253
本文介绍了什么是客户端应用程序立即响应数据库中的更新的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是编程对数据库中数据更新的立即反应的最好方法?

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.

我认为必须有一些方法,毕竟,像gmail这样的应用程序似乎能够在向我发送新电子邮件后立即更新我的收件箱。当然我的客户端不是一直在不断检查更新。我认为他们这样做的方式是与AJAX,但如何AJAX可以表现得像一个远程函数调用我不知道。我很想知道gmail如何做到这一点,但我最想知道的是如何在一般情况下使用数据库。

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.

编辑:
请注意,我想立即对客户端代码中的更新做出反应,而不是在数据库本身,所以我知道触发器不能这样做。基本上,我希望用户在数据库更改后获得通知或更新屏幕。

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.

推荐答案

基本上有两个问题:


  1. 您希望浏览器能够从Web应用程序服务器接收异步事件,

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

对于问题#1

查看这些维基百科我认为您正在寻找的技术类型的链接:

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

  • Comet
  • Reverse AJAX
  • HTTP Server Push

编辑:2009年3月19日 - 刚刚遇到 ReverseHTTP 。这可能是第一个问题的关注。

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

2

解决方案将特定于您使用的数据库,也可能是您的服务器使用的数据库驱动程序。例如,使用 PostgreSQL ,您将使用 LISTEN NOTIFY 。 (另外一种可能的方法是,在更改表格数据时,使用数据库触发器调用NOTIFY命令。)

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

这是如果数据库具有界面来创建存储过程或触发器链接到动态库(即DLL或.so文件)。然后,您可以在 C或任何中编写服务器信号代码。

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.

在同一主题下,一些数据库允许您使用 Java,Ruby Python 其他 。你可以使用其中的一个(而不是编译为类似C的机器代码DLL)的信号机制。

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.

这篇关于什么是客户端应用程序立即响应数据库中的更新的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆