# [FAQ] java 连接 TiDB Cloud 报错 Communications link failure 如何解决

## Key Words

Communications link failure, TiDB Cloud

## Description

1. Java 客户端详细报错如下：  

    ```java
    07:19:52 [WARN] [Agent 9] ignore error JavaException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last packet successfully received from the server was 238 milliseconds ago.  The last packet sent successfully to the server was 238 milliseconds ago.
    07:19:52 [WARN] [Agent 85] ignore error JavaException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last packet successfully received from the server was 226 milliseconds ago.  The last packet sent successfully to the server was 226 milliseconds ago.
    07:19:52 [INFO] [Progress] 871 sec, 1430 tps, 1446579 tx
            at org.apache.commons.dbcp2.PoolableConnectionFactory.destroyObject(PoolableConnectionFactory.java:151)
            at org.apache.commons.pool2.impl.GenericObjectPool.destroy(GenericObjectPool.java:958)
            at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:561)
            at org.apache.commons.dbcp2.PoolableConnection.close(PoolableConnection.java:184)
            at org.apache.commons.dbcp2.DelegatingConnection.closeInternal(DelegatingConnection.java:176)
            at org.apache.commons.dbcp2.DelegatingConnection.close(DelegatingConnection.java:155)
            at org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:58)
            at jdbcrunner.Agent.callRun(Agent.java:247)
            at jdbcrunner.Agent.run(Agent.java:108)
            at java.lang.Thread.run(Thread.java:750)
    07:19:52 [WARN] [Agent 417] ignore error JavaException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 
    ```

2. 看报错 `The last packet successfully received from the server was 238 milliseconds ago`, 依据[官方文档](https://docs.pingcap.com/tidb/dev/java-app-best-practices#probe-configuration)描述，与非活跃连接现象无法对应。

## Content

1. TiDB Cloud 目前支持 `SQL Web Shell`, `VPC Peering`, `Public NLB`, `PrivateLink` 四种连接方式，其中只有 `SQL Web Shell` 一种没有通过 TiDB 的 LB 连接，通过 LB 连接 TiDB Cloud 会因 AWS NLB 的跨 AZ 负载均衡的特殊性报错。
2. 在同时开启跨 AZ 负载均衡和保留源 IP 时，对短连接请求会出现同源 IP 同 client 端口的先后两次请求被不同的 LB node 转发到同一个 tidb-server 上，导致 tidb-server 看到正在关闭的连接突然客户端又来了同样的请求，从而认为是非法请求而出现 connection reset 现象。  
3. 该问题属于 AWS NLB 既有问题，在 NLB cross az 功能和 NLB 保留客户端 ip 选项同时开启时，且并发数稍高后容易出现该问题。  
4. Workaround : 关闭 cross az 或关闭后端实例组的保留客户端 ip 选项，二者任选其一即可，对于 TIS 推荐后者绕过方式。

## Affected Versions

ALL