Druid报错:The last packet successfully received from the server was 377,359 milliseconds ago.

遇到的问题:

        每次查询数据库,druid都会报错,数据还能查出来,但写入会失败

报的错:

com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 30,053 milliseconds ago. The last packet sent successfully to the server was 30,090 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.

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 30,053 milliseconds ago. The last packet sent successfully to the server was 30,090 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. 

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

解决方案:

1.数据库配置连接存活时间太短(一般不会是这个的错)

        查看连接状态:

SHOW VARIABLES LIKE 'wait_timeout';

 

         修改默认连接存活时间:

  1. 打开MySQL配置文件 my.cnf(在Windows上可能是 my.ini)。通常,这个文件位于 MySQL 安装目录的 /etc/ 或 /etc/mysql/ 目录下。
  2. 在配置文件中找到 [mysqld] 部分。
  3. 在 [mysqld] 部分中添加或修改以下行(如果不存在):

wait_timeout = 600

  • 这里的值 600 表示连接的超时时间为 600 秒,您可以根据需要进行调整。

         保存并关闭配置文件,重启 MySQL 服务,以使更改生效。

2.druid配置连接存活时间太长

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    dynamic:
      strict: true
      primary: main
      datasource:
        main:
          url: 
          username: 
          password: 
      druid:
      # 初始化大小,最小,最大
        initial-size: 5
        min-idle: 5
        max-active: 20
        # 配置获取连接等待超时的时间
        max-wait: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        time-between-eviction-runs-millis: 6000
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        min-evictable-idle-time-millis: 200000
        max-evictable-idle-time-millis: 240000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-return: true
        # 打开PSCache,并且指定每个连接上PSCache的大小
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,wall用于防火墙
        filters: stat,wall,slf4j
  • min-evictable-idle-time-millis: 连接最小存活时间
  • max-evictable-idle-time-millis: 连接最大存活时间

3.数据库版本问题(我是这个问题)

        数据库5.6版本的,有可能与 springboot3 + druid3 兼容有问题,我换了5.7或5.8的数据库都不会报这个错了

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Jinja2 模板标签有以下几种: 1. 变量: {{ variable }} 2. 控制结构: {% if variable %} ... {% endif %} 3. 循环: {% for item in items %} ... {% endfor %} 4. 宏: {% macro macro_name(args) %} ... {% endmacro %} 5. 引用: {% include 'template.html' %} 6. 块: {% block block_name %} ... {% endblock %} 7. 继承: {% extends 'base_template.html' %} 这些标签可以结合使用来生成动态的 HTML 页面。 ### 回答2: Jinja2是一个流行的Python模板引擎,用于在web应用程序中生成动态内容。它使用一些特殊的标签和语法来插入、循环和条件渲染数据。 下面是一些常用的Jinja2模板标签: 1. {% ... %}:这是Jinja2模板中的常规代码块标签。您可以在其中放置Python代码和表达式,并在渲染时执行。 2. {{ ... }}:这是用于在模板中插入变量和表达式的输出的插值标签。在这里,您可以将变量包装在双花括号中,并在渲染时使用实际的数据进行替换。 3. {% if ... %} ... {% endif %}:这是用于在模板中执行条件语句的标签。您可以在{% if %}标签中使用表达式来检查某些条件,并在满足条件时渲染特定的内容。 4. {% for ... in ... %} ... {% endfor %}:这是用于在模板中执行循环的标签。使用{% for %}标签,您可以遍历一个可迭代对象,并在每次迭代时渲染相应的内容。 5. {% else %}:这是与{% if %}和{% for %}标签一起使用的可选块,用于指定在条件不满足时或循环结束时的备选内容。 6. {% extends ... %}:这是用于创建模板继承的标签。您可以将一个模板扩展为另一个模板,并在子模板中重写和扩展父模板的内容。 7. {% include ... %}:这是用于在模板中包含其他模板的标签。您可以使用{% include %}标签将其他模板插入到主模板中,以便在渲染时显示。 这只是Jinja2模板标签的一小部分,但它们是常见的、常用的标签。根据具体的需求,您可能还会发现其他标签和功能,以满足更复杂的模板渲染需求。 ### 回答3: jinja2是一种流行的Python模板引擎,用于在HTML文件中嵌入逻辑代码和动态数据。其中,lask是基于jinja2模板引擎开发的Python轻量级Web框架。jinja2模板标签是用于在模板文件中进行逻辑控制和数据渲染的特殊标记。 常用的jinja2模板标签有以下几种: 1. `{% if 条件 %} ... {% endif %}`:用于条件判断,根据条件的真假决定是否渲染其中的内容。 2. `{% for 变量 in 列表 %} ... {% endfor %}`:用于循环迭代,将列表中的每个元素渲染到循环中的内容中。 3. `{% block 名称 %} ... {% endblock %}`:用于定义模板的块,可以在子模板中重写该块的内容。 4. `{% include '文件路径' %}`:用于引用其他模板文件,将指定的模板内容渲染到当前位置。 5. `{% set 变量 = 值 %}`:用于定义变量,可以在模板中使用该变量进行数据渲染。 6. `{% macro 名称(参数) %} ... {% endmacro %}`:用于定义可重用的宏,在模板中可以直接调用该宏进行代码复用。 除了上述标签之外,jinja2还提供了许多其他的标签和过滤器,如过滤器链、内置函数、空值判断等,可以根据具体需求进行灵活应用。在开发中,合理使用这些标签可以提高模板的灵活性和可重用性,使代码更加简洁和易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值