DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果
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踏坑过程(四)没有使用连接池的后果的更多相关文章
- DB数据源之SpringBoot+Mybatis踏坑过程实录系列(一)
DB数据源之SpringBoot+MyBatis踏坑过程(一) liuyuhang原创,未经允许进制转载 系列目录 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之Sp ...
- DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载 吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之Spr ...
- DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池
DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池
DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量
DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...
- Springboot & Mybatis 构建restful 服务四
Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...
- shardingsphere多数据源(springboot + mybatis+shardingsphere+druid)
org.springframeword.boot:spring-boot-starer-web: 2.0.4release io.shardingsphere:sharding-jdbc-spring ...
- springboot 学习之路 6(集成durid连接池)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
随机推荐
- Hadoop生态体系
Hadoop路线 HDFS 分布式文件系统 MapReduce 分布式编程框架 ZooKeeper 分布式协调框架 Hive 非关系型数据仓库 Flume 日志采集框架
- iframe 子页面改变父页面样式
iframe 子窗口调节父窗口样式: $(window.parent.document).find("body").attr("style"," 父元 ...
- React Native之React速学教程(上)
概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解 ...
- 花1台的钱入手2台【最能抗DDoS】阿里云主机【攻略】
花1台的钱入手2台[最能抗DDoS]阿里云主机[攻略]: 第一步:先申请0元半年 http://click.aliyun.com/m/335/:注:0元机器只有新帐号可申请第二步:再买6折37/月 h ...
- html5之hash
http://blog.csdn.net/u012028371/article/details/67636395 原文:https://www.studyscript.com/Post/index/i ...
- CentOS 附加软件包
本人初学 CentOS,安装软件与 windows 下区别很大,大部分得通过 yum install xxx .这有个问题,一方面 yum 资源有限,另一方面 yum 默认装的版本较低.比如 Cent ...
- Sql Server 如何去掉内容里面的Html标签
DECLARE @str NVARCHAR(MAX)= ' <!DOCTYPE html> <html> <head> </head> <body ...
- SQL Server ->> 内置标量函数TRY_PARSE、TRY_CAST和TRY_CONVERT的各自特点和区别
SQL Server到了目前的2014版本有三个函数是用来转换数据格式的.虽说之前版本中已经有CAST和CONVERT这两个函数来干这个事情.问题是,一旦往目标数据类型转换失败就会造成报错. TRY_ ...
- mongodb 3.4分片复制集配置
1:启动三个实例 mongod -f /home/mongodb/db27017/mongodb27017.conf mongod -f /home/mongodb/db27018/mongodb27 ...
- javaEE版本的eclipse中导入工程,发现server里面找不到工程,根本发布不了也不能运行
原文:http://www.cnblogs.com/sxmcACM/p/3674545.html 1.具体解决方法 首先确保,你导入的工程所用的JDK版本和你的机器上安装的版本是同一版本, 如果不同做 ...