Subject Multiple-Readers for a What's New Queue Query
Author fb2me
I have the following table connected to a generator/trigger:

CREATE TABLE QUEUE1 (Q_ID INTEGER NOT NULL,
Q_TIME TIMESTAMP default 'now',
Q_TEXT VARCHAR(50),
CONSTRAINT Q_PK1 PRIMARY KEY (Q_ID));

CREATE GENERATOR IDQ;
SET TERM ^ ;
CREATE TRIGGER QUEUE1_BI FOR QUEUE1 ACTIVE BEFORE INSERT POSITION 0 AS
BEGIN
IF (New.q_id IS NULL) THEN New.q_id = Gen_Id(IdQ,1);
END ^

Multiple/different programs insert (write) rows to Queue1 at any
time. What I
want to be able to do is have multiple/different programs selecting
(read)
only new data from Queue1. At first I thought I could simply keep a
last
max q_id for each reader and then select rows greater than this value,
ala this pseudocode:

get LastMaxQId for this reader
select max(q_id) from Queue1; //store this in MaxQId
select q_text from Queue1 where q_id > :LastMaxQId and q_id
<= :MaxQId;
set reader's LastMaxQId to MaxQId

However I will run into problems if a reader executes before
one of the writers does a commit. For instance, the following
timeline:

writer1: insert into Queue1 (q_text) VALUES ('reader1 will not see
me');
writer2: insert into Queue1 (q_text) VALUES ('reader1 will see me');
writer2: commit;
reader1: does the above select and gets the Q_Id value from
writer2's insert, which is one more than writers'1 Q_Id
writer1: commit;

Now reader1 will never select the insert made by writer1.

If there were only one reader I believe I could do it by deleting
records
that reader1 selected from Queue1. However with more than one reader
I'm not sure what's the best way to keep track of what records each
reader has
selected? (One thought I had would be if there were a trigger that
fired
on a commit...)