You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Guillaume CHAUVET <gu...@qualiformed.com> on 2013/07/15 18:01:47 UTC

JDBC Driver : connection listener

Hi Derby Users,

We need to detect a connection lost between Derby (in server mode) and our swing client.
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 because we use an ORM framework who catches them :
=============
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;
                    }
                }
            }
        });
========

IMO, an interesting feature will be a listener pattern provided by the JDBC driver. In this way, if a DisconnectedException will be throws, we will can notify a connection lost to another observers.

Regards,
Guillaume

RE: JDBC Driver : connection listener

Posted by Guillaume CHAUVET <gu...@qualiformed.com>.
I written a DataSource wrapper as you explained and it works well. 
Thank you !

Regards,
--
Guillaume

Re: JDBC Driver : connection listener

Posted by Knut Anders Hatlen <kn...@oracle.com>.
Guillaume CHAUVET <gu...@qualiformed.com> writes:

> Hi Knut,
>
> Thank you for your reply. So now, I use the ClientConnectionPoolDataSource as DataSource into my OpenJPA configuration.
> But getPooledConnection[X](...) methods aren't invoked by the ORM,  only getConnection[X]() methods inherited from ClientBaseDataSource are invoked.

Right, it will only work if your application has enough control over how
the connections are created. It would have to call getPooledConnection()
on the data source, register a listener on the PooledConnection, and
then call getConnection() on the PooledConnection to get an actual JDBC
Connection instance.

> I don't know if this is the good behaviour, should getConnection[X](...) methods be overloaded into ClientConnectionPoolDataSource and return getPooledConnection[X](...). getConnection() ?

I'm not sure. The JDBC specification isn't very explicit on what
getConnection() should do in a ConnectionPoolDataSource.

But even if it did return getPooledConnection().getConnection(), the
Connection interface doesn't provide any method to get a handle to the
PooledConnection, so there wouldn't be any way to register the listener.

What might work, is if you write your own little DataSource class whose
getConnection() method calls
ClientConnectionPoolDataSource.getPooledConnection(), registers a
listener on the PooledConnection instance, and finally calls
getConnection() on the PooledConnection. The ORM might be able to use
that DataSource instead of Derby's DataSource.


-- 
Knut Anders

RE: JDBC Driver : connection listener

Posted by Guillaume CHAUVET <gu...@qualiformed.com>.
Hi Knut,

Thank you for your reply. So now, I use the ClientConnectionPoolDataSource as DataSource into my OpenJPA configuration.
But getPooledConnection[X](...) methods aren't invoked by the ORM,  only getConnection[X]() methods inherited from ClientBaseDataSource are invoked.

I don't know if this is the good behaviour, should getConnection[X](...) methods be overloaded into ClientConnectionPoolDataSource and return getPooledConnection[X](...). getConnection() ?

Regards,
Guillaume

Re: JDBC Driver : connection listener

Posted by Knut Anders Hatlen <kn...@oracle.com>.
Guillaume CHAUVET <gu...@qualiformed.com> writes:

> IMO, an interesting feature will be a listener pattern provided by the
> JDBC driver. In this way, if a DisconnectedException will be throws,
> we will can notify a connection lost to another observers.

Hi Guillaume,

If you use org.apache.derby.jdbc.ClientConnectionPoolDataSource to
connect to the database, you can register a ConnectionEventListener that
will be notified if the connection to the server is lost:

http://docs.oracle.com/javase/7/docs/api/index.html?javax/sql/ConnectionEventListener.html


-- 
Knut Anders