网站建设资讯

NEWS

网站建设资讯

Grails3怎么更换连接池

这篇文章主要介绍“Grails3怎么更换连接池”,在日常操作中,相信很多人在Grails3怎么更换连接池问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Grails3怎么更换连接池”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联服务项目包括无极网站建设、无极网站制作、无极网页制作以及无极网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,无极网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到无极省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

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

  • 由于默认使用的是tomcat-jdbc连接池,需要去掉依赖

//    runtime "com.h3database:h3"
//    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

到此,关于“Grails3怎么更换连接池”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


分享名称:Grails3怎么更换连接池
网址分享:http://cdweb.net/article/gjpsgp.html