使用Spring Boot如何實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)鏈接池?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
專(zhuān)注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)旬陽(yáng)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
配置方法
基于當(dāng)前的1.5.2.RELEASE的Spring Boot。
依照官方文檔,如果增加了如下依賴的配置,或者類(lèi)路徑中存在spring-boot-starter-jdbc的jar,那么已默認(rèn)啟用了數(shù)據(jù)庫(kù)鏈接池。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
Spring Boot選擇數(shù)據(jù)庫(kù)鏈接池實(shí)現(xiàn)的判斷邏輯:
1.檢查T(mén)omcat的數(shù)據(jù)庫(kù)鏈接池實(shí)現(xiàn)是否可用,如可用,則啟用。使用spring.datasource.tomcat.*可以控制鏈接池的行為。
2.檢查HikariCP是否可用,如可用,則啟用。使用spring.datasource.hikari.*可以控制鏈接池的行為。
3.檢查Commons DBCP是否可用,如可用,則啟用;但Spring Boot不建議在生產(chǎn)環(huán)境使用該鏈接池的實(shí)現(xiàn)。
4.檢查Commons DBCP2是否可用,如可用,則啟用。使用spring.datasource.dbcp2.*可以控制鏈接池的行為。
使用tomcat-jdbc時(shí),可在application.yml增加配置項(xiàng)spring.datasource.tomcat.*來(lái)控制鏈接池的行為。比如如下配置。
spring:
datasource: url: jdbc:MySQL://localhost:3306/jackieathome?useSSL=false username: root password: mypassword # 6.x版本的MySQL JDBC驅(qū)動(dòng)類(lèi)為com.mysql.cj.jdbc.Driver # 5.X版本的MySQL JDBC驅(qū)動(dòng)類(lèi)為com.mysql.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver tomcat: max-wait: 10000 max-active: 30 test-on-borrow: true # 傳遞MySQL JDBC特有的參數(shù) db-properties: logger: net.jackieathome.db.customized.MySQLLogger gatherPerfMetrics: 'true' profileSQL: 'true' reportMetricsIntervalMillis: '60000' logSlowQueries: 'true' explainSlowQueries: 'true'
logging:
level: # 關(guān)閉其它軟件的日志,減少干擾 org: ERROR net: ERROR com: ERROR # 開(kāi)啟MySQL JDBC驅(qū)動(dòng)的日志 MySQL: DEBUG
上述spring.datasource.tomcat.*代表的配置項(xiàng),可參考tomcat-jdbc的官方文檔Apache Tomcat 8.5 - The Tomcat JDBC Connection Pool或者Apache Tomcat 8.0 - The Tomcat JDBC Connection Pool。
依據(jù)tomcat-jdbc的文檔,如需要向數(shù)據(jù)庫(kù)的JDBC驅(qū)動(dòng)傳入控制參數(shù),可以使用db-properties字段。需要注意的是,當(dāng)使用MySQL驅(qū)動(dòng)時(shí),控制參數(shù)的值需要強(qiáng)制轉(zhuǎn)換為字符串,否則創(chuàng)建數(shù)據(jù)庫(kù)鏈接時(shí)會(huì)報(bào)錯(cuò)。配置方法如上述樣例中的 reportMetricsIntervalMillis: '60000'和logSlowQueries: 'true'。
依照MySQL JDBC驅(qū)動(dòng)文檔,可以配置一個(gè)日志記錄器,用于記錄其工作時(shí)的輸出,如下是實(shí)現(xiàn)樣例。
package net.jackieathome.db.customized; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MySQLLogger implements com.mysql.cj.api.log.Log { private static Logger LOG; public MySQLLogger(String name) { LOG = LoggerFactory.getLogger(name); } @Override public boolean isDebugEnabled() { return LOG.isDebugEnabled(); } @Override public boolean isErrorEnabled() { return LOG.isErrorEnabled(); } @Override public boolean isFatalEnabled() { return LOG.isErrorEnabled(); } @Override public boolean isInfoEnabled() { return LOG.isInfoEnabled(); } @Override public boolean isTraceEnabled() { return LOG.isTraceEnabled(); } @Override public boolean isWarnEnabled() { return LOG.isWarnEnabled(); } @Override public void logDebug(Object msg) { LOG.debug("{}", msg); } @Override public void logDebug(Object msg, Throwable thrown) { LOG.debug("{}", msg, thrown); } @Override public void logError(Object msg) { LOG.error("{}", msg); } @Override public void logError(Object msg, Throwable thrown) { LOG.error("{}", msg, thrown); } @Override public void logFatal(Object msg) { LOG.error("{}", msg); } @Override public void logFatal(Object msg, Throwable thrown) { LOG.error("{}", msg, thrown); } @Override public void logInfo(Object msg) { LOG.info("{}", msg); } @Override public void logInfo(Object msg, Throwable thrown) { LOG.info("{}", msg, thrown); } @Override public void logTrace(Object msg) { LOG.trace("{}", msg); } @Override public void logTrace(Object msg, Throwable thrown) { LOG.trace("{}", msg, thrown); } @Override public void logWarn(Object msg) { LOG.warn("{}", msg); } @Override public void logWarn(Object msg, Throwable thrown) { LOG.warn("{}", msg, thrown); } }
同時(shí)修改application.yml,增加相應(yīng)的日志配置,如下。
logging: level: # 開(kāi)啟MySQL JDBC驅(qū)動(dòng)的日志 MySQL: DEBUG
如下是MySQL JDBC驅(qū)動(dòng)輸出的樣例日志。
2017-04-16 00:51:32.626 QUERY created: Sun Apr 16 00:51:32 CST 2017 duration: 0 connection: 93 statement: 1 resultset: 1 message: /* mysql-connector-java-6.0.6 ( Revision: 3dab84f4d9bede3cdd14d57b99e9e98a02a5b97d ) */SELECT @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS tx_isolation, @@wait_timeout AS wait_timeout
2017-04-16 00:51:32.629 FETCH created: Sun Apr 16 00:51:32 CST 2017 duration: 36 connection: 93 statement: 1 resultset: 1
2017-04-16 00:51:32.639 QUERY created: Sun Apr 16 00:51:32 CST 2017 duration: 1 connection: 93 statement: 999 resultset: 0 message: SET NAMES latin1
2017-04-16 00:51:32.640 FETCH created: Sun Apr 16 00:51:32 CST 2017 duration: 0 connection: 93 statement: 999 resultset: 0
2017-04-16 00:51:32.642 QUERY created: Sun Apr 16 00:51:32 CST 2017 duration: 2 connection: 93 statement: 999 resultset: 0 message: SET character_set_results = NULL
2017-04-16 00:51:32.643 FETCH created: Sun Apr 16 00:51:32 CST 2017 duration: 0 connection: 93 statement: 999 resultset: 0
2017-04-16 00:51:32.645 QUERY created: Sun Apr 16 00:51:32 CST 2017 duration: 0 connection: 93 statement: 999 resultset: 0 message: SET autocommit=1
2017-04-16 00:51:32.646 FETCH created: Sun Apr 16 00:51:32 CST 2017 duration: 0 connection: 93 statement: 999 resultset: 0
2017-04-16 00:51:32.667 QUERY created: Sun Apr 16 00:51:32 CST 2017 duration: 1 connection: 94 statement: 2 resultset: 2 message: /* mysql-connector-java-6.0.6 ( Revision: 3dab84f4d9bede3cdd14d57b99e9e98a02a5b97d ) */SELECT @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS tx_isolation, @@wait_timeout AS wait_timeout
看完上述內(nèi)容,你們掌握使用Spring Boot如何實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)鏈接池的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁(yè)名稱(chēng):使用SpringBoot如何實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)鏈接池
URL鏈接:http://www.rwnh.cn/article18/jjshgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)、微信小程序、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)