Page 1 of 1

ZAbstractConnection Disconnect performance issue with TZDbcPooledConnection

Posted: 30.06.2021, 12:49
by stoffman
Hi,
There is a performance issue and potential bug related to TZDbcPooledConnection when disconnecting a connection. I would like to propose a small change that will help a lot.
First some background: ZAbstractConnection.Disconnect has the following code:

Code: Select all

    
    try
      FConnection.Close;
      FConnection.SetOnConnectionLostErrorHandler(nil);
    finally
      FConnection := nil;
    end;
FConnection is TZDbcPooledConnection so the following happen:
1. FConnection.Close - returns the underlying connection to the connection pool.
2. FConnection.SetOnConnectionLostErrorHandler - now because it doesn't have a connection it acquires a new connection that may be unrelated to the connection that was just returned to the pool.
3. FConnection := nil - because it is an interface, it will destroy the object which in turn will return the underlying connection to the pool

The proposed change is very simple:
call FConnection.SetOnConnectionLostErrorHandler(nil); before calling FConnection.Close
I did some basic testing it looks like it doesn't affect other drivers (sqlite)

Comments?

Re: ZAbstractConnection Disconnect performance issue with TZDbcPooledConnection

Posted: 01.07.2021, 09:15
by marsupilami
Hmmm - I think, it should be different. The pooled connection should remove the disconnect handler from the connection that is retuned to te pool before returning it. Also it should just do nothing if the connection is already returned to the pool.

Re: ZAbstractConnection Disconnect performance issue with TZDbcPooledConnection

Posted: 01.07.2021, 09:35
by marsupilami
I implemented a possible solution. Could you please check the latest svn?

Re: ZAbstractConnection Disconnect performance issue with TZDbcPooledConnection

Posted: 03.07.2021, 18:26
by stoffman
For my use case it looks good. thanks.