| 1 | ## Connection heartbeat |
| 2 | |
| 3 | If connections stay idle for too long, they might be dropped by |
| 4 | intermediate network devices (routers, firewalls...). Normally, TCP |
| 5 | keepalive should take care of this; but tweaking low-level keepalive |
| 6 | settings might be impractical in some environments. |
| 7 | |
| 8 | The driver provides application-side keepalive in the form of a |
| 9 | connection heartbeat: when a connection has been idle for a given amount |
| 10 | of time, the driver will simulate activity by writing a dummy request to |
| 11 | it. |
| 12 | |
| 13 | This feature is enabled by default. The default heartbeat interval is 30 |
| 14 | seconds, it can be customized through `PoolingOptions`: |
| 15 | |
| 16 | ```java |
| 17 | cluster.getConfiguration().getPoolingOptions() |
| 18 | .setHeartbeatIntervalSeconds(60); |
| 19 | ``` |
| 20 | |
| 21 | This can be changed at runtime, but only connections created after that |
| 22 | will use the new interval. Most users will want to do this at startup: |
| 23 | |
| 24 | ```java |
| 25 | Cluster cluster = Cluster.builder() |
| 26 | .addContactPoint("127.0.0.1") |
| 27 | .withPoolingOptions(new PoolingOptions() |
| 28 | .setHeartbeatIntervalSeconds(60)) |
| 29 | .build(); |
| 30 | ``` |
| 31 | |
| 32 | The heartbeat interval should be set higher than |
| 33 | [SocketOptions.readTimeoutMillis](http://www.datastax.com/drivers/java/2.1/com/datastax/driver/core/SocketOptions.html#getReadTimeoutMillis()): |
| 34 | the read timeout is the maximum time that the driver waits for a regular |
| 35 | query to complete, therefore the connection should not be considered |
| 36 | idle before it has elapsed. |
| 37 | |
| 38 | To disable heartbeat, set the interval to 0. |
| 39 | |
| 40 | Implementation note: the dummy request sent by heartbeat is an |
| 41 | [OPTIONS](https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v3.spec#L278) |
| 42 | message. |