c3p0 mysql 自动重连_C3P0官方对于MySQL8小时问题的解决方案

2023-10-26

前一段时间在做一个发邮件的程序,程序是用定时器,每晚凌晨定时发邮件,邮件内容需要从数据库中获取,运行了一天就出问题了。

问题信息如下

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was56588 milliseconds ago.

The last packet sent successfully to the server was 56588 milliseconds ago, which

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.

大致意思是说超时了,连接不可用,给的提示可以将

autoReconnect=true

来避免这一问题的发生

关于超时问题大致有如下几种解决方案

解决方案

将mysql服务器的wait_timeout设置为无穷大,保证永不超时(不知道可以不,不建议采用)像这样 set global wait_timeout=10;

根据提示自动重连将autoReconnect=true添加到数据库链接的代码中,像这样 jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false

在mysql的wait_timeout时间之内发一次心跳,保证连接的有效性

在使用的时候检测连接的有效性,失效就获取一个新的连接

第一种方案不建议采用,因为有其他更优雅的方式

第二种方案

MySQL官网对于autoReconnect的解释

autoReconnect

Should the driver try to re-establish stale and/or dead connections? If enabled the driver will throw an exception for a queries issued on a stale or dead connection, which belong to the current transaction, but will attempt reconnect before the next query issued on the connection in a new transaction. 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. Alternatively, as a last option, investigate setting the MySQL server variable “wait_timeout” to a high value, rather than the default of 8 hours.

Default: false

Since version: 1.1

官方不建议使用着一参数因为首先,过时的链接仍然会报错,其次,使用这一参数对session和数据的并发操作具有负面效果,官方说把wait_timeout设置的高一些都比使用这个参数好,还是不要用这种方案了吧,第一种都不采用何况这种呢?

本文介绍一下C3P0官方推荐的方式,这种方式综合了第三和第四种解决方案

C3P0官方的建议

Simple advice on Connection testing

If you don’t know what to do, try this:

If you know your driver supports the JDBC 4 Connection.isValid(…) method and you are using c3p0-0.9.5 or above, don’t set a preferredTestQuery. If your driver does not support this method (or if you are not sure), try SELECT 1 for your preferredTestQuery, if you are running MySQL or Postgres. For other databases, look for suggestions here. Leave automatedTestTable undefined.

Begin by setting testConnectionOnCheckout to true and get your application to run correctly and stably. If you are happy with your application’s performance, you can stop here! This is the simplest, most reliable form of Connection-testing, but it does have a client-visible performance cost.

If you’d like to improve performance by eliminating Connection testing from clients’ code path:

a. Set testConnectionOnCheckout to false

b. Set testConnectionOnCheckin to true

c. Set idleConnectionTestPeriod to 30, fire up you application and observe. This is a pretty robust setting, all Connections will tested on check-in and every 30 seconds thereafter while in the pool. Your application should experience broken or stale Connections only very rarely, and the pool should recover from a database shutdown and restart quickly. But there is some overhead associated with all that Connection testing.

d. If database restarts will be rare so quick recovery is not an issue, consider reducing the frequency of tests by idleConnectionTestPeriod to, say, 300, and see whether clients are troubled by stale or broken Connections. If not, stick with 300, or try an even bigger number. Consider setting testConnectionOnCheckin back to false to avoid unnecessary tests on checkin. Alternatively, if your application does encounter bad Connections, consider reducing idleConnectionTestPeriod and set testConnectionOnCheckin back to true. There are no correct or incorrect values for these parameters: you are trading off overhead for reliability in deciding how frequently to test. The exact numbers are not so critical. It’s usually easy to find configurations that perform well. It’s rarely worth spending time in pursuit of “optimal” values here.

So, when should you stick with simple and reliable (Step 2 above), and when is it worth going for better performance (Step 3)? In general, it depends on how much work clients typically do with Connections once they check them out. If clients usually make complex queries and/or perform multiple operations, adding the extra cost of one fast test per checkout will not much affect performance. But if your application typically checks out a Connection and performs one simple query with it, throwing in an additional test can really slow things down.

That’s nice in theory, but often people don’t really have a good sense of how much work clients perform on average. The best thing to do is usually to try Step 3, see if it helps (however you measure performance), see if it hurts (is your application troubled by broken Connections? does it recover from database restarts well enough?), and then decide. You can always go back to simple, slow, and robust. Just set testConnectionOnCheckout to true, testConnectionOnCheckin to false, and set idleConnectionTestPeriod to 0.

But do, always, be sure that your tests themselves are performant, either because your JDBC driver supports Connection.isValid(…) or because you have set an efficient preferredTestQuery !!!

实在不想贴这么多,只是为了给出C3P0的本意

官方建议采用第三种方式

将testConnectionOnCheckout 设为 false

将testConnectionOnCheckin 设为 true

将idleConnectionTestPeriod 设为 30,这个数字要根据项目情况设定,比8小时小就好

解释一下这几个参数

testConnectionOnCheckout

If true, an operation will be performed at every connection checkout to verify that the connection is valid.

设置为true,所有的连接都将检测其有效性,会影响性能,所以将其设置为false

testConnectionOnCheckin

If true, an operation will be performed asynchronously at every connection checkin to verify that the connection is valid.

设置为true,异步检测连接的有效性

idleConnectionTestPeriod(单位是秒,不是毫秒)

If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.

每隔多少秒c3p0检测连接的有效性

示例

#---------------------------------------------------------

# c3p0反空闲设置,防止8小时失效问题28800

#---------------------------------------------------------

#idleConnectionTestPeriod要小于MySQL的wait_timeout

jdbc.c3p0.testConnectionOnCheckout=false

jdbc.c3p0.testConnectionOnCheckin=true

jdbc.c3p0.idleConnectionTestPeriod=3600

#---------------------------------------------------------

# c3p0连接池配置

#---------------------------------------------------------

#initialPoolSize, minPoolSize, maxPoolSize define the number of Connections that will be pooled.

#Please ensure that minPoolSize <= maxPoolSize. #Unreasonable values of initialPoolSize will be ignored, and minPoolSize will be used instead. jdbc.c3p0.initialPoolSize=10 jdbc.c3p0.minPoolSize=10 jdbc.c3p0.maxPoolSize=100 #maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool. jdbc.c3p0.maxIdleTime=3600

spring中dataSource的配置

c3p0的简单配置就介绍这么多。

参考文献

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c3p0 mysql 自动重连_C3P0官方对于MySQL8小时问题的解决方案 的相关文章

  • frame:通过鼠标拉动两个子页面的宽度

    除了本文 还可以参考https www cnblogs com LT0314 p 3805393 html1 只需要简单的frame标签即可实现鼠标拉动调整大小的效果 2 我在点击frameA中的链接 新页面在frameB中显示 你可以在页
  • python 读取txt文件

    在 Python 中读取 txt 文件可以使用内置的 open 函数 例如 假设你有一个文件叫做 test txt 你可以这样读取它 打开文件 f open test txt r 读取文件内容 content f read 关闭文件 f c
  • required a bean of type 'xxx' that could not be found.

    文章目录 解决办法 required 属性 解决办法 新增 application properties 配置 或者 application yml 配置 注意检查下配置完成后 是否有警告 application properties ma
  • baidu地图API叠加自定义图层(一)

    百度地图API提供了叠加自定义图层的方法 地址如下 官网例子 清华校园微观图地图 http developer baidu com map jsdemo htm g0 2 API说明 http developer baidu com map
  • 学习笔记python+opencv利用拉普拉斯算子锐化与sobel算子锐化

    应数字图像处理实验要求对图像进行锐化处理 使用opencv中的函数进行锐化操作 拉普拉斯算子运算后彩色图像效果比较明显 sobel算子与原图像堆叠之后在彩色图像锐化相当明显 但是与原图像色彩区别过大 在原图转换为灰度图像之后细节较多 导入库
  • Go使用gos7实现西门子PLC通讯

    Go简介 以下摘自百度百科 Go 又称 Golang 是 Google 的 Robert Griesemer Rob Pike 及 Ken Thompson 开发的一种静态强类型 编译型语言 Go 语言语法与 C 相近 但功能上有 内存安全
  • routeros 配置一个DMZ站点

    公司最近要上套业务系统 需要一个外网ip给它 于是就要在路由上给它个映射 我在网上看到一个资料很适合 RouterOS 网络中配置一台DMZ站点 下面将说明怎么样在网络中配置一台DMZ站点 DMZ是英文 demilitarized zone
  • 计算机网络输入一个URL全过程

    1 输入url时候 当我们开始在浏览器中输入网址的时候 浏览器他会从历史记录 书签等地方 找到已经输入的字符串可能对应的 url 然后给出智能提示 让你可以补全url地址 2 请求发起之后 解析这个域名 解析域名分为下面几个步骤 1 首先
  • CSS磨砂玻璃穿透效果 filter: blur(4px);

    文章目录 一 参考 二 问题描述 三 原理说明 四 项目代码说明 一 参考 纯CSS教你实现磨砂玻璃背景效果 附代码 二 问题描述 工作中 UCD 设计了一个 磨砂穿透 的效果 本想着画一个磨砂效果 然后在通过透明度穿透 实现 磨砂穿透 的
  • Xshell 常用命令大全 自用

    1 命令ls 列出文件 ls la 给出当前目录下所有文件的一个长列表 包括以句点开头的 隐藏 文件 ls a 列出当前目录下以字母a开头的所有文件 ls l doc 给出当前目录下以 doc结尾的所有文件 2 命令cp 复制文件 cp a
  • 特征选择(Feature Selection)

    主要内容 为什么要进行特征选择 什么是特征选择 怎么进行特征选择 特征选择 在现实生活中 一个对象往往具有很多属性 以下称为特征 这些特征大致可以被分成三种主要的类型 相关特征 对于学习任务 例如分类问题 有帮助 可以提升学习算法的效果 无
  • 【Spring源码】createBean()

    目录 1 resolveBeanClass 2 prepareMethodOverrides 3 resolveBeforeInstantiation 1 applyBeanPostProcessorsBeforeInstantiation
  • whisper模型 环境搭建与使用

    1 创建conda环境 conda create n whisper python 3 9 激活环境 conda activate whisper 2 安装whisper pip install openai whisper conda i
  • 计算机系统基础课程实验课bomb--phase_3

    首先栈指针减去24 应该是为了存储数组所需要 rcx存储栈指针加12的地址 rdx存储栈指针加8的地址分别作为第四第三参数 并且把0放入了返回值 eax中 然后可以看到输入函数的第二个参数地址为0x4025cf 通过x s查看内存的值为 即
  • java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes [Generic

    该错误表示有重复的spring boot启动类 去掉重复的就行 项目是service工程里的测试部分加了一个springboot启动类 用于测试 但是该工程模块依赖于其他模块 如model模块和base模块的pojo类 因此注释掉base和
  • 搭建Vulhub漏洞测试靶场+成功环境

    可点击目录分类快捷浏览 官方地址 环境安装成功后做好快照 环境是否正常运行 检查docker是否运行 没运行则运行 进入对应漏洞文件夹 搭建及运行漏洞环境 官方地址 https vulhub org docs 环境安装成功后做好快照 我个人
  • 怎么可以有颜色的将matlab的代码复制到word中不产生乱码。

    我们直接将matlab中的代码复制粘贴到word中 就会产生如图的乱码 如果选择选择只保留文本 也能解决乱码问题 但是会将matlab原带的代码颜色消失 显得不美观 其实只要改正一下字体就可以解决这种问题 在matlab中设置一下字体就可以
  • Tomcat控制台中文乱码问题

    解决方案 第一 只修改java util logging ConsoleHandler encoding UTF 8下的编码格式 修改成GBK 不成功看第二 我用第一个配置就成功了 第二 在Tomcat根目录下 conf 子目录下的 log

随机推荐

  • Daily Scrum: 2012/11/7

    成员 角色 今天工作 明天计划 王安然 PM Dev 进行Craft类的供给Craft子类的编写 186 继续进行Craft CraftFactory类的编写 186 黄杨 PM Dev Art 完成粒子爆炸效果测试 199 基本样式的星空
  • 前端JavaScript面试技巧

    前端JavaScript面试技巧 第一章 课程简介 1 1课程简介 前端基础 1 2前言 网站前端程序开发分析 1 3几个面试题 typeof操作符 require命令加载ES6模块 SpringLoaded 1 4如何搞定所有面试题 获取
  • 【力扣练习题】加一

    package sim import java math BigDecimal import java util Arrays public class Add1 给定一个由 整数 组成的 非空 数组所表示的非负整数 在该数的基础上加一 最
  • eclipse环境搭建C++环境

    eclipse搭建C 编译环境使用eclipse CDT msys gcc gdb共4个软件 其中几个软件简单理解为 eclipse CDT 用于编辑软件文本 msys 管理使用的软件下载 gcc 用于编译 链接文件 gdb 用于调试 一
  • graphpad7.04多组比较p值_同是折线图为何你却这么优秀,这才是多组数据作图应该有的样子...

    相信大家对Excel做折线图应该不陌生 在展示数据的时候 图表是一种最好的展示方法 但是经常会碰到一种尴尬的事情就是 当数据维多比较多的时候 做出的图表就会显得非常难看 今天我们就来学习一下 多组数据怎么做折线图才好看 平民手中的折线图 当
  • 读书笔记-oo项目生存法则

    1 建立一个成功的项目的简单四步 1 采用增量式进度安排和阶段划分 2 拥有发现和改正错误的机制 3 建立一个良好的产品发布习惯 4 拥有优秀的项目负责人 项目经理和技术主管 2 相关概念 1 类是一组子程序和相关数据的集合 常用类图表示
  • springboot+flowable+mybatisplus初始化建表时如何指定数据源

    springboot flowable mybatisplus初始化表单如何指定数据源 问题描述 解决过程 直接上代码 问题描述 之前在自己的springboot当中集成了flowable 在集成flowable之前 springboot当
  • 直方图均衡化

    https www zhihu com question 37204742 answer 221844779 https zhuanlan zhihu com p 32857009
  • 明文传输漏洞

    业务系统对用户口令等机密信息的保护不足 攻击者可以利用攻击工具 从网络上窃取合法用户的口令数据 从而登录系统执行非法操作 攻击者可以利用监听工具在网络中窃取合法用户的口令数据 从而非法获取系统的访问权限 检测方法 通过burpsuite工具
  • OD-数列还原(python)

    数列还原 题目描述 有一个数列A n 从A 0 开始每一项都是一个数字 数列中A n 1 都是A n 的描述 其中A 0 1 规则如下A 0 1A 1 11 含义其中A 0 1是1个1 即11 表示A 0 从左到右连续出现了1次1A 2 2
  • C语言/C++基础之跨年烟花程序代码(附源码)

    C语言 C 基础之跨年烟花程序代码 程序之美 前言 主体 运行效果 代码实例 结束语 程序之美 前言 元旦将至 新年将至 转眼间2022年即将过去 崭新的一年正向我们缓缓走来 风花雪夜新年临近 入冬寒意随风吹进 繁星点点缀满天际 黎明晨阳元
  • 修改jar包中的class文件

    需求及准备 需求 现在有一个 jar文件 要修改其中某个文件的代码 准备 确保JRE已安装且环境变量已配置 安装Java Decompiler 官方地址为 http java decompiler github io 选择其中的JD GUI
  • Spring循环依赖源码debug详解

    1 什么是循环依赖 在Spring里 指两个或多个bean互相依赖 比如有两个Bean A B A中注入B B中注入A 这样就形成了循环依赖 Spring默认是支持循环依赖的 本文我们就从Spring源码层面对循环依赖进行分析 2 环境构建
  • Node.js学习四(文件流stream)

    文章目录 前言 一 Node处理缓存的方式 二 什么是Node js Stream 流 三 stream 流 的类型 四 创建可读流 五 拷贝文件 六 链式流 1 压缩文件 2 解压文件 前言 在我们学过fs模块后 可以知道读取文件时采用r
  • 12 shell命令之打包

    昨晚写的awk 说实话 对我而言 那是一个最复杂的命令 写得不是很好 可能在结构组织上面有很大的问题 后续有心得会再调整修改 本文将介绍linux的一组打包命令 这其中有我们最常用的tar 也有我们几乎没有见过的mksquansh 接下来就
  • 简明SQL截断和偏移指南:掌握LIMIT实现数据筛选

    以下是用到的表 截断 LIMIT 用于限制查询结果返回的行数 即最多返回多少行数据 例如 返回前两行数据 例如 从第二个数据开始返回两条数据 从0开始计算 偏移 OFFSET 用于指定查询结果的起始位置 即从结果集中的第几行开始返回数据 例
  • Spring使用——通过配置类注入Bean

    配置类 Configuration 告诉spring这是一个配置类 ComponentScan value指定要扫描的包 Filter excludeFilters default 扫描的时候按照规则排除哪些 ComponentScan v
  • App自动化测试 —— Appium的使用

    目录 简介 安装 配置 Run 问题 解决方案 优点 缺点 总结 简介 Appium是一个开源测试自动化框架 用于原生 混合和移动 Web 应用程序 安装 Appium安装方式有两种 一种是通过npm命令行安装 另一种是通过安装可视化工具
  • 华为OD机试真题 Java 实现【查找单入口空闲区域】【2022 Q4 100分】,附详细解题思路

    目录 一 题目描述 二 输入描述 三 输出描述 四 解题思路 五 Java算法源码 六 效果展示 1 输入 2 输出 3 说明 一 题目描述 给定一个 m x n 的矩阵 由若干字符 X 和 O 构成 X 表示该处已被占据 O 表示该处空闲
  • c3p0 mysql 自动重连_C3P0官方对于MySQL8小时问题的解决方案

    前一段时间在做一个发邮件的程序 程序是用定时器 每晚凌晨定时发邮件 邮件内容需要从数据库中获取 运行了一天就出问题了 问题信息如下 com mysql jdbc exceptions jdbc4 CommunicationsExceptio