This repository was archived by the owner on Jun 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Connection lifetime & pooling
Oleg V. Kozlyuk edited this page Dec 22, 2021
·
6 revisions
ClickHouse.Client uses System.Net.Http.HttpClient under the hood. HttpClient has a per-endpoint connection pool. As a consequence:
- a
ClickHouseConnectionobject does not have 1:1 mapping to TCP connections - multiple database sessions will be multiplexed through several (2 by default) TCP connections per server - connections can stay alive after
ClickHouseConnectionobject was disposed - this behavior can be tweaked by passing a bespoke
HttpClientwith customHttpClientHandler
For DI environments, there is a bespoke constructor ClickHouseConnection(string connectionString, IHttpClientFactory httpClientFactory, string httpClientName = "") which allows to generalize HTTP client settings
- A
ClickHouseConnectionrepresents a "session" with the server. It performs feature discovery by querying server version (so there is a minor overhead on opening), but generally it is safe to create and destroy such objects multiple times - Recommended lifetime for a connection is one connection object per large "transaction" spanning multiple queries. There is a minor overhead on connection startup, so it's not recommended to create a connection object for each query
- If an application operates on large volumes of transactions and requires to create/destroy
ClickHouseConnectionobjects often, it is recommended to useIHttpClientFactoryor a static instance ofHttpClientto manage connections