欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot数据库链接池配置方法

程序员文章站 2024-02-29 10:46:10
配置方法 基于当前的1.5.2.release的spring boot。 依照官方文档,如果增加了如下依赖的配置,或者类路径中存在spring-boot-starter...

配置方法

基于当前的1.5.2.release的spring boot。

依照官方文档,如果增加了如下依赖的配置,或者类路径中存在spring-boot-starter-jdbc的jar,那么已默认启用了数据库链接池。

 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-jdbc</artifactid>
 </dependency>

spring boot选择数据库链接池实现的判断逻辑:

1.检查tomcat的数据库链接池实现是否可用,如可用,则启用。使用spring.datasource.tomcat.*可以控制链接池的行为。

2.检查hikaricp是否可用,如可用,则启用。使用spring.datasource.hikari.*可以控制链接池的行为。

3.检查commons dbcp是否可用,如可用,则启用;但spring boot不建议在生产环境使用该链接池的实现。

4.检查commons dbcp2是否可用,如可用,则启用。使用spring.datasource.dbcp2.*可以控制链接池的行为。

使用tomcat-jdbc时,可在application.yml增加配置项spring.datasource.tomcat.*来控制链接池的行为。比如如下配置。

spring:

datasource:
  url: jdbc:mysql://localhost:3306/jackieathome?usessl=false
  username: root
  password: mypassword
  # 6.x版本的mysql jdbc驱动类为com.mysql.cj.jdbc.driver
  # 5.x版本的mysql jdbc驱动类为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特有的参数
   db-properties:
    logger: net.jackieathome.db.customized.mysqllogger
    gatherperfmetrics: 'true'
    profilesql: 'true'
    reportmetricsintervalmillis: '60000'
    logslowqueries: 'true'
    explainslowqueries: 'true'

    logging:

 level:
  # 关闭其它软件的日志,减少干扰
  org: error
  net: error
  com: error
  # 开启mysql jdbc驱动的日志
  mysql: debug

上述spring.datasource.tomcat.*代表的配置项,可参考tomcat-jdbc的官方文档apache tomcat 8.5 - the tomcat jdbc connection pool或者apache tomcat 8.0 - the tomcat jdbc connection pool

依据tomcat-jdbc的文档,如需要向数据库的jdbc驱动传入控制参数,可以使用db-properties字段。需要注意的是,当使用mysql驱动时,控制参数的值需要强制转换为字符串,否则创建数据库链接时会报错。配置方法如上述样例中的 reportmetricsintervalmillis: '60000'和logslowqueries: 'true'。

依照mysql jdbc驱动文档,可以配置一个日志记录器,用于记录其工作时的输出,如下是实现样例。

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);
  }
}

同时修改application.yml,增加相应的日志配置,如下。

logging:
  level:
    # 开启mysql jdbc驱动的日志
    mysql: debug

如下是mysql jdbc驱动输出的样例日志。

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

以上所述是小编给大家介绍的spring boot数据库链接池配置方法,希望对大家有所帮助