1. 程式人生 > 其它 >druid配置和常用操作

druid配置和常用操作

druid配置和常用操作

基於SpringBoot的專案加入druid連線池

<!--阿里連線池druid:1.2.8-2021年-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
     <version>1.2.8</version>
</dependency>

一、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