[JavaWeb] Database connection pool

1. What are the benefits of connection pooling?

The database connection pool is to put the database connection in a pool and get it when it needs to be used. In this way, you don't have to get the connection yourself every time.

insert image description here

① Obtaining a connection in a normal way
Every time a user accesses the database, he must obtain a connection, so there will be several problems.

  • After the connection is used, it will be destroyed, resulting in a waste of resources.
  • If there are many users, many connections need to be obtained.
  • Frequent connection data will cause excessive resource consumption, which will cause the database to crash.

②The connection pool obtains the connection.
The advantage of the connection pool is that
each time the connection is used, it is put back into the connection pool, and resources are reused, which can improve program performance.
No matter how many users, you only need to get a connection from the connection pool.
insert image description here

Two, custom connection pool

In fact, there is already a framework for the connection pool to encapsulate it, and we can use it directly, but now it is the learning stage, and we must know why. We might as well customize a connection pool to see how it works. And this can also better learn the connection pool framework.

insert image description here

① Realize the DataSource interface
dataSource is an interface in Java, and it is also a specification formulated by Java. Different connection pools need to implement this interface.
MyDataSource is a connection pool that we customize, so naturally we need to implement this interface.

②Create a connection pool container
Why use LindkedList?
The connection in the connection pool is taken away, which is equivalent to deleting the connection in the connection pool, and the user returns it to the connection pool after running out of connections, which is equivalent to increasing the connection in the connection pool, so the connection pool should be added and deleted frequently .
The bottom layer of the LinkedList collection is a linked list, which is characterized by fast addition, deletion, and slow query, so LinkedList is chosen.

③Initialize the connection pool,
that is, the construction method of the custom connection pool, the number of connections in the connection pool can be specified by the parameter count.

④ Obtaining a connection from the connection pool
A connection is taken from the connection pool, which is equivalent to deleting the connection in the connection pool.

⑤Return the connection after use
Customize a backToPool() method, call this method to return the connection after using the connection. This is equivalent to adding the connection to the connection pool.
After writing the custom connection pool, do a test:
insert image description here
①Create a connection pool
Here we initialize 6 connections for the connection pool.

② Get a connection
Now you only need to get a connection from the connection pool.

③Precompilation and processing results
④Release resources
Use the release() method encapsulated in the tool class JdbcUtil yesterday, in which the connection does not need to be released, and is replaced by null.

⑤ Return the connection
After the connection is used up, return it to the connection pool.

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/125203042