본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] Invalid use of SingleClientConnManager: connection still allocated.

by 돼지왕 왕돼지 2012. 11. 9.
반응형


Invalid use of SingleClientConnManager: connection still allocated.

- 연속적으로 HttpClient object 를 사용할 경우 간혹 발생하는 Exception 이다. full error 는 아래와 같다.

11-08 16:19:03.466: W/SingleClientConnManager(24709): Invalid use of SingleClientConnManager: connection still allocated.

11-08 16:19:03.466: W/SingleClientConnManager(24709): Make sure to release the connection before allocating another one.

11-08 16:19:03.476: E/[Teamable]HttpManager(24709): statusCode=200

11-08 16:19:03.486: E/[Teamable]HttpManager(24709): Exception in [getGetResponse]

11-08 16:19:03.486: W/System.err(24709): java.lang.NullPointerException

11-08 16:19:03.506: W/System.err(24709):  at org.apache.http.impl.conn.SingleClientConnManager.revokeConnection(SingleClientConnManager.java:373)

11-08 16:19:03.506: W/System.err(24709):  at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:218)

11-08 16:19:03.536: E/[Teamable]HttpManager(24709): result=[]

11-08 16:19:03.536: W/System.err(24709):  at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:189)

11-08 16:19:03.536: W/System.err(24709):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:325)

11-08 16:19:03.536: W/System.err(24709):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

11-08 16:19:03.536: W/System.err(24709):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

11-08 16:19:03.536: W/System.err(24709):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

11-08 16:19:03.536: W/System.err(24709):  at android.os.AsyncTask$2.call(AsyncTask.java:185)

11-08 16:19:03.536: W/System.err(24709):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

11-08 16:19:03.536: W/System.err(24709):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)

11-08 16:19:03.536: W/System.err(24709):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)

11-08 16:19:03.536: W/System.err(24709):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

11-08 16:19:03.536: W/System.err(24709):  at java.lang.Thread.run(Thread.java:1027)





원인

- Default HttpClient 는 SingleClientConnManager 가 기본적으로 연결되어 있는데, 해당 Connection Manager 는 말 그대로 single connection 만을 지원한다. 하지만, 기존의 Connection 이 제대로 종료되기 전에 새로운 Connection 을 연결하기 때문에 해당 문제가 발생하는 것이다.





해결법

1. 여러개의 Connection 을 지원하기 위해 ThreadSafeClientConnManger 연결.

HttpClient httpClient = new DefaultHttpClient();

ClientConnectionManager clientConnectionManager = mHttpClient.getConnectionManager();

HttpParams params = mHttpClient.getParams();

mHttpClient = new DefaultHttpClient( new ThreadSafeClientConnManager( params, clientConnectionManager.getSchemeRegistry()), params );




2. 기존 커넥션을 확실히 닫아준다.

- 기존 커넥션을 확실히 닫는 것은 response 의 entity 에 있는 input stream 을 모두 consume 했을 때이다.

entity.consumeContents();



도움이 되셨다면 손가락 꾸욱~ ( 로그인 필요 x )



반응형

댓글