Could not open JPA EntityManager for transaction

1.错误信息

Could not open JPA EntityManager for transaction; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed

原因是mysql的设置的 interactive_timeout和wait_timeout 两个值为8小时(28800s),所以此问题多数出现在隔夜再访问报错的情景.

解决方案
配置数据库连接池

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.11</version>
</dependency>

配置application.yml

datasource:
  url: jdbc:mysql://${MYSQL_HOST:127.0.0.1}:${MYSQL_PORT:3306}/ag_admin_v2?useUnicode=true&characterEncoding=UTF8&useSSL=false
  username: root
  password: 321321
  driver-class-name: com.mysql.cj.jdbc.Driver
  # 连接池的配置信息
  # 初始化大小,最小,最大
  initialSize: 10
  minIdle: 10
  maxActive: 300
  # 配置获取连接等待超时的时间
  maxWait: 60000
  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  timeBetweenEvictionRunsMillis: 60000
  # 配置一个连接在池中最小生存的时间,单位是毫秒
  minEvictableIdleTimeMillis: 300000
  validationQuery: SELECT 1
  testWhileIdle: true
  testOnBorrow: false
  testOnReturn: false
  # 打开PSCache,并且指定每个连接上PSCache的大小
  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20
  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  filters: stat,wall,logging
  # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000

你可能感兴趣的:(mysql,数据库,jdbc,java)