19

An application needs to have data as more freshly updated from a database as possible. In such a case, is there any other way for getting the data, besides of a timer based requesting (polling) the database?

I work with a MS SQL Server 2008 (and .NET applications + Entity Framework), but I'd like to get knowing about other types of databases as well.

1

7 Answers 7

5

Service Broker for SQL Server 2005+ can do this.

Sorry, I'm not sure of other RDBMS

2
  • 2
    Can you elaborate on how Service Broker does this? @rem is asking about retrieving event-triggered (as opposed to time-triggered) updates from a database. SB is for queuing and asyc processing. Commented Sep 12, 2011 at 19:37
  • 1
    -1 Service Broker does not seem have anything built into it to let an app know that it has received a message. (I am currently stuck on this issue.)
    – Vaccano
    Commented Aug 30, 2013 at 20:43
10

In Oracle you can use the built in DBMS_ALERT package to facilitate this.

DBMS_ALERT supports asynchronous notification of database events (alerts). By appropriate use of this package and database triggers, an application can notify itself whenever values of interest in the database are changed.

Suppose a graphics tool is displaying a graph of some data from a database table. The graphics tool can, after reading and graphing the data, wait on a database alert (WAITONE) covering the data just read. The tool automatically wakes up when the data is changed by any other user. All that is required is that a trigger be placed on the database table, which performs a signal (SIGNAL) whenever the trigger is fired.

3
  • And for "MS SQL Server 2008" as per OPs question? They asked for other RDBMS but this doesn't actually help.
    – gbn
    Commented Mar 4, 2011 at 20:22
  • 9
    @gbn Why doesn't it help? I can't answer the SQL Server part like you can, so I answered the part I could. No one is an expert on all other databases, but if we each contribute our knowledge the OP and future visitors will find useful information. I would expect the OP to accept an SQL Server answer and up-vote other useful answers like ScottCher's (+1). Commented Mar 4, 2011 at 20:58
  • well said. After reading your DBMS_ALERT answer, I learned another way of doing this - more tools for the toolbox! +1
    – ScottCher
    Commented Mar 4, 2011 at 22:08
8

LISTEN / NOTIFY for PostgreSQL

http://www.postgresql.org/docs/current/static/sql-notify.html

in the database...

NOTIFY static_channel_name, 'static-message';

or in a function/trigger:

perform pg_notify('dynamic-channel-name', 'dynamic-message');

in the database client:

LISTEN some_channel_name; --note the lack of quotes

The LISTEN client will receive the PostgreSQL process ID, channel name, and message value.

The standard JDBC driver for PostgreSQL doesn't like notifications, however you can use the https://github.com/impossibl/pgjdbc-ng driver for this purpose

7

Certain database vendors also provide integrated message buses that your app can simply subscribe to:

An alternative would be to route the data into the database in the first place via a message bus like Tibco/RV and simply "branch" it, on stream going into the DB and one going to your application, or use a caching layer like Coherence between your app and the DB.

6

Another Oracle solution: We've developed applications using the dotnet framework from Microsoft that take advantage of Database Change Notification feature of Oracle in conjunction with ODP.Net (the Oracle data provider for dotnet). Using this, the database actually notifies the dotnet application when new data has arrived allowing us to avoid constant polling. The link I reference above is an Oracle tutorial for doing just that. Hope this helps you out.

Don't know about any other RDBMS's.

2

For one of our applications (accessed trough Chrome and ONLY Chrome), we're using MySQL with sys_exec UDF. Basically, why Chrome - due to WebSocket support.

Once a critical update / insert / delete occurs, an external program is invoked trough newly added sys_exec functionality embedded in certain triggers. At that point, we've got all we need to relay the message to every connected client without any need for polling or multiple queries as everything happens real-time.

2

We use a combination of Oracle GoldenGate and the Java Persistence API (JPA) to do this with Oracle database, but also with DB2, Sybase, Microsoft SQL Server, MySQL, Teradata, etc. The feature is described here: http://docs.oracle.com/middleware/1212/coherence/COHIG/golden_g.htm

What GoldenGate does is turn the database transaction log into a filterable event stream that can be consumed anywhere across the network. We use it to turn relevant transactions into either cache updates or cache invalidations, both of which can trigger application-level events, e.g. pushing the data all the way out to desktop or HTML5 websocket applications.

(For full disclosure, I work at Oracle on one of the products using GoldenGate.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.