简体   繁体   English

Java客户端应用程序的方式。 可以“捕获”(通过JDBC)触发过程查询产生的结果吗?

[英]How a Java client app. can “catch” (via JDBC) the result produced by a trigger procedure query?

I'm trying to understand how a java (client) application that communicates, through JDBC, with a pgSQL database (server) can "catch" the result produced by a query that will be fired (using a trigger) whenever a record is inserted into a table. 我试图了解通过JDBC与pgSQL数据库(服务器)通信的java(客户端)应用程序如何“捕获”每当插入记录时将被触发(使用触发器)的查询所产生的结果到一张桌子。

So, to clarify, via JDBC I install a trigger procedure prepared to execute a query whenever a record is inserted into a given database table, and from this query's execution will result an output (wrapped in a resultSet, I suppose). 因此,为澄清起见,我通过JDBC安装了一个触发器程序,该触发器程序准备在将记录插入给定数据库表中时执行查询,并且从该查询的执行将产生输出(我想将其包装在resultSet中)。 And my problem is that I have no idea how the client will be aware of those results, that are asynchronously produced. 我的问题是我不知道客户如何知道异步产生的那些结果。

I wonder if JDBC supports any "callback" mechanism able to catch the results produced by a query that is fired through a trigger procedure under the "INSERT INTO table" condition. 我想知道JDBC是否支持任何“回调”机制,以捕获在“ INSERT INTO表”条件下通过触发过程触发的查询所产生的结果。 And if there is no such "callback" mechanism, what is the best approach to achieve this result? 如果没有这种“回调”机制,那么达到此结果的最佳方法是什么?

Thank you in advance :) 先感谢您 :)

Triggers can't return a resultset. 触发器无法返回结果集。

There's no way to send such a result to the JDBC driver. 无法将这样的结果发送到JDBC驱动程序。

There are a few dirty hacks you can use to get results from a trigger to the client, but they're all exactly that. 您可以使用一些肮脏的技巧来从触发器获取结果到客户端,但是它们全都如此。 Things like: 像:

  • DECLARE a cursor for the resultset, then send the cursor name as a NOTIFY payload, so the app can FETCH ALL FROM <cursorname> ; DECLARE结果集的游标,然后将游标名称作为NOTIFY有效负载发送,因此应用程序可以FETCH ALL FROM <cursorname>

  • Create a TEMPORARY table and report the name via NOTIFY 创建一个TEMPORARY表并通过NOTIFY报告名称

It is more typical to append anything the trigger needs to communicate to the app to a table that exists for that purpose and have the app SELECT from it after the operation that fired the trigger ran. 通常,将触发器需要与应用程序进行通信的所有内容附加到为此目的而存在的表中,并在触发触发器的操作运行后从中SELECT应用程序SELECT

In most cases if you need to do this, you're probably using a trigger where a regular function is a better fit. 在大多数情况下,如果需要执行此操作,则可能使用的触发器更适合常规功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM