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&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

浙公网安备 33010602011771号