FirebirdSQL / jaybird

JDBC driver for Firebird
https://firebirdsql.org/en/jdbc-driver/
GNU Lesser General Public License v2.1
94 stars 23 forks source link

EventManager.isConnected() always true after connection #735

Closed silvafabio closed 1 year ago

silvafabio commented 1 year ago

Hi.

I'm using EventManager in asynchronous mode and sometimes the connection between application and firebird server may experience some rarely instability.

When I first instantiate FBEventManager and call connect method it's works as expected, connecting if everything is fine between application and firebird server, or throwin java.sql.SQLNonTransientConnectionException otherwise.

But after the first successfully connection the method isConnected always reports true, even if the connection was down.

It's turns impossible to detect when the connection is down and prevents (silently) the application to receive the events.

I'm using jaybird-jdk18:4.0.6.java8

Thanks

mrotteveel commented 1 year ago

This is by design, it reflects whether it was explicitly closed or not. I'll see if I can make some changes to detect broken connections.

silvafabio commented 1 year ago

Thanks for the quickly reply ;-)

As a workaround I'm reinstantiating the FBEventManager every 10 minutos.

    while (!interrupt) {
            if (eventManager == null || !eventManager.isConnected()) {
                // instantiate the FBEventManager
                // add listeners to eventManager
                nextEventManagerRenew = LocalDateTime.now().plusMinutes(10);
            }

            if(LocalDateTime.now().isAfter(nextEventManagerRenew)){
                try{
                    if(eventManager != null && eventManager.isConnected()){
                        eventManager.close();
                    }
                } catch (Exception e) {
                    // Log exception
                } finally {
                    eventManager = null;
                }
            }
    }

When I call eventManager.close() in a lost connection it's stuck forever.

This is another point to keep in mind when thinking in "some changes to detect broken connections".

Thanks again

mrotteveel commented 1 year ago

With Jaybird 5, you could try if setting setSoTimeout will improve detection of broken connections

silvafabio commented 1 year ago

I did a search in the repository source code and did not find a way to get access to setSoTimeout through FBEventManager.

It's only available at:

https://github.com/FirebirdSQL/jaybird/blob/0ff3dce1c7ccfa8925e90a7e46f206b2d3120032/src/main/org/firebirdsql/gds/ng/wire/WireConnection.java#L198

https://github.com/FirebirdSQL/jaybird/blob/e094382a964df93f3c555ce2547416c419ee8138/src/main/org/firebirdsql/ds/AbstractConnectionPropertiesDataSource.java#L163

https://github.com/FirebirdSQL/jaybird/blob/e094382a964df93f3c555ce2547416c419ee8138/src/main/org/firebirdsql/gds/ng/wire/AbstractWireOperations.java#L313

https://github.com/FirebirdSQL/jaybird/blob/e094382a964df93f3c555ce2547416c419ee8138/src/main/org/firebirdsql/jaybird/props/AttachmentProperties.java#L283

I'm using:

        eventManager = new FBEventManager();
        eventManager.setHost(host);
        eventManager.setUser(user);
        eventManager.setPassword(password);
        eventManager.setDatabase(database);
        eventManager.connect();
mrotteveel commented 1 year ago

It is available with Jaybird 5 on EventManager/FBEventManager, given you mention in your starting post you're using Jaybird 4.0.6, you will need to upgrade to Jaybird 5 first.

mrotteveel commented 1 year ago

After upgrading, you can call eventManager.setSoTimeout (e.g. eventManager.setSoTimeout(5000) for a timeout of 5000 milliseconds).

silvafabio commented 1 year ago

Ok, Sorry, now I see the heritage of AttachmentProperties.java

I'll give a try and let you know.

silvafabio commented 1 year ago

Upgrading to Jaybird 5.0.1 and using the setSoTimeout(5000) solves the eventManager.close() stucks forever part ;-)

Thanks

mrotteveel commented 1 year ago

You're welcome. I am working on some changes which should detect fatal connection errors and mark the event manager as not connected, but those will only be added to Jaybird 6.