Grails3更换连接池为阿里巴巴Druid

栏目: Groovy · 发布时间: 5年前

内容简介:Grails3默认数据库连接池为

Grails3默认数据库连接池为 tomcat-jdbc pool ,性能较 druidHikariCP 差太多,所以想换成其他连接池

  • 由于默认使用的是 tomcat-jdbc 连接池,需要去掉依赖
//    runtime "com.h2database:h2"
//    runtime "org.apache.tomcat:tomcat-jdbc"

// 添加阿里巴巴druid连接池
compile group: 'com.alibaba', name: 'druid', version: '1.1.18'
compile 'mysql:mysql-connector-java:5.1.40'
复制代码
  • grails-app/conf/applicaiton.yml 配置也要注释掉默认的连接池
dataSource:
#    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    username: root
    password: root
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
    # 为了兼容grails,让Hibernate自动建表,所以在dataSource下增加连接池相关的配置
    druid:
        minIdle: 1
        maxActive: 40
        maxWait: 60000
        initialSize: 20
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        removeAbandoned: true
        filters: stat,wall,slf4j
        validationQuery: select 'x'
        useGlobalDataSourceStat: true
        poolPreparedStatements: true
        minEvictableIdleTimeMillis: 300000
        timeBetweenEvictionRunsMillis: 60000
        connectionProperties: clientEncoding=UTF-8
        maxPoolPreparedStatementPerConnectionSize: 20
environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
    test:
        dataSource:
            dbCreate: update
            url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
    production:
        dataSource:
            dbCreate: update
            url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
server:
  port: 8081  # TRANSACTION_READ_COMMITTED
复制代码
  • grails-app/conf/spring/resources.groovy 配置
// Place your Spring DSL code here
beans = {

    // 监控管理页面配置(管理用户名、密码)
    druidConsoleServlet(org.springframework.boot.web.servlet.ServletRegistrationBean) {
        servlet = bean(com.alibaba.druid.support.http.StatViewServlet)
        urlMappings = ["/druid/*"]
//        urlMappings = ["*.js","*.gif","*.jpg","*.png","*.css","*.ico","/druid/*"]
        initParameters = [
                "loginUsername": "root",
                "loginPassword": "123456",
                "allow": "127.0.0.1",
                "deny": ""
        ]
//        loadOnStartup = 2
    }


    // alibaba druid配置
    dataSource(com.alibaba.druid.pool.DruidDataSource) { bean ->
        bean.initMethod = 'init'
        bean.destroyMethod = 'close'
        driverClassName = grailsApplication.config.dataSource.driverClassName
        url = grailsApplication.config.dataSource.url
        username = grailsApplication.config.dataSource.username
        password = grailsApplication.config.dataSource.password
        initialSize = grailsApplication.config.dataSource.druid.initialSize
        minIdle = grailsApplication.config.dataSource.druid.minIdle
        maxActive = grailsApplication.config.dataSource.druid.maxActive
        maxWait = grailsApplication.config.dataSource.druid.maxWait
        timeBetweenEvictionRunsMillis = grailsApplication.config.dataSource.druid.timeBetweenEvictionRunsMillis
        minEvictableIdleTimeMillis = grailsApplication.config.dataSource.druid.minEvictableIdleTimeMillis
        validationQuery = grailsApplication.config.dataSource.druid.validationQuery
        removeAbandoned = grailsApplication.config.dataSource.druid.removeAbandoned
        testWhileIdle = grailsApplication.config.dataSource.druid.testWhileIdle
        connectionProperties = grailsApplication.config.dataSource.druid.connectionProperties
        testOnBorrow = grailsApplication.config.dataSource.druid.testOnBorrow
        poolPreparedStatements = grailsApplication.config.dataSource.druid.poolPreparedStatements
        testOnReturn = grailsApplication.config.dataSource.druid.testOnReturn
        useGlobalDataSourceStat = grailsApplication.config.dataSource.druid.useGlobalDataSourceStat
        maxPoolPreparedStatementPerConnectionSize = grailsApplication.config.dataSource.druid.maxPoolPreparedStatementPerConnectionSize
        filters = grailsApplication.config.dataSource.druid.filters
    }
}
复制代码
  • grails-app/conf/logback.groovy 添加日志配置
// 日志采集
logger('com.alibaba', DEBUG, ['STDOUT'], false)
复制代码
  • 启动项目,出现日志
2019-07-03 03:15:06.691  WARN --- [           main] com.alibaba.druid.pool.DruidDataSource   : removeAbandoned is true, not use in productiion.
2019-07-03 03:15:07.078  INFO --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
Grails application running at http://localhost:8081 in environment: development
复制代码
  • web 监控地址
http://127.0.0.1:8081/druid/index.html
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据挖掘导论

数据挖掘导论

(美)Pang-Ning Tan、Michael Steinbach、Vipin Kumar / 机械工业出版社 / 2010-9 / 59.00元

本书全面介绍了数据挖掘的理论和方法,着重介绍如何用数据挖掘知识解决各种实际问题,涉及学科领域众多,适用面广。 书中涵盖5个主题:数据、分类、关联分析、聚类和异常检测。除异常检测外,每个主题都包含两章:前面一章讲述基本概念、代表性算法和评估技术,后面一章较深入地讨论高级概念和算法。目的是使读者在透彻地理解数据挖掘基础的同时,还能了解更多重要的高级主题。 本书特色 ·包含大量的图表、......一起来看看 《数据挖掘导论》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码