数据库连接池检查 - Springboot项目

有时会遇到一个springboot工程数据库连接断掉的情况。为避免该问题发生,我们可以加上数据库连接池检查。

解决方案

  • 加一个autoReconnection属性 ( 不推荐这种方法了 )

    spring.datasource.url = jdbc:mysql://localhost:3306/xxx?characterEncoding=UTF-8&autoReconnect=true
    
    The use of this feature is not recommended, because it has side effects related to session state and
    data consistency when applications don't handle SQLExceptions properly, and is only designed to be used
    when you are unable to configure your application to handle SQLExceptions resulting from dead and 
    stale connections properly.
    
  • 加上空闲检查配置

    spring.datasource.tomcat.test-while-idle=true
    spring.datasource.tomcat.test-on-borrow=true 
    spring.datasource.tomcat.validation-query=SELECT 1
    
  • 属性说明

connection.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容