Fixing The “Invalid use of SingleClientConnManager: connection still allocated” Problem
Posted by blogmeister on
May 5, 2011
I came upon this problem because the site I wanted to access had server problems, meaning the website was down. When my code tried to access it again, it gave out this “Invalid use of SingleClientConnManager: connection still allocated” exception message.
My code uses Apache’s HttpClient 3rd party library and I thought at first calling the consumeContent() method of the HttpEntity class did the trick since I never even closed the InputStream when getting a reference to it. It did not.
Luckily, after much surfing I stumbled upon Jason Hudgin’s site where he created a convenience method that avoids this problem.
|
1 2 3 4 5 6 7 8 9 10 |
public static DefaultHttpClient getThreadSafeClient() { DefaultHttpClient client = new DefaultHttpClient(); ClientConnectionManager mgr = client.getConnectionManager(); HttpParams params = client.getParams(); client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params); return client; } |











3 Responses to “Fixing The “Invalid use of SingleClientConnManager: connection still allocated” Problem”
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
By Patrick Chung on Jan 11, 2012
To Patrick:
I check your solution. I doesn’t work. Problem still appears.
By Alex on Jan 11, 2012
Excellent!
By nikolas7_7 on Feb 19, 2013