You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Guillaume CHAUVET <gu...@qualiformed.com> on 2013/07/17 10:18:38 UTC

Detecting database connection lost

Hi OpenJPA Users,

We need to detect a connection lost between Derby (in server mode) and our swing client that uses OpenJPA 2.x.
Our goal is to provide a more user friendly message (with ping button, a troubleshooting procedure and more).

Right now, we use the below pattern to detect some connection lost, but some of them were not catched :
=============
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable thrwbl) {
                for (Throwable cause = thrwbl.getCause(); cause != null; cause = cause.getCause()) {
                    if (cause instanceof DisconnectException) {
                        fireDisconnected();
                        break;
                    }
                }
            }
        });
========

I discussed on the Derby User ML that replied me I should use a < ClientConnectionPoolDataSource > and register a ConnectionEventListener that will be notified if the connection to the server is lost.

So, I configured the DS (without errors at runtime) and I added a listener like this :
========
DecoratingDataSource dds = (DecoratingDataSource) ((OpenJPAConfiguration) emf.getConfiguration()).getConnectionFactory();
            ClientConnectionPoolDataSource ds = (ClientConnectionPoolDataSource) dds.getDelegate();
            ds.getPooledConnection().addConnectionEventListener(new ConnectionEventListener() {
                @Override
                public void connectionClosed(ConnectionEvent ce) {
                }

                @Override
                public void connectionErrorOccurred(ConnectionEvent ce) {
                    fireDisconnected(); // <===== expected call
                }
            });
========

But the listener is never called...
Would I forget something ? (surely !)

Regards,
Guillaume