If a running session loses a connection to a node, it tries to re-establish it according to a configurable policy. This is used in two places:
The reconnection policy controls the interval between each attempt. It is defined in the configuration:
datastax-java-driver { advanced.reconnection-policy { class = ExponentialReconnectionPolicy base-delay = 1 second max-delay = 60 seconds } }
ExponentialReconnectionPolicy is the default; it starts with a base delay, and then doubles it after each attempt. ConstantReconnectionPolicy uses the same delay every time, regardless of the previous number of attempts.
You can also write your own policy; it must implement ReconnectionPolicy and declare a public constructor with a DriverContext argument.
For best results, use reasonable values: very low values (for example a constant delay of 10 milliseconds) will quickly saturate your system.
The policy works by creating a schedule each time a reconnection starts. These schedules are independent across reconnection attempts, meaning that each pool will start with a fresh delay even if other pools are already reconnecting. For example, assuming that the pool size is 3, the policy is the exponential one with the default values, and the control connection is initially on node1:
If a session fails to connect when it is first created, the default behavior is to abort and throw an error immediately.
If you prefer to retry, you can set the configuration option advanced.reconnect-on-init
to true. Instead of failing, the driver will keep attempting to initialize the session at regular intervals, according to the reconnection policy, until at least one contact point replies. This can be useful when dealing with containers and microservices.
Note that the session is not accessible until it is fully ready: the CqlSessionBuilder.build()
call — or the future returned by buildAsync()
— will not complete until the connection was established.