Mysql 小记 No operations allowed after connection closed.错误

  1. 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配置同时开启时有异常,暂不清楚什么原因。

  2. 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

    其他配置基本可以保持不变

  3. 原来这跟连接池的实现有关:

    连接池实现该功能属性名
    The Tomcat JDBC Connection PoolvalidationQuery
    The Apache Commons DBCP Connection PoolvalidationQuery
    c3p0 - JDBC3 Connection and Statement PoolingpreferredTestQuery
    Atomikos:Tomcat Spring ActiveMQ MySQL JMX Integration
    分析Atomikos数据连接池源码,弄清testQuery
    testQuery


    此外,测试/验证连接池连接的SQL语句也因数据库而异:
    Efficient SQL test query or validation query that will work across all (or most) databases
    DBCP - validationQuery for different Databases

    综合上述两个帖子,汇总结果如下:
    数据库测试/验证查询
    MySQLSELECT 1
    PostgreSQLSELECT 1
    Microsoft SQL ServerSELECT 1
    SQLiteSELECT 1
    H2SELECT 1
    IngresSELECT 1
    Oracleselect 1 from dual
    DB2select 1 from sysibm.sysdummy1 或
    SELECT current date FROM sysibm.sysdummy1
    Apache DerbyVALUES 1 FROM SYSIBM.SYSDUMMY1 或
    SELECT 1 FROM SYSIBM.SYSDUMMY1
    HSQLDBSELECT 1 FROM any_existing_table WHERE 1=0 或
    SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS
    Informixselect count(*) from systables

转载于:https://my.oschina.net/u/2859567/blog/745926

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值