Spring Boot Druid 如何配置?
目的
記錄兩種配置方式。
網上說最佳的配置
spring bean 版
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本屬性 url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="5" />
<property name="minIdle" value="5" />
<property name="maxActive" value="10" />
<!-- 配置從連接池獲取連接等待超時的時間 -->
<property name="maxWait" value="10000" />
<!-- 配置間隔多久啓動一次DestroyThread,對連接池內的連接才進行一次檢測,單位是毫秒。
檢測時:1.如果連接空閒並且超過minIdle以外的連接,如果空閒時間超過minEvictableIdleTimeMillis設置的值則直接物理關閉。2.在minIdle以內的不處理。
-->
<property name="timeBetweenEvictionRunsMillis" value="600000" />
<!-- 配置一個連接在池中最大空閒時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 設置從連接池獲取連接時是否檢查連接有效性,true時,每次都檢查;false時,不檢查 -->
<property name="testOnBorrow" value="false" />
<!-- 設置往連接池歸還連接時是否檢查連接有效性,true時,每次都檢查;false時,不檢查 -->
<property name="testOnReturn" value="false" />
<!-- 設置從連接池獲取連接時是否檢查連接有效性,true時,如果連接空閒時間超過minEvictableIdleTimeMillis進行檢查,否則不檢查;false時,不檢查 -->
<property name="testWhileIdle" value="true" />
<!-- 檢驗連接是否有效的查詢語句。如果數據庫Driver支持ping()方法,則優先使用ping()方法進行檢查,否則使用validationQuery查詢進行檢查。(Oracle jdbc Driver目前不支持ping方法) -->
<property name="validationQuery" value="select 1 from dual" />
<!-- 單位:秒,檢測連接是否有效的超時時間。底層調用jdbc Statement對象的void setQueryTimeout(int seconds)方法 -->
<!-- <property name="validationQueryTimeout" value="1" /> -->
<!-- 打開後,增強timeBetweenEvictionRunsMillis的週期性連接檢查,minIdle內的空閒連接,每次檢查強制驗證連接有效性. 參考:https://github.com/alibaba/druid/wiki/KeepAlive_cn -->
<property name="keepAlive" value="true" />
<!-- 連接泄露檢查,打開removeAbandoned功能 , 連接從連接池借出後,長時間不歸還,將觸發強制回連接。回收週期隨timeBetweenEvictionRunsMillis進行,如果連接爲從連接池借出狀態,並且未執行任何sql,並且從借出時間起已超過removeAbandonedTimeout時間,則強制歸還連接到連接池中。 -->
<property name="removeAbandoned" value="true" />
<!-- 超時時間,秒 -->
<property name="removeAbandonedTimeout" value="80"/>
<!-- 關閉abanded連接時輸出錯誤日誌,這樣出現連接泄露時可以通過錯誤日誌定位忘記關閉連接的位置 -->
<property name="logAbandoned" value="true" />
<!-- 根據自身業務及事務大小來設置 -->
<property name="connectionProperties"
value="oracle.net.CONNECT_TIMEOUT=2000;oracle.jdbc.ReadTimeout=10000"></property>
<!-- 打開PSCache,並且指定每個連接上PSCache的大小,Oracle等支持遊標的數據庫,打開此開關,會以數量級提升性能,具體查閱PSCache相關資料 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- 配置監控統計攔截的filters -->
<!-- <property name="filters" value="stat,slf4j" /> -->
<property name="proxyFilters">
<list>
<ref bean="log-filter" />
<ref bean="stat-filter" />
</list>
</property>
<!-- 配置監控統計日誌的輸出間隔,單位毫秒,每次輸出所有統計數據會重置,酌情開啓 -->
<property name="timeBetweenLogStatsMillis" value="120000" />
</bean>
Spring Boot yml 版
spring:
datasource:
name: mysql_spring_yml
type: com.alibaba.druid.pool.DruidDataSource
# druid相關配置
druid:
#基本屬性
url: jdbc:mysql://ip:3306/database?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: db_user
password: db_password
filter:
config:
enabled: true
# 監控統計攔截的filters
driver-class-name: com.mysql.jdbc.Driver
# 配置初始化大小/最小/最大
initial-size: 5
min-idle: 5
max-active: 10
# 獲取連接等待超時時間
max-wait: 10000
# 間隔多久進行一次檢測,檢測需要關閉的空閒連接
time-between-eviction-runs-millis: 60000
# 一個連接在池中最小生存的時間
min-evictable-idle-time-millis: 30000
# 設置從連接池獲取連接時是否檢查連接有效性,true時,每次都檢查;false時,不檢查
test-on-borrow: false
# 設置往連接池歸還連接時是否檢查連接有效性,true時,每次都檢查;false時,不檢查
test-on-return: false
# 設置從連接池獲取連接時是否檢查連接有效性,true時,如果連接空閒時間超過minEvictableIdleTimeMillis進行檢查,否則不檢查;false時,不檢查
test-while-idle: true
# 檢驗連接是否有效的查詢語句。如果數據庫Driver支持ping()方法,則優先使用ping()方法進行檢查,否則使用validationQuery查詢進行檢查。(Oracle jdbc Driver目前不支持ping方法)
validation-query: SELECT 'x'
# 單位:秒,檢測連接是否有效的超時時間。底層調用jdbc Statement對象的void setQueryTimeout(int seconds)方法
validation-query-timeout: 1
# 打開後,增強timeBetweenEvictionRunsMillis的週期性連接檢查,minIdle內的空閒連接,每次檢查強制驗證連接有效性. 參考:https://github.com/alibaba/druid/wiki/KeepAlive_cn
keep-alive: true
# 連接泄露檢查,打開removeAbandoned功能 ,連接從連接池借出後,長時間不歸還,將觸發強制回連接。回收週期隨timeBetweenEvictionRunsMillis進行,如果連接爲從連接池借出狀態,並且未執行任何sql,並且從借出時間起已超過removeAbandonedTimeout時間,則強制歸還連接到連接池中。
# 打開PSCache,並指定每個連接上PSCache的大小。oracle設爲true,mysql設爲false。分庫分表較多推薦設置爲false
remove-abandoned: true
# 超時時間,秒
remove-abandoned-timeout: 80
# 關閉abanded連接時輸出錯誤日誌,這樣出現連接泄露時可以通過錯誤日誌定位忘記關閉連接的位置
log-abandoned: true
# 根據自身業務及事務大小來設置
# connection-properties:
# 打開PSCache,並且指定每個連接上PSCache的大小,Oracle等支持遊標的數據庫,打開此開關,會以數量級提升性能,具體查閱PSCache相關資料
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置監控統計日誌的輸出間隔,單位毫秒,每次輸出所有統計數據會重置,酌情開啓
time-between-log-stats-millis: 120000
filters: stat
# filter: stat,slf4j
# proxy-filters: log-filter,stat-filter