1.mybatis提供的数据源类型

以下是官网介绍

UNPOOLED    不使用连接池的数据源

This implementation of DataSource simply opens and closes a connection each time it is requested. While it’s a bit slower, this is a good choice for simple applications that do not require the performance of immediately available connections. Different databases are also different in this performance area, so for some it may be less important to pool and this configuration will be ideal. The UNPOOLED DataSource is configured with only five properties:

POOLED      使用连接池的数据源

This implementation of DataSource pools JDBC Connection objects to avoid the initial connection and authentication time required to create a new Connection instance. This is a popular approach for concurrent web applications to achieve the fastest response

JNDI            使用JNDI实现的数据

This implementation of DataSource is intended for use with containers such as EJB or Application Servers that may configure the DataSource centrally or externally and place a reference to it in a JNDI context.

结构图:

2.场景:使用mybatis操作数据库

问题:长时间没有操作数据出现失去mysql的连接错误

配置:

  <environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="${mysql.driver}"/><property name="url" value="${recommend.url}"/><property name="username" value="${recommend.user}"/><property name="password" value="${recommend.pwd}"/><property name="poolPingEnabled" value="true"/><property name="poolPingQuery" value="select 1"/><property name="poolPingConnectionsNotUsedFor" value="3600000"/></dataSource></environment></environments>

连接:

recommend.url=jdbc:mysql://ip:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false

这里使用mybatis的POOLED带数据池的数据源,同是也按照官网开启ping操作以及配置autoReconnect,但会出现以下错误:

2018-06-26 08:30:59.746  WARN 9296 --- [http-nio-7802-exec-37] o.a.i.d.pooled.PooledDataSource          : Execution of ping query 'select 1' failed: The last packet successfully received from the server was 50,897,120 milliseconds ago.  The last packet sent successfully to the server was 50,897,120 milliseconds ago. is longer than the server configured value of 'wait_timeout'.
 You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

启动mybatisping操作与设置autoReconnect不适用于mysql5.x版本,只试用与mysql4.0的版本,

配置中的poolPinQuery语句,一般我们用Select now | | select 1。
但是这种策略会严重降低sql的查询效率,如果开启此功能,每一次Query相当于两次Query。
而其他的数据库连接池,则是有单独的线程去check,像C3P0。所以在线程池这里,Mybatis原生做的并不是很好。

解决方法:

使用c3p0或者druid的连接池插件

以下是使用c3p0的配置:

首先定义c3p0连接连接池

package base.datasource;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;/*** Author: hezishan* Date: 2018/6/26.* Description: c3p0连接池**/
public class C3P0DataSourceFactory extends UnpooledDataSourceFactory {public C3P0DataSourceFactory() {this.dataSource = new ComboPooledDataSource();}
}

修改xml配置:

<configuration><properties resource="db.properties"></properties><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="base.datasource.C3P0DataSourceFactory"><property name="driverClass" value="${mysql.driver}"/><property name="jdbcUrl" value="${recommend.url}"/><property name="user" value="${recommend.user}"/><property name="password" value="${recommend.pwd}"/><property name="idleConnectionTestPeriod" value="60" /><property name="maxPoolSize" value="20" /><property name="maxIdleTime" value="600" /><property name="preferredTestQuery" value="SELECT 1" /></dataSource></environment></environments><!--由于mybatis不支持通配符使用,估这里需要列出每一个mapper--><mappers></mappers>
</configuration>

mybatis连接mysql断连接问题记录相关推荐

  1. 连接mysql数据库小知识记录

    配置 application.properties 由于我使用的是比较新的Mysql连接驱动,所以配置文件可能和之前有一点不同. server.port=8333 spring.datasource. ...

  2. python3连接mysql,python3连接MySQL数据库实例详解

    本文实例为大家分享了python3连接MySQL数据库的具体代码,供大家参考,具体内容如下 #python3连接MySQL实例 import pymysql """导入连 ...

  3. c 连接mysql.mwb_CodeSmith连接mysql提示“找不到请求的 .Net Framework Data Provider”的解决方法...

    下载了codesmith 8,连接Mysql却提示"找不到请求的 .Net Framework Data Provider". 1,下载MySql.Data.dll:https:/ ...

  4. vbS访问mysql语句_关于VBS连接MySQL与连接Excel

    目标:连接MySQL查找数据放入新建Excel表 实现过程: 1.连接数据库 首先要去MySQL官网下载个ODBC数据源. 安装之后,即可在控制面板-管理工具-ODBC数据源-添加,中看到MySQL驱 ...

  5. mysql 遗失对主机的连接,MySQL远程连接丢失问题解决方法(Lost connection to MySQL server)...

    MySQL远程连接丢失问题解决方法(Lost connection to MySQL server) 最近服务器很不稳定,于是重装了mysql 和php 服务,但是接着却遇到了很头疼的麻烦. 远程连接 ...

  6. java连接mysql(java连接mysql的jar包)

    mysql与apache怎么连接呀??? 设置 my.ini 就 OK 了 在windows文件夹下. java与mysql做的毕业设计 你说的我不是很明白哦.不过我想你的意思是不是说:"通 ...

  7. mysql怎么连接,mysql怎么连接

    使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 例如 以下是从命令行中连接mysql服务器的简单实例:[root@host]# mys ...

  8. sqlserver怎么连接mysql,sqlserver连接mysql

    1.需要远程连接MYSQL的服务器上安装mysql-connector-odbc-5.1.8-winx64 (根据自己OS和mysql版本下载相应版本) 2.就可以通过sqlserver查询分析器执行 ...

  9. php连接mysql 1045_连接mysql报1045错误怎么办

    连接mysql报1045错误的解决方法:1.打开my.ini配置文件:2.添加[skip-grant-tables],保存文件:3.重启mysql服务:4.登录mysql并修改密码:5.退出登录并重新 ...

最新文章

  1. while true 死循环判断端口按顺序启动应用
  2. 40亿移动设备的用户画像和标签架构实践
  3. SQL Server 分区表的创建方法与管理
  4. DELL XPS 9350 无线网卡DW1820A WIN7驱动异常解决
  5. python处理时间和日期_python时间和日期的处理
  6. raid ahci模式哪个好_比群晖好用?威联通TR-004磁盘阵列外接盒使用详解
  7. 解决python报错写入文件 io.UnsupportedOperation: not writable
  8. rocketmq linux环境下载地址,Linux下RocketMQ下载安装教程
  9. 表主键自增长Mybatis插入数据报错
  10. 金数据表单接口请求(php)
  11. Swift-函数学习
  12. 互联网硅谷投资教父 保罗·格雷厄姆 Paul Graham
  13. 智慧电力信息化系统建设整体方案
  14. 如何轻松搞定内网摄像头远程运维?EasyNTS上云网关简单三步实现设备公网远程控制、远程配置
  15. dxf制作kml_kml到dxf
  16. 使用华为云云耀服务器安装宝塔面板
  17. 最大公约最小公倍数算法
  18. android请求https(一)生成BKS
  19. Python下数值型与字符型类别变量独热编码(One-hot Encoding)实现
  20. [转载]C++经典书籍汇总

热门文章

  1. Scrapy爬取伯乐在线的所有文章
  2. 手机可以找出 暗藏的摄像头
  3. Redis篇之解决redis从节点无法写入数据问题
  4. python爬取文字和图片_Python学习第七天之爬虫的学习与使用(爬取文字、图片、 视频)...
  5. 剑指Offer——如何做好自我介绍
  6. MySQL8从入门到精通学习笔记
  7. video analysis -- 弱监督的动作检测-UntrimmedNets
  8. wr742n虚拟服务器,TP-Link TL-WR742N路由器作为无线交换机的设置方法
  9. Jackson 序列化 自定义注解处理Null 值
  10. WinCE WebCamera驱动开发白皮书