The behavior regarding these configuration properties might be dependent on implementation.
BagOfConnectionsConnectionPoolImpl
BagOfConnectionsConnectionPoolImpl
is the only implementation at the moment that honors both these properties. It behaves as follows:
Connection is borrowed from the pool on every cassandra operation (query or mutation) and returned to pool upon completion of operation.
maxConnsPerHost
- maximum number of connections per single cassandra host.
maxConns
- maximum number of connections in the pool.
Both these numbers must be positive, so setMaxConns(-1)
just won't work.
On the attempt to borrow a connection from pool, the pool checks active connection number against maxConns
. If the limit is exceeded, it waits until some connection is released. If no connection is available in specified timeout, the pool throws PoolTimeoutException
.
If maxConns
limit is not exceeded, the pool attempts to find a cassandra host it's aware of (specified as seed or found during discovery) that has the number of active connections below maxConnsPerHost
and connect to it. If all hosts reached connection limit, the pool throws NoAvailableHostsException
.
For example, let's take a client that connects to cluster of 4 nodes:
setMaxConns(100); setMaxConnsPerHost(10)
: Effective maximum number of connections is 40 (10 connections per node, no further connection attempts will be made). NoAvailableHostsException
will be thrown.
setMaxConns(20); setMaxConnsPerHost(10)
: Effective maximum number of connections is 20. The connections to different hosts will be distributed uniformly, but not necessary equally. PoolTimeoutException
will be thrown.
Things get more complicated if nodes join or leave cluster, but general idea is the same.
TokenAwareConnectionPoolImpl & RoundRobinConnectionPoolImpl
Both TokenAwareConnectionPoolImpl
& RoundRobinConnectionPoolImpl
ignore maxConns
configuration property. They just select a host (depending on row token or randomly) and attempt to connect to it.
If the number of active connections to that host exceeds maxConnsPerHost
, the pool waits until some connection is released. If no connection is available during specified timeout, another connection attempt to (potentially) another host is executed as a part of failover.