In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about what the connection pool in SpringBoot is like. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.
Recall that before deploying the springboot project to the mainline, the online environment required jdk7, but the project was developed based on jdk8, and springboot also used a version above springboot2. It can be said that it takes a whole day to sew and mend the code that can meet the online environment.
All right, without saying much, let's get straight to the point.
In fact, switching is not too troublesome. After switching from SpringBoot2 to SpringBoot1, the default connection pool has changed, and the stress test done before has been redone.
Next, let's examine the default data sources used by SpringBoot2 and SpringBoot1.
1. The HikariCP of SpringBoot2 first needs to introduce the dependent package in the pom file:
Org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 1.8 1.3.1 org.springframework.boot spring-boot-starter org.springframework.boot Spring-boot-starter-web mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.spring.boot.version} org.projectlombok lombok
Secondly, the following attributes need to be defined in the configuration file (default values are automatically used when not defined)
# spring related configuration spring: application: name: HikariCP Test # data Source configuration datasource: # configuration of connection Pool type: com.zaxxer.hikari.HikariDataSource hikari: minimum-idle: 5 maximum-pool-size: 15 connection-test-query: SELECT 1 max-lifetime: 1800000 connection-timeout: 30000 pool-name: DatebookHikariCP
Once configured, you can see a print message similar to this when the startup is successful:
2020-01-16 16 INFO 2312.911 INFO 9996-- [main] o.s.j.e.a.AnnotationMBeanExporter: Registering beans for JMX exposure on startup 2020-01-16 16 o.s.j.e.a.AnnotationMBeanExporter 2312.913 INFO 9996-[main] o.s.j.e.a.AnnotationMBeanExporter: Bean with name 'dataSource' has been autodetected for JMX exposure 2020-01-16 16 INFO 2312.924 INFO 9996-[main] o.s.j. E.a.AnnotationMBeanExporter: Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource Type=HikariDataSource] 2020-01-16 16 main 23 type=HikariDataSource 12.994 INFO 9996-[main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port (s): 18001 (http) with context path '2020-01-16 16 type=HikariDataSource 2313. 002 INFO 9996-[main] c.j.mmzsblog.DatasourceTestApplication: Started DatasourceTestApplication in 6.724 seconds (JVM running for 8.883)
Line 3 [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] shows the type of connection pool used.
2. After the tomcat-jdbc version of SpringBoot1 was downgraded, I didn't see the information printed above, and I almost didn't know what connection pool was used. However, it was said on the Internet that it was tomcat-jdbc;, but I believed that seeing is believing, so I had to print it somewhere to rest assured, so I did the following:
Set up a controller to simply print the connection pool information.
@ RestController public class testController {@ Resource private DataSource dataSource; @ GetMapping ("/ query") public void query () {System.out.println ("the data source connection pool information queried is" + dataSource); System.out.println ("the data source connection pool type queried is:" + dataSource.getClass ()) System.out.println ("the name of the queried data source connection pool is:" + dataSource.getPoolProperties () .getName ());}}
Then I saw the following print message, which was really tomcat-jdbc.
The data source connection pool information queried is: org.apache.tomcat.jdbc.pool.DataSource@181d8899 {ConnectionPool [defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=com.mysql.jdbc.Driver; maxActive=100; maxIdle=100; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=*; url=jdbc:mysql://localhost:3306/xxxxxx; username=xxxx; validationQuery=SELECT 1; validationQueryTimeout=-1; validatorClassName=null; validationInterval=3000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0] The type of data source connection pool queried by useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; useStatementFacade=true;} is: the name of data source connection pool queried by class org.apache.tomcat.jdbc.pool.DataSource is: Tomcat Connection Pool [1-1715657818]
In fact, we can also see the way in the pom file:
Org.springframework.boot spring-boot-starter-jdbc
This dependency in the dependency file actually indicates that SpringBoot1 uses tomcat-jdbc connection pooling.
Alas, only now do we know that the default databases used by SpringBoot2.0 and SpringBoot1.0 versions are different.
Now the reason is that we have found it, but how to solve it? Otherwise, the default connection pool for the SpringBoot1 version will be changed to the same as the SpringBoot2 version. All right, if you have an idea, let's do it.
In fact, in the version of SpringBoot1, you can also use HikariCP connection pooling. The operation is as follows:
First, the data source with default configuration is introduced and tomcat-jdbc is excluded.
Org.springframework.boot spring-boot-starter-jdbc org.apache tomcat-jdbc com.zaxxer HikariCP 3.3.1
Then configure the information about the HikariCP data source in the .yml file
# configuration of spring spring: # configuration of data source datasource: # configuration of connection pool type: com.zaxxer.hikari.HikariDataSource hikari: minimum-idle: 5 maximum-pool-size: 15 connection-test-query: SELECT 1 max-lifetime: 1800000 connection-timeout: 30000
Why should I switch the data source here to the default data source used by SpringBoot2.0? Because of the use of SpringBoot1.0 's tomcat-jdbc data source, I am afraid that the stress test can not meet the requirements, in order not to add work pressure to the test (A Fan I am such a good person)
So I did the replacement operation above.
But it must be good to do so. The advantage is that HikariCP has a fascinating advantage:
1. Bytecode level optimization (many methods are generated by JavaAssist)
2. A large number of small improvements
Replace ArrayList with FastStatementList
Unlocked set ConcurrentBag
Optimization of proxy classes (for example, using invokestatic instead of invokevirtual)
As the comparison chart on the official website shows: it's faster.
In fact, if I had used a third-party database in the first place, there would have been no such moths created by myself!
For example, Alibaba's Druid connection pool is an excellent product! How good is it? You first look at its use:
3. Other connection pooling (e.g. Druid) 3.1, reference Druid in SpringBoot1.0
As with the reference HikariCP in the previous SpringBoot1.0, exclude the default data source tomcat-jdbc before referencing the connection pool you want to use
3.1.1. First, the data source with default configuration is introduced to exclude tomcat-jdbc.
Org.springframework.boot spring-boot-starter-jdbc org.apache tomcat-jdbc com.alibaba druid-spring-boot-starter 1.1.10 com.alibaba druid 1.0.31
3.1.2. Configure information about the Druid data source in the .yml file
Spring: # configuration of data source datasource: # configuration of connection pool type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 5 max-active: 10 min-idle: 5 max-wait: 30000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 validation-query: SELECT 1 FROM DUAL validation-query-timeout: 60000 test-on-borrow: false test-on-return: false test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 100000
3.1.3. Write a configuration class to load the data source
Configuration @ ConditionalOnClass (DruidDataSource.class) @ ConditionalOnProperty (name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true) public class DataSourceConfig {@ Bean @ ConfigurationProperties ("spring.datasource.druid") public DataSource dataSourceOne () {return DruidDataSourceBuilder.create () .build ();}}
3.1.4. Startup effect:
2020-01-17 16 Registering beans for JMX exposure on startup 59 Registering beans for JMX exposure on startup 32.804 INFO 8520-[main] o.s.j.e.a.AnnotationMBeanExporter: Registering beans for JMX exposure on startup 2020-01-17 16 Registering beans for JMX exposure on startup 59 Registering beans for JMX exposure on startup 32.806 INFO 8520-[main] o.s.j.e.a.AnnotationMBeanExporter: Bean with name 'dataSourceOne' has been autodetected for JMX exposure 2020-01-17 16 INFO 59 Registering beans for JMX exposure on startup 32.808 INFO 8520-[main] o.s.j. E.a.AnnotationMBeanExporter: Bean with name 'statFilter' has been autodetected for JMX exposure 2020-01-17 16 purge 59 INFO 32.818 INFO 8520-[main] o.s.j.e.a.AnnotationMBeanExporter: Located MBean' dataSourceOne': registering with JMX server as MBean [com.alibaba.druid.spring.boot.autoconfigure:name=dataSourceOne Type=DruidDataSourceWrapper] 2020-01-17 16 main 59 o.s.j.e.a.AnnotationMBeanExporter 32.822 INFO 8520-[main] o.s.j.e.a.AnnotationMBeanExporter: Located MBean 'statFilter': registering with JMX server as MBean [com.alibaba.druid.filter.stat:name=statFilter Type=StatFilter] 2020-01-17 16 main 59 s.b.c.e.t.TomcatEmbeddedServletContainer 32.932 INFO 8520-[main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port (s): 18001 (http) 2020-01-17 16 type=StatFilter 59 Switzerland 32.943 INFO 8520-[main] c.j.mmzsblog.DatasourceTestApplication:
3.2.Quote Druid in SpringBoot2.0
Referencing Druid in SpringBoot2.0 is similar to introducing similar in SpringBoot1.0
3.2.1. Do not need to exclude the default configuration of the data source, but directly introduce the Druid data source
Com.alibaba druid-spring-boot-starter 1.1.10 com.alibaba druid 1.0.31
3.2.2. The information about the Druid data source configured in the .yml file is the same as 3.1.3
3.2.3. Write a configuration class to load the data source as 3.1.3
3.2.4. After startup, you can also see similar information printed.
Located MBean 'dataSourceOne': registering with JMX server as MBean [com.alibaba.druid.spring.boot.autoconfigure:name=dataSourceOne,type=DruidDataSourceWrapper]
3.3. Where is the excellence?
Look at the above use, super simple and non-existent?
First of all, let's take a look at the comparison between several traditional connection pooling given on druid's official website:
As can be seen from the above table, Druid connection pool far exceeds the competition in terms of performance, monitoring, diagnosis, security, scalability and so on.
The official website introduces it as follows:
Druid connection pooling is Alibaba's open source database connection pooling project. Druid connection pool is created for monitoring, built-in powerful monitoring functions, monitoring features do not affect performance. Powerful, anti-SQL injection, built-in Loging can diagnose Hack application behavior.
This is what the connection pool in SpringBoot is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.