gpt4 book ai didi

java - 当数据库中的postgresql表更新时如何触发java进程

转载 作者:行者123 更新时间:2023-12-02 09:30:06 24 4
gpt4 key购买 nike

系统 A - 更新 postgresql 表,例如测试,每次用新条目更新表时,

系统 B(Spring boot 项目) - 应该以某种方式监听 dbChange(测试)并读取新条目

我尝试在系统B中使用@Eventlistener来获取更新的数据,但没有成功。有人可以帮助如何在表更新时将表中的数据获取到单独的系统 B 中吗?

最佳答案

你看到 postgres 的“TRIGGER”了吗?它允许您在执行特定操作之后或之前执行某些操作。每个触发器都与特定的表或 View 相关。

Postgress 的文档说:

CREATE [ CONSTRAINT ] TRIGGER name { BEFORE | AFTER | INSTEAD OF } { event [ OR ... ] }  
ON table
[ FROM referenced_table_name ]
[ NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY DEFERRED } ]
[ FOR [ EACH ] { ROW | STATEMENT } ]
[ WHEN ( condition ) ]
EXECUTE PROCEDURE function_name ( arguments )

其中事件可以是以下之一:

INSERT,  
UPDATE [ OF column_name [, ... ] ],
DELETE,
TRUNCATE

简单描述:

CREATE TRIGGER creates a new trigger. The trigger will be associated with the specified table or view and will execute the specified function function_name when certain events occur.

The trigger can be specified to fire before the operation is attempted on a row (before constraints are checked and the INSERT, UPDATE, or DELETE is attempted); or after the operation has completed (after constraints are checked and the INSERT, UPDATE, or DELETE has completed); or instead of the operation (in the case of inserts, updates or deletes on a view). If the trigger fires before or instead of the event, the trigger can skip the operation for the current row, or change the row being inserted (for INSERT and UPDATE operations only). If the trigger fires after the event, all changes, including the effects of other triggers, are "visible" to the trigger.

如果你想了解更多,可以阅读官方 Postgres' documentation

您可以使用触发器向一个特定端点发出 HTTP(S) 请求,以便向您的 Spring 应用程序表明您的数据库刚刚发生了某些变化。

关于java - 当数据库中的postgresql表更新时如何触发java进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58057490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com