- H2数据库
# For H2 # 配置数据源 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 使用file存储数据 spring.datasource.url = jdbc:h2:file:~/.h2/spring-boot;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE # 存储在内存中 #spring.datasource.url = jdbc:h2:mem:~/.h2/spring-boot;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username = sa spring.datasource.password = # 驱动 spring.datasource.driverClassName = org.h2.Driver # 是否初始化 spring.datasource.initialize=false # 数据库结构 spring.datasource.schema=classpath:sql/h2/schema.sql # 数据文件 spring.datasource.data=classpath:sql/h2/import-data.sql # 导入编码 spring.datasource.sql-script-encoding=UTF-8 spring.datasource.continue-on-error=false # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 #spring.datasource.useGlobalDataSourceStat=true # 是否允许访问控制台 spring.h2.console.enabled=true # 配置h2控制台访问路径 默认 /h2-console spring.h2.console.path=/h2-console
这里initialize=true时和spring.datasource.filters配置同时开启时有异常,暂不清楚什么原因。
- Mysql数据库
# For Mysql spring.datasource.url = jdbc:mysql://localhost:3306/spring-boot?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = spring.datasource.driverClassName = com.mysql.jdbc.Driver
其他配置基本可以保持不变
-
原来这跟连接池的实现有关:
连接池实现 该功能属性名 The Tomcat JDBC Connection Pool validationQuery The Apache Commons DBCP Connection Pool validationQuery c3p0 - JDBC3 Connection and Statement Pooling preferredTestQuery Atomikos:Tomcat Spring ActiveMQ MySQL JMX Integration
分析Atomikos数据连接池源码,弄清testQuerytestQuery
此外,测试/验证连接池连接的SQL语句也因数据库而异:
Efficient SQL test query or validation query that will work across all (or most) databases
DBCP - validationQuery for different Databases
综合上述两个帖子,汇总结果如下:数据库 测试/验证查询 MySQL SELECT 1 PostgreSQL SELECT 1 Microsoft SQL Server SELECT 1 SQLite SELECT 1 H2 SELECT 1 Ingres SELECT 1 Oracle select 1 from dual DB2 select 1 from sysibm.sysdummy1 或
SELECT current date FROM sysibm.sysdummy1Apache Derby VALUES 1 FROM SYSIBM.SYSDUMMY1 或
SELECT 1 FROM SYSIBM.SYSDUMMY1HSQLDB SELECT 1 FROM any_existing_table WHERE 1=0 或
SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERSInformix select count(*) from systables