Subject Access to MON$ATTACHMENTS inside ON DISCONNEXT db trigger
Author Stefan
Hello.

I've got some strange problem when accessing table MON$ATTACHMENTS inside an ON DISCONNECT db trigger.

In an ON CONNECT db trigger I create a new line in a table with the CURRENT_CONNECTION integer as the primary key, like this:

CREATE TRIGGER DB_ON_CONNECT_0
ACTIVE ON CONNECT POSITION 0
AS
DECLARE VARIABLE M_I INTEGER;
BEGIN
BEGIN
DELETE FROM TPV_DATOS_CONEXION
WHERE ID_CONEXION = CURRENT_CONNECTION;
INSERT INTO TPV_DATOS_CONEXION (
ID_CONEXION
) VALUES (
CURRENT_CONNECTION
);
WHEN ANY DO BEGIN
M_I = 0;
END
END
END

Then, in the disconnect trigger I delete the record **and any orphan record that may exist in my table**:

CREATE TRIGGER DB_ON_DISCONNECT_0
ACTIVE ON DISCONNECT POSITION 0
AS
DECLARE VARIABLE M_ID_CONEXION INTEGER;
BEGIN
DELETE FROM TPV_DATOS_CONEXION
WHERE ID_CONEXION = CURRENT_CONNECTION;
/* Borrar conexiones 'orfanatas' */
BEGIN
FOR SELECT A.ID_CONEXION
FROM TPV_DATOS_CONEXION A
LEFT JOIN MON$ATTACHMENTS B ON A.ID_CONEXION = B.MON$ATTACHMENT_ID
WHERE B.MON$ATTACHMENT_ID IS NULL
INTO M_ID_CONEXION
DO
DELETE FROM TPV_DATOS_CONEXION
WHERE ID_CONEXION = :M_ID_CONEXION;
WHEN ANY DO BEGIN
M_ID_CONEXION = 0;
END
END
END

The problem now is, that in one of our customers I can see connections that don't have any record in TPV_DATOS_CONEXION for
their CURRENT_CONNECTION.
It seems as if the disconnect trigger doesn't see all actual connections in MON$ATTACHMENTS. Or with other words: Is it possible that one connection is about to start, and has already created the record in the table TPV_DATOS_CONEXION, and at the same time another connection closes and does not see the opening connection in MON$ATTACHMENTS?

BTW, the environment is Firebird 2.5.1 SC on Ubuntu server.

Thanks for any hint.

Stefan