当前位置:网站首页>This article introduces you to the necessity of database connection pooling

This article introduces you to the necessity of database connection pooling

2022-06-23 20:45:00 Gourmet Jianghu

Do not use database connection pool :

In use to develop database based web The program , The traditional model Basically follow the following steps :  

  • In the main program ( Such as servlet、beans、DAO) Establishing database connection in .
  • Conduct sql operation
  • Disconnect the database .

This mode of development , The problem is :

ordinary JDBC Database connection use DriverManager To get , Every time you establish a connection to the database, you need to Connection Load into memory , Revalidation IP Address , User name and password ( It costs 0.05s~1s Time for ). When you need a database connection , Just ask the database for a , Disconnect after execution . This way will consume a lot of resources and time . The connection resources of the database are not well reused . If there are hundreds or even thousands of people online at the same time , Frequent database connection operations will occupy a lot of system resources , Serious even can cause the server to crash .

For every database connection , They have to be disconnected after use . otherwise , If the program is abnormal and Failed to close , Will lead to Memory leak in database system , It will eventually lead to a restart of the database .

This development cannot control what is created Number of connected objects , System resources will be allocated without consideration , If connected Too much , May also lead to Memory leak , Server crash .

The basic idea of database connection pool :

To solve the problem of database connection in traditional development , May adopt Database connection pool technology (connection pool) .

The basic idea of database connection pool A connection is a database “ Buffer pool ”. Put a certain number of connections in the buffer pool in advance , When you need to establish a database connection , Just from “ Buffer pool ” Take out one of , Put it back after use . The database connection pool is responsible for allocation 、 Manage and release database connections , it Allow applications to reuse an existing database connection , Instead of re establishing a .

When the database connection pool is initialized, a certain number of database connections will be created and put into the connection pool , The number of these database connections is set by the minimum number of database connections . Whether these database connections are used or not ,java train Connection pools will always guarantee at least so many connections . The maximum number of database connections in the connection pool limits the maximum number of connections that the connection pool can hold , When the number of connections the application requests from the connection pool exceeds the maximum number of connections , These requests will be added to the waiting queue .

Advantages of database connection pool technology :

Resource reuse :

Because database connections can be reused , Avoid frequent creation of , A lot of performance overhead caused by releasing connections . On the basis of reducing system consumption , On the other hand, it also increases the stability of the system operation environment .

Faster system response

The database connection pool is in the process of initialization , Often a number of database connections have been created for standby in the connection pool . At this time, the initialization of the connection is completed . For business request processing , Take advantage of the available connections directly , Avoid the time cost of database connection initialization and release , Thus, the response time of the system is reduced .

New means of resource allocation

For systems where multiple applications share the same database , It can be configured in the application layer through the database connection pool , Limit of the maximum number of available database connections for an application , Avoid an application monopolizing all database resources .

Unified connection management , Avoid database connection leaks

In a more complete database connection pool implementation , It can be set according to the pre occupation timeout , Force recycling of occupied connections , In this way, the possible resource leakage in the normal database connection operation is avoided .

原网站

版权声明
本文为[Gourmet Jianghu]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112291438054924.html

随机推荐