asfernandes / node-firebird-drivers

Node.js Firebird Drivers
MIT License
53 stars 17 forks source link

Event reconnection #148

Closed arthurbolsoni closed 12 hours ago

arthurbolsoni commented 1 month ago

Currently I have been using post events to receive notifications when records are changed in Firebird for integrations, however in some cases when the connection with the database is lost, a reconnection does not occur.

My question is whether it is possible to implement this and what the steps are

asfernandes commented 1 month ago

Se perdeu a conexão você tem que detectar e reconectar. Não é tarefa da lib isso, assim como não é da fbclient.

arthurbolsoni commented 1 month ago

Essa é a questão, como saber quando a conexão cai? A conexão socket (auxiliar) é feito dentro do c++ library. Mas por se tratar de um listener eu nunca vou saber que a conexão caiu, porque eu nunca recebo um evento para me alertar ou uma variavel/funcao para verificar o status

Eu vi que a lib possuem um "isValid" para verificar se está conectado, porém, mesmo desconectado ela não altera o status;

Eu pensei em checar a cada X segundos através de uma query na conexão principal. Porem isso seria um pooling no banco, abrindo e fechando transações.

Eu estive usando o node-firebird (uma alternativa um pouco instável), quando a conexão socket é fechada eu recebo um evento, é possível fazer isso usando a lib c++ ou o fbclient? eu pergunto isso porque não tenho conhecimento com c++

asfernandes commented 1 month ago

Você pode fazer com setInterval e usando o mesmo Attachment iniciar e fechar uma transação, por enquanto.

Existe um método ping que pode ser exposto em uma pŕoxima versão e evitar ter que criar transação.

isValid é apenas para detectar se a conexão já foi fechada via código.

marcin-bury commented 1 month ago

Care to continue in English? What would be the best direction to reconnect broken connection? Since firebird looses the connection in case of some kind of network malfunction, how in optimal way check if attachment object still has "alive" connection to the FB server?

arthurbolsoni commented 1 month ago

Você pode fazer com setInterval e usando o mesmo Attachment iniciar e fechar uma transação, por enquanto.

Existe um método ping que pode ser exposto em uma pŕoxima versão e evitar ter que criar transação.

isValid é apenas para detectar se a conexão já foi fechada via código.

Is this ping for secondary connection? Because even pinging/pooling the primary connection, the secondary connection can still drop. Is it possible to expose the method/function/object that makes the connection in C++? believe that this is the best way, if there is no other for secondary connection.

asfernandes commented 1 month ago

What do you mean by primary/secondary connection?

It's only the user who makes connections.

The methods are already present, but not in TypeScript typings. So you can call them, but changes may break your code since they are not part of the public interface.

arthurbolsoni commented 1 month ago

I spent some time studying how node-firebird worked to implement post_event in my project, there are 2 connections made, one of them is the primary via port 3050 and the other is auxiliary via port 3051 (or random by default), the auxiliary connection receives only POST_EVENTS, while the primary records them.

https://firebirdsql.org/file/documentation/html/en/firebirddocs/wireprotocol/firebird-wire-protocol.html#wireprotocol-events

asfernandes commented 1 month ago

Good point. But note that node-firebird connects to Firebird with nodejs sockets, while this lib uses fbclient, which may not even use sockets for connection. It does not expose such things even for C/C++ applications.

And there is the ping function, but it only checks the "primary" connection, not the event one. I think this should be improved in Firebird side.

asfernandes commented 1 month ago

https://github.com/FirebirdSQL/firebird/issues/8122