客户端应用程序立即对数据库中的更新做出反应的最佳方法是什么?

时间:2021-07-24 20:10:00

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.

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

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.

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

4 个解决方案

#1


7  

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.

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

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

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

For Problem #1

对于问题#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.

编辑:2009年3月19日 - 刚刚遇到了可能对问题#1感兴趣的ReverseHTTP。

For Problem #2

对于问题#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.)

解决方案将特定于您使用的数据库,也可能是您的服务器使用的数据库驱动程序。例如,使用PostgreSQL,您将使用LISTEN和NOTIFY。 (并且冒着被投票的风险,你可能会使用数据库触发器在更改表数据时调用NOTIFY命令。)

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.

另一种可能的方法是,如果数据库具有用于创建链接到动态库(即DLL或.so文件)的存储过程或触发器的接口。然后你可以用C或其他任何方式编写服务器信令代码。

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.

在同一主题上,一些数据库允许您使用Java,Ruby,Python等语言编写存储过程。您可能能够使用其中一种(而不是像C那样编译机器代码DLL的东西)用于信令机制。

Hope that gives you enough ideas to get started.

希望能给你足够的想法开始。

#2


6  

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.

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

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.

只是我的两分钱。

#3


1  

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

好吧,最好的方法是数据库触发器。取决于您未指定的DBMS支持它们的能力。

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.

重新编辑:像Gmail这样的应用程序实际上是使用AJAX轮询。安装Tamper Data Firefox扩展以查看其运行情况。诀窍是在“无新闻”案例中保持您的民意调查快速查询。

#4


1  

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.

遗憾的是,没有办法将数据推送到Web浏览器 - 您只能将数据作为对请求的响应发送 - 这就是HTTP的工作方式。

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.

AJAX就是你想要使用的:只要你设计一个Web服务以确保它接收少量数据,发回少量数据并且可以非常快速地生成以生成那个回应。

#1


7  

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.

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

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

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

For Problem #1

对于问题#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.

编辑:2009年3月19日 - 刚刚遇到了可能对问题#1感兴趣的ReverseHTTP。

For Problem #2

对于问题#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.)

解决方案将特定于您使用的数据库,也可能是您的服务器使用的数据库驱动程序。例如,使用PostgreSQL,您将使用LISTEN和NOTIFY。 (并且冒着被投票的风险,你可能会使用数据库触发器在更改表数据时调用NOTIFY命令。)

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.

另一种可能的方法是,如果数据库具有用于创建链接到动态库(即DLL或.so文件)的存储过程或触发器的接口。然后你可以用C或其他任何方式编写服务器信令代码。

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.

在同一主题上,一些数据库允许您使用Java,Ruby,Python等语言编写存储过程。您可能能够使用其中一种(而不是像C那样编译机器代码DLL的东西)用于信令机制。

Hope that gives you enough ideas to get started.

希望能给你足够的想法开始。

#2


6  

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.

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

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.

只是我的两分钱。

#3


1  

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

好吧,最好的方法是数据库触发器。取决于您未指定的DBMS支持它们的能力。

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.

重新编辑:像Gmail这样的应用程序实际上是使用AJAX轮询。安装Tamper Data Firefox扩展以查看其运行情况。诀窍是在“无新闻”案例中保持您的民意调查快速查询。

#4


1  

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.

遗憾的是,没有办法将数据推送到Web浏览器 - 您只能将数据作为对请求的响应发送 - 这就是HTTP的工作方式。

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.

AJAX就是你想要使用的:只要你设计一个Web服务以确保它接收少量数据,发回少量数据并且可以非常快速地生成以生成那个回应。