What SVNKit resources should be disposed?

Like in a previous post about SVNKit objects reusability I’d like to write about the most common mistakes related to resources closing.

SVNRepository instances should be closed

SVNRepository class of SVNKit represents a connection with a remote or maybe local repository. It has SVNRepository#closeSession method that closes that connection. Unfortunately people often forget to call it.

If SVNRepository instance was constructed using not SVNRepositoryFactory but using ISVNRepositoryPool it should be closed if and only if it was constructed by passing mayReuse=false to ISVNRepositoryPool#createRepository. Otherwise the repository is controlled by the pool and should be disposed together with the pool (by calling ISVNRepositoryPool#dispose).

For example, these ways of closing of SVNRepository are correct:

final SVNRepository svnRepository = SVNRepositoryFactory.create(url);
svnRepository.closeSession();

Non-reusable repository connection should be closed explicitly:

final ISVNRepositoryPool repositoryPool = new DefaultSVNRepositoryPool(null, null);
try {
    final SVNRepository svnRepository = repositoryPool.createRepository(url, false);
    svnRepository.closeSession();
} finally {
    repositoryPool.dispose();
}

Reusable repository connection will be closed together with the connections pool:

final ISVNRepositoryPool repositoryPool = new DefaultSVNRepositoryPool(null, null);
try {
    final SVNRepository svnRepository = repositoryPool.createRepository(url, true);
} finally {
    repositoryPool.dispose();
}

And of course all ISVNRepositoryPool instances should be always disposed in finally-block.

SVNClientManager instances should also be disposed with dispose() method

I would say that this is the most common mistake. As I mentioned in a previous post, SVNClientManager aggregates an ISVNRepositoryPool instance and implements this interface. And of course it should be disposed, otherwise all connections it creates won’t be closed

All other classes that have dispose() or close() methods should be disposed

Actually this goes without saying, I just remind about that evident fact.

Comments are closed.