@@ -170,9 +170,8 @@ public void init(ClickHouseConfig config) {
170170 this .config = config ;
171171 if (this .executor == null ) { // only initialize once
172172 int threads = config .getMaxThreadsPerClient ();
173- this .executor = threads <= 0 ? ClickHouseClient .getExecutorService ()
174- : ClickHouseUtils .newThreadPool (getClass ().getSimpleName (), threads ,
175- config .getMaxQueuedRequests ());
173+ this .executor = threads < 1 ? ClickHouseClient .getExecutorService ()
174+ : ClickHouseUtils .newThreadPool (this , threads , config .getMaxQueuedRequests ());
176175 }
177176
178177 initialized = true ;
@@ -196,27 +195,16 @@ public final void close() {
196195 try {
197196 server = null ;
198197
199- if (executor != null ) {
200- executor .shutdown ();
201- }
202-
203198 if (connection != null ) {
204199 closeConnection (connection , false );
200+ connection = null ;
205201 }
206202
207- // shutdown* won't shutdown commonPool, so awaitTermination will always time out
208- // on the other hand, for a client-specific thread pool, we'd better shut it
209- // down for real
210- if (executor != null && config .getMaxThreadsPerClient () > 0
211- && !executor .awaitTermination (config .getConnectionTimeout (), TimeUnit .MILLISECONDS )) {
212- executor .shutdownNow ();
203+ // avoid shutting down shared thread pool
204+ if (executor != null && config .getMaxThreadsPerClient () > 0 && !executor .isTerminated ()) {
205+ executor .shutdown ();
213206 }
214-
215207 executor = null ;
216- connection = null ;
217- } catch (InterruptedException e ) {
218- log .warn ("Got interrupted when closing client" , e );
219- Thread .currentThread ().interrupt ();
220208 } catch (Exception e ) {
221209 log .warn ("Exception occurred when closing client" , e );
222210 } finally {
@@ -226,7 +214,7 @@ public final void close() {
226214 closeConnection (connection , true );
227215 }
228216
229- if (executor != null ) {
217+ if (executor != null && config . getMaxThreadsPerClient () > 0 ) {
230218 executor .shutdownNow ();
231219 }
232220 } finally {
0 commit comments