简体   繁体   English

如何检查是否已在数据库中插入具有某些值的特定行

[英]How to check if a specific row with certain values has been inserted in the Database

I am using Informix DB. 我正在使用Informix DB。 This question may not be tied to one specific database. 这个问题可能与一个特定的数据库无关。 But I want to know how I can in Java, continuously probe into a Database and check if a certain row has been added to a table in the DB. 但是我想知道如何用Java不断地探究数据库,并检查是否已将某行添加到数据库的表中。 Basically, the flow is: 基本上,流程是:

  1. My Java application should use JDBC to check if a certain table is populated. 我的Java应用程序应使用JDBC检查是否填充了某个表。
  2. If no, it should wait until a row has been inserted. 如果否,则应等待插入一行。

My question how can I have Java be aware of a row insertion. 我的问题是如何让Java知道行插入。 I am not expecting to add any triggers or anything, but in pure Java be able to check that the row is added. 我不希望添加任何触发器或任何东西,但是在纯Java中,能够检查是否添加了该行。

Some thoughts that come to my mind are continuously call DB for the row, or periodically (every half-hour or so) call DB and check if the row is available. 我想到的一些想法是连续为该行调用DB,或者定期(每半小时左右)调用DB并检查该行是否可用。 But what I am looking for is something like a Listener which can do this. 但是我正在寻找的是像Listener这样的东西。

There is no facility in the Informix DBMS to signal when a particular row arrives in a table. Informix DBMS中没有工具来通知特定行何时到达表中。

Well, I say that, but there is the DB-Cron facility which can periodically execute tasks (inside the server), and you could conceivably schedule a task to poll for the data to see if it has arrived and to send a message (somehow) to indicate that it has. 好吧,我这么说,但是有一个DB-Cron工具可以定期执行任务(在服务器内部),并且可以想象地安排一个任务以轮询数据以查看其是否到达并发送消息(以某种方式发送) )表示它已经拥有。 It would be non-trivial, especially the part the indicates that it has arrived. 这将是不平凡的,尤其是表明它已经到达的部分。

The JDBC protocol (and SQL protocols generally) are essentially synchronous; JDBC协议(通常和SQL协议)本质上是同步的。 the client sends a request and waits for an answer from the DBMS. 客户端发送请求并等待DBMS的答复。

So, pragmatically, if your delay period is half an hour, you can either create an admin task to handle the processing (you could write a Java UDR to be executed in the server by the server if that's crucial to you), or you can arrange for the Java (client-side) program to poll periodically to find out whether the information you need is there. 因此,务实的是,如果延迟时间为半小时,则可以创建一个管理任务来处理该处理(如果对您至关重要,则可以编写一个Java UDR由服务器在服务器中执行),或者可以安排Java(客户端)程序定期进行轮询,以查找所需的信息是否存在。 A half-hour delay is not going to stress anything, even with a moderate number of processes polling for separate values (or even the same value). 即使有中等数量的进程轮询单独的值(甚至是相同的值),半小时的延迟也不会增加任何压力。 On the other hand, you normally try to avoid polling when you can. 另一方面,通常您会尽量避免轮询。 You'll need to strike a balance between responsiveness to the special data arriving and general system responsiveness. 您需要在对特殊数据到达的响应与常规系统响应之间取得平衡。 On the whole, general system responsiveness is more important, so keep the polling interval as large as you can. 总体而言,一般系统的响应能力更为重要,因此,请保持尽可能大的轮询间隔。

If your polling interval needed to be sub-second, then the balance would be different - the job would be a lot harder. 如果您的轮询间隔需要小于一秒,那么余额将有所不同-这项工作会困难得多。

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

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