The concept, meaning and use of connection pooling [Important]

The concept of connection pool

    Create a fixed number of connection objects for the application and save them in the pool for reuse. Get existing from the pool every time you access it, and return it to the pool after use.

The meaning of connection pool

    First of all, we must understand that "establishing a database connection" is quite resource-intensive and time-consuming, and at the same time, the number of established database connections is also limited (so in the beginning, many friends who forgot to write and close the connection found after refreshing the application a few times. The console reports an error, but I can't find the reason, but restart the computer or restart Eclipse. This is because of the number of database connections, the oracle of the xe version is about 7 to 10 at most).

    Why establishing a database connection is quite resource and time consuming

        First establish a TCP connection; then send the corresponding TCP protocol three-way handshake; the client's account is verified, and the server returns confirmation; after user verification, it is necessary to transmit relevant connection variables such as whether to automatically submit the transaction settings, etc., there will be multiple data interactions, Then you can perform operations such as real data queries and updates.

The role of connection pool

    The connection pool is to establish multiple database connection objects in advance, and then put them into the connection pool. When there is a client request, a connection object is taken out to serve the client. When the request is completed, the client calls the .close() method. Put the connection object back into the pool. In an ordinary database connection, the client gets a physical connection. In the connection pool, the client gets a connection object. From the beginning of use to the end of use, the physical connection of the connection object is never closed, so we have to a certain extent. The time required to establish a connection is reduced, which is very beneficial for multi-use, high-concurrency websites.

Use of connection pool

    For beginners, connection pools usually introduce related jar packages

        

  Then you need to configure some properties such as committing transactions (note that the difference from the JDBC configuration is driverClassName, which has one more class)

    

In the Utils file, the following steps are required to obtain the connection object (usually when the connection object is obtained, it is written in the getConnection method, and then some judgment conditions are added. When the client does not end the connection, the obtained connection is the last transaction. The connection (using ThreadLoacl to tie the transaction to the thread), when it is accessed for the first time, it is ds.getConnection())

        

    getConnection method

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607977&siteId=291194637