Spring Boot 2. + default connection pool Detailed HikariCP

A, Spring Boot 2. + default connection pool HikariCP

If Spring Boot2. +, Then the default connection pool is Hikaricp, no need to import additional packages and configuration, how to prove? Start the project, you can see the console

9571610-50e2bab28f2d627c.png
Start information HikariPool
9571610-d20d3af6eddd403a.png
Start information HikariDataSource

We saw in the console

HikariPool-1 - Starting...
HikariPool-1 - Start completed

Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]

Description Spring Boot 2. + is the default connection pool HikariCP

Second, adjusting the connection pool parameters

HikariCP connection pooling provides a default value if there is a need also to adjust the parameters in the configuration file

spring:
  ########-spring datasource-########
  datasource:
     #账号配置
     url: jdbc:mysql://127.0.0.1:3306/retail_db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
     username: ENC(5raHicqGiQ1nEXKO+R9ykYwJUrD/+nbx)
     password: ENC(1NiRLG1lUkzLSg3uerUwU0bIRCDYiZnX)
     driver-class-name: com.mysql.cj.jdbc.Driver

     #hikari数据库连接池
     hikari:
      pool-name: Retail_HikariCP
      minimum-idle: 5 #最小空闲连接数量
      idle-timeout: 180000 #空闲连接存活最大时间,默认600000(10分钟)
      maximum-pool-size: 10 #连接池最大连接数,默认是10
      auto-commit: true  #此属性控制从池返回的连接的默认自动提交行为,默认值:true
      max-lifetime: 1800000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
      connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000
      connection-test-query: SELECT 1

Three, HikariCP connection pool faster than the other causes the connection pool

  • Compared to the general connection pool to use an ArrayList Statement object stored in ConnectionProxy, HikariCP use FatList stored objects, and wherein the difference is, each execution ArrayList get (Index) Method, need to be the range List inspection, and FastList not, in the case to ensure the legitimacy of range, range checking can save overhead.

  • Also in Java code, many operating on Connection, is finished using the connection directly, were previously traversed from beginning to end, to close the corresponding Connection, while HikariCP Connection is set to scan from the tail, holistically said that starting from the end of the performance better.

  • Internal lock without a set of storage, and wherein the switching operation is optimized.

  • 从字节码指令的角度去优化,连接池使用
    PROXY_FACTORY.getProxyPreparedStatement(this, delegate.prepareStatement(sql, columnNames))语句来建立statements,resultset等的实例,将其翻译成字节码指令后,会有如下的指令,0: getstatic和15: invokevirtual #69。将上述的方法改为使用 ProxyFactory.getProxyPreparedStatement(this, delegate.prepareStatement(sql, columnNames)),后,就不在需要0: getstatic指令,并且使用了12: invokestatic #67代替了15: invokevirtual #69,前者invokestatic 更容易被JIT优化。另外从堆栈的角度来说,堆栈大小也从原来的5变成了4。

四、HiKariCP和Druid对比

我们所熟知的C3P0,DBCP,Druid, HiKariCP为我们所常用的数据库连接池,其中C3P0已经很久没有更新了。DBCP更新速度很慢,基本处于不活跃状态,而Druid和HikariCP处于活跃状态的更新中,这就是我们说的二代产品了。

HiKariCP

  • 字节码精简 :优化代码,直到编译后的字节码最少,这样,CPU缓存可以加载更多的程序代码;
  • 优化代理和拦截器 :减少代码,例如HikariCP的Statement proxy只有100行代码,只有BoneCP的十分之一;
  • 自定义数组类型(FastStatementList)代替ArrayList :避免每次get()调用都要进行range check,避免调用remove()时的从头到尾的扫描;
  • 自定义集合类型(ConcurrentBag :提高并发读写的效率;
  • 其他针对BoneCP缺陷的优化。

Druid

  • Druid offers high performance connection pooling function, but also integrates SQL monitoring, blacklist interception function,
  • Powerful monitoring features provided by monitoring Druid, you can clearly know that the connection pool and SQL work.
  • Monitoring SQL execution time, ResultSet hold time, number of rows, updating rows, the number of errors, error stack information;
  • Consuming SQL execution interval distribution. What is the time-consuming interval distribution of it? For example, a SQL performed 1000 times, where 0 a 50 millisecond interval, a 10 ms 800, 10 100 100 ms 100 1000 ms 30 times, 15 times 1 to 10 seconds, 10 seconds or 5 times. Interval distribution through time-consuming, can be very time-consuming situation well aware of SQL;
  • Application monitoring connection pool creation and destruction of physical connection number, the number of logical connections on and off, non-empty waiting times, PSCache hit rate.
  • Facilitate the expansion. Druid offers extended API Filter-Chain mode, you can write your own method Filter intercept any JDBC, you can do anything on it, such as performance monitoring, SQL audit, user name and password encryption, logging and so on.

Overall:
1, HiKariCP performance than Druid
2, HiKariCP is Spring Boot 2+ official support, better compatibility and Spring Boot
3, the advantage is to monitor Druid perfect, better scalability (but too much can also intercept frame and the frame to increase the complexity of the performance)

Specifically, as the team selected HiKariCP or Druid specific needs, and provide Druid usage: the Spring Druid connection pool using the Boot integration Mybatis-Plus connection Mysql database

Reference article

Reproduced in: https: //www.jianshu.com/p/7e4c0e9ad49a

Guess you like

Origin blog.csdn.net/weixin_34099526/article/details/91077603