application.yml或application.properties数据库配置文件中配置问题,解决方法参考下面例子:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
jpa:
properties:
hibernate:
show_sql: true
format_sql: true
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
其中validationQuery: select 1和physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl是重点。
另外:配置validationQuery的时候需注意,数据库不同配置的validationQuery值也不同。
validationQuery是用来验证数据库连接的查询语句,这个查询语句必须是至少返回一条结果的SELECT语句。每种数据库都有各自的验证语句,下表中从网上收集了几种常见数据库的validationQuery。
数据库 | validationQuery |
---|---|
Oracle | select 1 from dual |
MySQL | select 1 |
Microsoft SQL Server | select 1 |
DB2 | select 1 from sysibm.sysdummy1 |
SQLite | select 1 |
HSQLDB | select 1 from INFORMATION_SCHEMA.SYSTEM_USERS |
postgresql | select version() |
ingres | select 1 |
Apache Derby | select 1 |
H2 | select 1 |
Informix | select count(*) from systables |