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

The difference between HikariDataSource and JdbcTemplate in Spring

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the difference between HikariDataSource and JdbcTemplate in Spring. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Understanding of data Source and connection Pool

Data source: refers to the source of data, such as mysql, postgresql database, etc.

Connection pool: it is a pool in which multiple threads (called "connection") are stored. When users want to connect to a data source, they will take a "connection" from the pool. After the user has finished using the "connection", the "connection" will be released and the "connection" will be returned to the pool for other users to use.

If there is no connection pool, users will create a connection each time they operate the database. If the number of concurrency is large (let's say 10w), frequent connection creation will take up cpu and memory resources, and will cause excessive errors in database connections (the number of database connections is limited).

When there is a connection pool, assuming that the maximum number of connections in the connection pool is 200. now the program has 10w concurrent operations. At this time, 10w concurrency will be queued to use these 200connections to perform sql. In this way, the connection between the connection pool and the database is limited to 200, which will not cause too many database connection errors.

HikariDataSourceHikariConfig hikariConfig = new HikariConfig (); hikariConfig.setDriverClassName (); hikariConfig.setJdbcUrl (); hikariConfig.setUsername (); hikariConfig.setPassword (); HikariDataSource hikariDataSource = new HikariDataSource (hikariConfig); boolean close = hikariDataSource.getConnction (). IsClosed (); JdbcTemplate

It is a tool class provided by spring to operate the database after encapsulating the original jdbc. We can use it to complete the operation of adding, deleting, changing and searching the database.

Manual connection data source process

1 > parameters such as connection name, database server ip, port, user name, password, database type, etc.

2 > the backend connects to the database and saves the connection name information to the database after success. Cache records the relationship between the id and the data source.

3 > the front end queries all database lists, that is, "show databases". The id; backend obtains the data source from the cache according to the record id, and then executes the sql query.

4 > the front end queries the list of all tables in a database, that is, "show tables", transfers the data source record id and the database name; the back end obtains the record details from the database, changes the database name, re-acquires the database connection, and establishes a new data source. The cache name is the combination of data source name and database name.

5 > the front end queries the list of column names of a table in a database, and transfers the data source record id, database name, table name; the back end fetches the data source from the cache according to the record id (data source name) + database name, and executes the business sql.

The difference between HikariDataSource and JdbcTemplate in Spring is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

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