Chris2018998 / beecp-starter

beecp starter on springboot
Apache License 2.0
35 stars 12 forks source link

经常卡住 加载数据源的时候 #10

Open liangbaika opened 3 years ago

liangbaika commented 3 years ago

image

经常卡住 加载数据源的时候 经常卡在这一行

Chris2018998 commented 3 years ago

导致这个卡壳的原因如下

为了提高性能,连接池在启动的时候,会尝试去创建一个连接,这个导致卡住原因。驱动底层实现是使用Socket去连接数据服务,可能是由于网络原因或服务反应得问题,导致连接请求发出后得不到回应,所以。。。

解决办法加入 Socket Connect Timeout的参数,关于这个参数,不同驱动名字可能不一样的,可以采用类似下面的代码

mysql URL jdbc:mysql:xxxxx?useUnicode=true&characterEncoding=UTF8&connectTimeout=60000&socketTimeout=60000

PG URL jdbc:postgresql://localhost/test?user=fred&password=secret&&connectTimeout=60&socketTimeout=60

Chris2018998 commented 3 years ago

也可以这样配置

spring: datasource: ds1: connectProperties: socketTimeout=6000;connectTimeout=6000

liangbaika commented 3 years ago

配置的连接数越多 卡住的时间越久? 该优化啊 ------------------ 原始邮件 ------------------ 发件人: "欧德"<notifications@github.com> 发送时间: 2021年2月1日(星期一) 晚上10:55 收件人: "Chris2018998/BeeCP-Starter"<BeeCP-Starter@noreply.github.com>; 抄送: "凉白开"<1144388620@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Chris2018998/BeeCP-Starter] 经常卡住 加载数据源的时候 (#10)

liangbaika commented 3 years ago

总是报这个

2021-02-02 09:47:18.373 [restartedMain] ERROR [c.b.b.d.BeeDataSourceSetFactory:144] [] - Failed to get Connection: java.sql.SQLException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

image

Chris2018998 commented 3 years ago

针对这个问题,建议在客户机器上做两个测试

1:使用mysql客户UI客户端工具连接数据库,看看是否存在迟缓情况。

2: 编写一段获取连接JDBC代码段(运行在抛出异常的机器上,比如循环1000次获取/关闭),看看是否也抛出问题。

String username="xxxx";
String password="xxxx";
String url ="xxxxx";
Connection con=null;
for(int i=0;i<1000;i++){
    try{
       con = DriverManager.getConnection(url, username, password);
     }catch(Exception e){
       e.printStack();
    }finally{
        if(con!=null){ con.close();}
         con=null;
    }
}
liangbaika commented 3 years ago

以为你做了心跳 自动保活呢------------------ 原始邮件 ------------------ 发件人: "欧德"<notifications@github.com> 发送时间: 2021年2月2日(星期二) 晚上10:13 收件人: "Chris2018998/BeeCP-Starter"<BeeCP-Starter@noreply.github.com>; 抄送: "凉白开"<1144388620@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Chris2018998/BeeCP-Starter] 经常卡住 加载数据源的时候 (#10)

liangbaika commented 3 years ago

我连着navicat都很流畅的 但是你这个池子用了 总会报那个异常 启动的时候很容易触发 druid不会有这个问题------------------ 原始邮件 ------------------ 发件人: "欧德"<notifications@github.com> 发送时间: 2021年2月2日(星期二) 晚上10:13 收件人: "Chris2018998/BeeCP-Starter"<BeeCP-Starter@noreply.github.com>; 抄送: "凉白开"<1144388620@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Chris2018998/BeeCP-Starter] 经常卡住 加载数据源的时候 (#10)

Chris2018998 commented 3 years ago

在URL后面追加 &connectTimeout=6000&socketTimeout=6000 看看是否有改善。

这里有一个地址,不知道是否可以解决这个问题 https://blog.csdn.net/gaojing2240/article/details/77336287/

liangbaika commented 3 years ago

并没有的 ------------------ 原始邮件 ------------------ 发件人: "欧德"<notifications@github.com> 发送时间: 2021年2月2日(星期二) 晚上10:44 收件人: "Chris2018998/BeeCP-Starter"<BeeCP-Starter@noreply.github.com>; 抄送: "凉白开"<1144388620@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Chris2018998/BeeCP-Starter] 经常卡住 加载数据源的时候 (#10)

liangbaika commented 3 years ago

建议加心跳保活 看看有没有必要------------------ 原始邮件 ------------------ 发件人: "欧德"<notifications@github.com> 发送时间: 2021年2月2日(星期二) 晚上10:44 收件人: "Chris2018998/BeeCP-Starter"<BeeCP-Starter@noreply.github.com>; 抄送: "凉白开"<1144388620@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Chris2018998/BeeCP-Starter] 经常卡住 加载数据源的时候 (#10)

Chris2018998 commented 3 years ago

好的,池中有个定时器,用于检查连接的存活性

Chris2018998 commented 3 years ago

可以用下面这段代码测试一下您客户机到数据库通讯情况吗?

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class TestSocket {
    public static void main(String[]args){
        testSocket("dbhost",3306);//将参数换成db host的机器和端口
    }
    private static void testSocket(String host,int port){
        Socket socket = new Socket();
        long time1=System.currentTimeMillis();
        try {
            InetSocketAddress address = new InetSocketAddress(host, port);
            socket.connect(address);
            socket.close();
        }catch(Exception e) {
        }finally {
            long time=System.currentTimeMillis()-time1;
            if(socket.isConnected()){
                System.out.println("连接成功,耗时:"+time+"毫秒");
            }else{
                System.out.println("连接失败,耗时:"+time+"毫秒");
            }
        }
    }
}
liangbaika commented 3 years ago

好的 最近比较忙 等我后边空了试下------------------ 原始邮件 ------------------ 发件人: "欧德"<notifications@github.com> 发送时间: 2021年2月5日(星期五) 晚上10:30 收件人: "Chris2018998/BeeCP-Starter"<BeeCP-Starter@noreply.github.com>; 抄送: "凉白开"<1144388620@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [Chris2018998/BeeCP-Starter] 经常卡住 加载数据源的时候 (#10)