【Druid】数据库连接超时
创始人
2025-01-15 12:35:04
0

现象:
接口请求,10秒钟超时,看了一下,是sql查询耗时太长了,数据还没来得及返回,连接就断开了。
连接断开,上层应用直接报错。。

这些问题,都是。。。知道答案后,就觉得,emmm,好简单。知道答案前,…tmd, 这啥玩意儿。。。。

直接说答案吧:
数据库连接超时,那就配置超时时长,准确地说是 druid 的超时时长。。。
要设置 druid 的 connectTimeout 、socketTimeout 这两个配置项,
嗯。单这样说,有点。。。

一份druid 配置

druid:   url: jdbc:mysql://10.12.1.9:3306/student?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true   username: username   password: passwordxxxx   useSSL: false   initial-size: 50   min-idle: 50   max-active: 200   connectTimeout: 600000  #1.2.12版本需要单独配置才生效   socketTimeout: 600000   #1.2.12版本需要单独配置才生效 

有了配置,不够,需要在代码里面解析配置
于是要写一个:
DruidProperties.java

import org.springframework.boot.context.properties.ConfigurationProperties;  /** * druid */ @ConfigurationProperties(prefix = "druid") public class DruidProperties {    private String url;    private String username;    private String password;     private int initialSize;    private int minIdle;    private int maxActive;    private long maxWait;     private String validationQuery;     private int connectTimeout;     private int socketTimeout;     private boolean testOnBorrow;     public String getUrl() {        return url;    }     public void setUrl(String url) {        this.url = url;    }     public String getUsername() {        return username;    }     public void setUsername(String username) {        this.username = username;    }     public String getPassword() {        return password;    }     public void setPassword(String password) {        this.password = password;    }     public int getInitialSize() {        return initialSize;    }     public void setInitialSize(int initialSize) {        this.initialSize = initialSize;    }     public int getMinIdle() {        return minIdle;    }     public void setMinIdle(int minIdle) {        this.minIdle = minIdle;    }     public int getMaxActive() {        return maxActive;    }     public void setMaxActive(int maxActive) {        this.maxActive = maxActive;    }     public long getMaxWait() {        return maxWait;    }     public void setMaxWait(long maxWait) {        this.maxWait = maxWait;    }     public String getValidationQuery() {        return validationQuery;    }     public void setValidationQuery(String validationQuery) {        this.validationQuery = validationQuery;    }      public int getConnectTimeout() {        return connectTimeout;    }     public void setConnectTimeout(int connectTimeout) {        this.connectTimeout = connectTimeout;    }     public int getSocketTimeout() {        return socketTimeout;    }     public void setSocketTimeout(int socketTimeout) {        this.socketTimeout = socketTimeout;    }     public boolean isTestOnBorrow() {        return testOnBorrow;    }     public void setTestOnBorrow(boolean testOnBorrow) {        this.testOnBorrow = testOnBorrow;    } } 

还有 DruidAutoConfiguration.java

import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet;   import javax.sql.DataSource; import java.sql.SQLException;   /**  * Created by chu on 2017/3/2.  */ @Configuration @EnableConfigurationProperties(DruidProperties.class) @ConditionalOnClass(DruidDataSource.class) @ConditionalOnProperty(prefix = "druid", name = "url") @AutoConfigureBefore(DataSourceAutoConfiguration.class) public class DruidAutoConfiguration {       @Autowired     private DruidProperties properties;     /**      * 获取数据源      * @return DataSource      */     @Bean     public DataSource dataSource() {         DruidDataSource dataSource = new DruidDataSource();         dataSource.setUrl(properties.getUrl());         dataSource.setUsername(properties.getUsername());         dataSource.setPassword(properties.getPassword());         if (properties.getInitialSize() > 0) {             dataSource.setInitialSize(properties.getInitialSize());         }         if (properties.getMaxActive() > 0) {             dataSource.setMaxActive(properties.getMaxActive());         }         if (properties.getMaxWait() > 0) {             dataSource.setMaxWait(properties.getMaxWait());         }         if (properties.getMinIdle() > 0) {             dataSource.setMinIdle(properties.getMinIdle());         }         if (properties.getValidationQuery() != null) {             dataSource.setValidationQuery(properties.getValidationQuery());         }         if (properties.getConnectTimeout() > 0) {             dataSource.setConnectTimeout(properties.getConnectTimeout());         }         if (properties.getSocketTimeout() > 0) {             dataSource.setSocketTimeout(properties.getSocketTimeout());         }         dataSource.setTestOnBorrow(properties.isTestOnBorrow());           try {             dataSource.init();         } catch (SQLException e) {             throw new RuntimeException(e);         }         return dataSource;     }       /**      * 获取ServletRegistrationBean      * @return ServletRegistrationBean      */     @Bean     public ServletRegistrationBean druidServlet() {         ServletRegistrationBean druidServletRegistration = new ServletRegistrationBean(new DruidStatViewServlet());         druidServletRegistration.addInitParameter("allow", "127.0.0.1");         druidServletRegistration.addUrlMappings("/druid/*");         return druidServletRegistration;     }   } 

一开始,配置文件写到了

spring:   datasource:     druid:       connectTimeout: 600000  #1.2.12版本需要单独配置才生效       socketTimeout: 600000   #1.2.12版本需要单独配置才生效 

然后配置死活不生效。。。

呃,,,, 所以配置文件,和解析类的前缀一定要匹配才行噢! 不熟悉的同学,其实很容易踩坑。。。。

我就踩了好多次。。。emmmm
踩坑记录:单独配置:socketTimeout: 600000不生效,需要两项同时配置:

      connectTimeout: 600000  #1.2.12版本需要单独配置才生效       socketTimeout: 600000   #1.2.12版本需要单独配置才生效 

相关内容

热门资讯

技术分享中至二人麻将有猫腻的!... 技术分享中至二人麻将有猫腻的!太坑了原来确实是有挂(2021已更新)(有挂普及);中至二人麻将有猫腻...
解密关于(Wepoke大厅房)... 解密关于(Wepoke大厅房)外挂辅助器神器(透视)详细教程(2021已更新)(哔哩哔哩)是一款可以...
【C++】用红黑树封装map、... 用红黑树封装map、set1. 红黑树1.1 模板参数的控制1.1.1 Value1.1.2 Key...
最新技巧爱来麻将到底有挂!太坑... 最新技巧爱来麻将到底有挂!太坑了原来确实是有挂(2022已更新)(有挂教程);相信小伙伴都知道这个爱...
动态内存管理<C语言> 导言        在C语言学习阶段,指针、结构体和动态内存管理,是后期...
AI辅助(gg扑克)软件透明挂... AI辅助(gg扑克)软件透明挂(辅助挂)透明辅助挂辅助器(2024已更新)(哔哩哔哩);德扑锦标赛是...
C++ AVL树 详细讲解 目录一、AVL树的概念二、AVL树的实现1.AVL树节点的定义2.AVL树的插入3.AVL树的旋转4...
发现一款(Wepoke挂)软件... 发现一款(Wepoke挂)软件透明挂(透视)外挂辅助器神器(2021已更新)(哔哩哔哩);超受欢迎的...
【LeetCode】2089.... 当有简单方法可以使用时,千万别饶了远路。1. 题目2. 分析3. 代码 class S...
指导大家中至九江跑得快辅助器!... 自定义新版中至九江跑得快辅助器系统规律,只需要输入自己想要的开挂功能,一键便可以生成出中至九江跑得快...