I created https://issues.apache.org/jira/browse/DERBY-5560 and am seeing this in production.   


Basically what is happening is that the LogicalConnection.close() is being called which attempts to recycle the physical connection by calling ClientPooledConnection.recycleConnection().  At the same time ClientPooledConnection.close() is being called which attempts to call LogicalConnection.nullPhysicalConnection().    The first thread holds a lock on LogicalConnection and needs the lock on ClientPooledConnection and the second thread holds a lock on ClientPolledConnection and needs a lock on LogicalConnection and a deadlock occurs.


This is occurring because of the configuration of the connection pool in use (ClienXADataSource) and the pool is configured to close all connections on any error.   The stack trace of the deadlock as a transaction being committed by the first thread and the connection pool closing all threads on a detected error in the second thread.


I don’t see immediately how to synchronize in an orderly way to eliminate the deadlock.  The first thread has a handle on the LogicalConnection which references the physical ClientPooledConnection.  The second thread has a handle on the physical ClientPooledConnection which references the LogicalConnection.


One thought is to change the LogicalConnection.close to be something like:


    public void close() throws SQLException {

       synchronized(phsyicalConnection) {





   synchronized public void _close() throws SQLException {



            // we also need to loop thru all the logicalStatements and close them

            if (physicalConnection_ == null) {



            if (physicalConnection_.agent_.loggingEnabled()) {

                physicalConnection_.agent_.logWriter_.traceEntry(this, "close");



            if (physicalConnection_.isClosed()) // connection is closed or has become stale


                pooledConnection_.informListeners(new SqlException(null,

                    new ClientMessageId(


            } else {




                if (!physicalConnection_.isGlobalPending_()) {




            physicalConnection_ = null;



        catch ( SqlException se )


            throw se.getSQLException();




but this has a problem if “physicalConnection” is already null.

Any guidance will be greatly appreciated as I need to patch Derby to work around this issue in any case.