目录

Caused-by-java.net.SocketTimeoutException-Read-timed-out

Caused by: java.net.SocketTimeoutException: Read timed out;

Cause: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.; An I/O error occurred while sending to the backend.; nested exception is org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.

Caused by: org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.

Caused by: java.net.SocketTimeoutException: Read timed out;

https://i-blog.csdnimg.cn/direct/6159088e5f0f4bc68e5680a94cd3b1de.png

最近在做一个复杂查询,数据量比较大,需要二十多秒才能返回得结果,提交了请求之后还没等结果吐出来就报错了,发现是因为没有添加超时配置,所以在查询又或者读取的时候超时了,我这里是读取的时候超时了,既然超时了直接把超时时间延长就好了,我用的是druid(德鲁伊)

在数据库url补上两个参数 socketTimeout=300000&loginTimeout=60000

spring:
  datasource:
    url: jdbc:postgresql://localhost:3306/yourdb?socketTimeout=300000&loginTimeout=60000


如果你的前端用nginx反向代理的话,你可能会有504的报错,因为查询和读写都花了这么长时间,nginx有概率也会超时,所以如果504的话,nginx的超时时间也要设置延长一些

在 在nginx的配置文件  http/server/location 中都可以设置 / 根据你架构放置

proxy_connect_timeout 60s;
proxy_send_timeout    300s;
proxy_read_timeout    300s;
send_timeout          300s;

若是大文件或流式响应,考虑关闭 proxy_buffering

proxy_buffering off;