Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot uses HikariDataSource data sources by default and how it is configured

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

SpringBoot default use of HikariDataSource data source and configuration is how, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

SpringBoot uses HikariDataSource data source definition by default

Data source: stores all the information for establishing a database connection. By providing the correct data source name, you can find the appropriate database connection.

The data source is responsible for maintaining a database connection pool. when the program creates a data source instance, the system will create multiple database connections at one time and save these data connections in the connection pool. When the program needs to access the database, it does not need to re-obtain the database connection, but takes an idle database connection from the connection pool. when the program uses the database connection access, it does not need to close the database connection. instead, return the database connection to the connection pool. In this way, the frequent acquisition of database connections can be avoided and the performance degradation caused by closing database connections can be avoided.

Only account, password, database address and connection driver are configured under spring.datasource in the global configuration file application.yml, because HikariDataSource data sources are used by default.

If it is a custom data source, you can use the

Spring.datasource.type=com.zaxxer.hikari.HikariDataSource

Test:

@ RunWith (SpringRunner.class) @ SpringBootTestpublic class HorseApplicationTests {/ * SpringBoot has been configured by default. Programmers can directly DI injection and then use * / @ Resource DataSource dataSource; @ Testpublic void contextLoads () throws SQLException {System.out.println ("data source >" + dataSource.getClass ());}

Running result:

Data source > class com.zaxxer.hikari.HikariDataSource

Database connection

With the data source, you can get the database connection, use JdbcTemplate for CRUD database, even without the use of a third-party database operation framework, such as: Mybatis,Hibernate, Spring itself can do a lightweight package to the native JDBC, real-time JdbcTemplate.

SpringBoot not only provides the default data source, but also defaults to configure JdbcTemplate to be placed in the container, and programmers only need to inject it themselves.

The principle of JdbcTemplate auto-assembly is to rely on the JdbcTemplateAutoConfiguration class.

The default configuration of HikariDataSource is com.zaxxer.hikari.HikariConfigprivate static final long IDLE_TIMEOUT = MINUTES.toMillis (10); private static final long MAX_LIFETIME = MINUTES.toMillis (30); private static final int DEFAULT_POOL_SIZE = 10 × private static boolean unitTest = false;// Properties changeable at runtime through the HikariConfigMXBean//private volatile long connectionTimeout;private volatile long validationTimeout;private volatile long idleTimeout;private volatile long leakDetectionThreshold;private volatile long maxLifetime;private volatile int maxPoolSize;private volatile int minIdle;private volatile String username;private volatile String password After reading the above, have you mastered the default way and configuration of SpringBoot to use HikariDataSource data sources? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report