从数据库中获取事件 [英] Getting events from a database

查看:25
本文介绍了从数据库中获取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太熟悉数据库以及它们在 CRUD 操作之外提供的功能.

I am not very familiar with databases and what they offer outside of the CRUD operations.

我的研究使我触发.基本上看起来触发器提供了这种类型的功能:

My research has led me to triggers. Basically it looks like triggers offer this type of functionality:

(来自维基百科)

通常有三种触发事件会导致触发器触发":

There are typically three triggering events that cause triggers to "fire":

  • INSERT 事件(当一条新记录被插入到数据库中).
  • UPDATE 事件(因为正在更改记录).
  • DELETE 事件(因为正在删除记录).

我的问题是:当使用某种触发器语义更新/删除/插入记录时,是否可以通过某种方式在 Java(最好包括更改的数据)中通知我?

My question is: is there some way I can be notified in Java (preferably including the data that changed) by the database when a record is Updated/Deleted/Inserted using some sort of trigger semantics?

这个问题有哪些替代解决方案?如何监听数据库事件?

What might be some alternate solutions to this problem? How can I listen to database events?

我想这样做的主要原因是这样的场景:

我有 5 个客户端应用程序,它们都在不同的进程中/存在于不同的 PC 上.它们都共享一个公共数据库(在本例中为 Postgres).

I have 5 client applications all in different processes/existing across different PCs. They all share a common database (Postgres in this case).

假设一个客户更改了数据库中所有 5 个客户都感兴趣"的记录;在.我正在努力想办法让客户得到通知";更改(最好附上受影响的数据),而不是每隔一段时间查询数据.

Lets say one client changes a record in the DB that all 5 of the clients are "interested" in. I am trying to think of ways for the clients to be "notified" of the change (preferably with the affected data attached) instead of them querying for the data at some interval.

推荐答案

使用 Oracle,您可以在表上设置触发器,然后让触发器发送 JMS 消息.Oracle 有两种不同的 JMS 实现.然后,您可以拥有一个使用 JDBC 驱动程序侦听"消息的进程.我已经使用这种方法将更改推送到我的应用程序与轮询.如果您使用的是 Java 数据库 (H2),您还有其他选择.在我当前的应用程序 (SIEM) 中,我在 H2 中有使用 JMX 发布更改事件的触发器.

Using Oracle you can setup a Trigger on a table and then have the trigger send a JMS message. Oracle has two different JMS implementations. You can then have a process that will 'listen' for the message using the JDBC Driver. I have used this method to push changes out to my application vs. polling. If you are using a Java database (H2) you have additional options. In my current application (SIEM) I have triggers in H2 that publish change events using JMX.

这篇关于从数据库中获取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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