Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
1 reply Latest Post - ‏2011-04-21T13:15:00Z by SystemAdmin
SystemAdmin
SystemAdmin
9224 Posts
ACCEPTED ANSWER

Pinned topic Closing connections of Manager

‏2011-03-30T11:35:52Z |
NOTE: this thread was migrated from projectzero.org. Some content and formatting may have been lost in the move.


Originally posted by projectzero userid: edgraaff - f=4&t=2298#p9346
Since we switched over to DBCP for connection pooling we encounter hanging connections to our database. Obviously some of the connections we create, aren't closed, leaving the pool with no available connections anymore.

Digging into the source of zero.data.Manager, I notice that methods like query, queryList and queryFirst end with a closeConnection in the finally clause. But queryIterator and queryResults do not (which is expected behaviour ofcourse).

Unfortunately, we've written some code that utilizes queryResults and does not close the connection. To manage this, I can think of two sollutions:

  • just close() the Manager after queryIterator or queryResults
  • somehow manage it, so that by the end of the requests all open connections are closed.

I prefer the second one over the first one, as it prevents a common coding mistake when using either queryIterator or queryResults. Besides, it is okay to leave the connection open if you need it again.

To somehow manage this, I made the following changes:

  • All Manager using classes are using one class to obtain it (for example: DB2DAOFactory.getManager()), which 'caches' the Manager of this request in the globalcontext under /request/manager.
  • A custom zone handler for the request zone, MyRequestHandler, extends the default RequestHandler. This checks, upon deactivation, if a Manager exists under /request/manager and closes it's connection.
  • IF the connection is already closed, zero.data.Manager logs an entry in severe (why?) that the connection already is closed. There seems no way of checking if connection != null before calling close, so I've written MyManager, which extends Manager, and just adds public boolean isConnected() { return connection != null; }. Not really the sollution I prefer.

I notice that the request zone handler is a thread local, which is deactivated when every request ends, so I think this is a nice moment to find out if there's any connection to be closed.

For a better management of this, I suggest one of the following:

  • Please add comments in zero.data.Manager to queryList, queryIterator and all others which do not close the connection by itself, to warn the developer that he/she has to close the connection himself. Without this warning, and being used to Manager closing connections by default, it's a very common mistake to forget to close it.
  • Change or remove the logging in closeConnection, when there does not seem to be a connection. The intention is to get rid of the connection, it doesn't matter if it's already gone.

Thanks,
Edgar
~~
Updated on 2011-04-21T13:15:00Z at 2011-04-21T13:15:00Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    9224 Posts
    ACCEPTED ANSWER

    Re: Closing connections of Manager

    ‏2011-04-21T13:15:00Z  in response to SystemAdmin

    Originally posted by projectzero userid: edgraaff - f=4&t=2298#p9364
    blah