zoukankan      html  css  js  c++  java
  • druid连接池的配置和应用

    druid配置和常用操作

    基于SpringBoot的项目加入druid连接池

    <!--阿里连接池druid:1.1.14-2019年-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
         <version>1.1.14</version>
    </dependency>

    参考网址:https://www.cnblogs.com/halberd-lee/p/11304790.html

    好文分享:https://blog.csdn.net/scholar_man/article/details/78844172

         https://www.jianshu.com/p/4cb04939e370

    一、SpringBoot中的properties配置

    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    #驱动配置信息
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    #基本连接信息
    spring.datasource.username = root
    spring.datasource.password = root
    spring.datasource.url=jdbc:mysql://192.168.153.23:3306/mytest?useUnicode=true&characterEncoding=utf-8
    
    #连接池属性
    spring.datasource.druid.initial-size=15
    spring.datasource.druid.max-active=100
    spring.datasource.druid.min-idle=15
    spring.datasource.druid.max-wait=60000
    spring.datasource.druid.time-between-eviction-runs-millis=60000
    spring.datasource.druid.min-evictable-idle-time-millis=300000
    spring.datasource.druid.test-on-borrow=false
    spring.datasource.druid.test-on-return=false
    spring.datasource.druid.test-while-idle=true
    spring.datasource.druid.validation-query=SELECT 1
    spring.datasource.druid.validation-query-timeout=1000
    spring.datasource.druid.keep-alive=true
    spring.datasource.druid.remove-abandoned=true
    spring.datasource.druid.remove-abandoned-timeout=180
    spring.datasource.druid.log-abandoned=true
    spring.datasource.druid.pool-prepared-statements=true
    spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
    spring.datasource.druid.filters=stat,wall,slf4j
    spring.datasource.druid.use-global-data-source-stat=true
    spring.datasource.druid.preparedStatement=true
    spring.datasource.druid.maxOpenPreparedStatements=100
    spring.datasource.druid.connect-properties.mergeSql=true
    spring.datasource.druid.connect-properties.slowSqlMillis=5000

    二、Spring中配置连接池

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
          <!-- ”连接“的基本属性  -->
          <property name="url" value="jdbc_url" />
          <property name="username" value="${jdbc_user}" />
          <property name="password" value="${jdbc_password}" />
          <!-- 连接池属性 -->
          <property name="initialSize" value="100" />
          <property name="maxActive" value="1000" />
          <property name="maxWait" value="60000" />
          <property name="minEvictableIdleTimeMillis" value=300000 />
          <property name="keepAlive" value=true />
          <property name="timeBetweenEvictionRunsMillis" value=-1 />
          <property name="minIdle" value="20" />
          <property name="removeAbandoned" value="true"/>
          <property name="removeAbandonedTimeout" value="180"/>
          <property name="logAbandoned" value="true" />
          <property name="testWhileIdle" value="true" />
          <property name="validationQuery" value="SELECT 'x'" />
          <property name="testOnBorrow" value="false" />
          <property name="testOnReturn" value="false" />
          <property name="poolPreparedStatements" value="true"/>
          <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
          <property name="filters" value="stat,wall,slf4j"/>
          <property name="connectionProperties" value="druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000" />
    </bean>

    三、jdbc配置连接池

    jdbc.properties:
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://172.49.15.55:3306/testdb?useUnicode=true&amp;characterEncoding=utf-8
    jdbc.username=test
    jdbc.password=test
    jdbc.filters=stat
    jdbc.maxActive=300
    jdbc.initialSize=2
    jdbc.maxWait=60000
    jdbc.minIdle=1
    jdbc.timeBetweenEvictionRunsMillis=60000
    jdbc.minEvictableIdleTimeMillis=300000
    jdbc.validationQuery=SELECT 'x'
    jdbc.testWhileIdle=true
    jdbc.testOnBorrow=false
    jdbc.testOnReturn=false
    jdbc.poolPreparedStatements=false
    jdbc.maxPoolPreparedStatementPerConnectionSize=50

    四、常见问题

    4.1 空闲检查问题

    在使用阿里的SLB时,建议将timeBetweenEvictionRunsMillis设置为2秒或者负值(关闭检查机制)。否则,连接进程会报:

    Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicaiotnsException: Communications link failure

    设置

    spring.datasource.druid.time-between-eviction-runs-millis=60000

    欢迎加入作者微信公众号

  • 相关阅读:
    Qt MFC 混合编程的问题
    DECLARE_MESSAGE_MAP用法
    DECLARE_DYNCREATE与DECLARE_DYNAMIC区别
    Qt unsigned char* (uchar*) 转为QImage
    C++ SafeArrayAccessData,SafeArrayUnaccessData使用
    C++ 实现 COM → IUnknown → 接口
    C++ COM编程之IUnknown接口
    C++ COM三大接口:IUnknown、IClassFactory、IDispatch。
    C++ COM组件QueryInterface函数
    C++ COM组件的AddRef和Release()方法使用
  • 原文地址:https://www.cnblogs.com/zhanqing/p/druid.html
Copyright © 2011-2022 走看看