DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果

liuyuhang原创,未经允许禁止转载 

系列目录连接

DB数据源之SpringBoot+Mybatis踏坑过程实录(一)

1.环境说明

  1.1.使用springboot手动获取数据源,其中数据源DataSource使用如下代码获取:

 DataSourceBuilder create = DataSourceBuilder.create();
... DataSource source = create.build();

  1.2.只使用了这种方式来创建数据源,并且没有配置数据源连接池

  1.3.在springboot1.0中没有配置tomcat数据源连接池

  1.4.在springboot2.0中没有配置tomcat数据源连接池,也没有配置HikariCP连接池

2.报错表现

  2.1.部分报错内容如下:

    Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    The last packet successfully received from the server was 14,595,596 milliseconds ago.  The last packet sent successfully to the server was 14,595,612 milliseconds ago.

  2.2.或部分报错内容如下:

    o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

    Data source rejected establishment of connection,  message from server: "Too many connections"

  2.3.在报错内容中,有关于连接池的类,springboot1.0中会出现tomcat的连接池,springboot2.0中会出现Hikari的连接池这两种错误。

  2.4.数据库中现存的连接数量会持续上涨,一直到上涨阈值。查看方法如下:

    cmd-->连接mysql数据库-->输入show status like 'Threads%',结果可能如下:

    

    其中Threads_connected的数量就是上文提到的现存的连接数量,如果配置了连接池,在不断对数据库发送请求的时候,

    该数量虽然会上涨,但是停下来会有消退,同时上涨速度不会那么快的。

    可以持续刷新服务发送请求,然后不断使用此命令查看链接变化,达到某个数字以后,如果报错了,那么查看mysql设置。

    

    该设置应该在mysql根目录中,或者是mysql的data所在目录下,名为my.ini的配置文件,其中有配置最大链接数量,

    可进行调整。

3.原因分析

    有两种写法实际上中间是有问题的,写法和问题原因如下:

 //使用DataSourceBuilder获得数据源额创建者过程中,DataSourceBuilder是有泛型指定的,该泛型没有指定
//create只可以对url,driverClassName,username,password进行设置,并没有相关连接池配置
DataSourceBuilder<?> create = DataSourceBuilder.create();
DataSource source1 = create.build();
//使用DataSourceBuilder获得数据源创建的过程中,使用额是链式编程
DataSource source2 = DataSourceBuilder.create().build(); //DataSource是一个接口,并非实际的类,因此能set的设置项非常少
public interface DataSource extends CommonDataSource, Wrapper

    

    由于上述代码只定义了链接属性,并没有定义连接池,而mysql数据库可以维持的连接数量有限,所以导致本文所指错误。

4.解决方案

    对于springboot2.0以下的版本,可考虑额外配置一个tomcat连接池

    对于springboot2.0以上的版本,可考虑额外配置一个tomcat连接池之外,还可以配置使用HikariCP连接池

    具体手动配置方案,下次在更!

以上

DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果的更多相关文章

  1. DB数据源之SpringBoot+Mybatis踏坑过程实录系列(一)

    DB数据源之SpringBoot+MyBatis踏坑过程(一) liuyuhang原创,未经允许进制转载 系列目录 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之Sp ...

  2. DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载  吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...

  3. DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载    系列目录连接 DB数据源之Spr ...

  4. DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池

    DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池 liuyuhang原创,未经允许禁止转载  系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...

  5. DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池

    DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池 liuyuhang原创,未经允许禁止转载  系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...

  6. DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量

    DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...

  7. Springboot & Mybatis 构建restful 服务四

    Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...

  8. shardingsphere多数据源(springboot + mybatis+shardingsphere+druid)

    org.springframeword.boot:spring-boot-starer-web: 2.0.4release io.shardingsphere:sharding-jdbc-spring ...

  9. springboot 学习之路 6(集成durid连接池)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

随机推荐

  1. MongoDB 创建集合

    createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 createCollection() ...

  2. C++基础--回调的应用

    一.类成员函数的回调 1. 类成员函数的回调,函数的调用必须通过类来调用: CallBack.h #pragma once class CallBack { public: CallBack(); ~ ...

  3. MARS3.6 Programming

    An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains ...

  4. ArcGIS 编程中对接口的理解

    学习AO,最重要的是理解“接口”这个概念.接口是什么?有什么具体作用?在多种计算机高级语言中,都可以看到“接口”这个术语,但基本上每一本书对“为什么使用接口”等重要文都都“语焉不详”,使得初学者往往不 ...

  5. Python爬虫教程-32-Scrapy 爬虫框架项目 Settings.py 介绍

    本篇介绍项目开发的过程中,对 Setting 文件的配置和使用 Python爬虫教程-32-Scrapy 爬虫框架项目 Settings.py 介绍 settings.py 文件的使用 想要详细查看 ...

  6. addEventListener(event, function, useCapture) 简记

    监听事件方法:addEventListener(<event-name>, <callback>, <use-capture>) 移除监听事件方法:removeEv ...

  7. arm汇编学习(六)---跳转到thumb状态

    通常函数返回使用 pop {r7,pc}或bx lr等方式(bx,b类似jmp为跳转指令,但bx可以指定跳转区域究竟为thumb还是arm指令.thumb指令指令的时候,直接填写该地址却总是产生SIG ...

  8. Atlas+Keepalived实现MySQL读写分离、读负载均衡

    一.基础介绍 ========================================================================================== 1. ...

  9. 【Leetcode】【Medium】4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  10. RabbitMq ack 与 nack

    1.ack 移除 2.nack 移除再次发送队列