JDBC connection pool analysis

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[JDBC connection pool analysis]

Video link:

Ppt link:

https://ptteng.github.io/PPT/PPT-java/JAVA-task1-jdbcpool.html#/

1. Background introduction

When using JDBC, you need to obtain the connection (create) every time. After the connection is used up, release (destroy) the connection. Optimize the curd operation through the connection pool.

The connection pool is like a large pool. Once initialized, many links will be put into it. When you use it, you can take it from the large pool and return it to me after use. This is our connection pool.

A database connection must be established for each web request.

For example, during the double 11, I only create one link at a time. Hundreds of millions of people chop their hands, and the server may collapse.

Therefore, this reflects the necessity of connection pool, that is, when the connection pool is initialized, a certain number of connections are stored, obtained through methods when used, and returned when not in use

2. Knowledge analysis

1. Concurrency problem

In order to maximize the generality of connection management services, multi-threaded environment must be considered

2. Multi database server and multi-user for large enterprise applications, It is often necessary to connect to different databases at the same time (such as connecting Oracle and Sybase). Design a connection pool management class that conforms to the singleton mode. When the only instance of the connection pool management class is created, read a resource file, in which the URL address and other information of multiple databases are stored. According to the information provided by the resource file, create multiple instances of the connection pool class, and each instance is a specific database Connection pool. The connection pool management class instance takes a name for each connection pool instance and manages different connection pools through different names.

3. Allocation and release of connection pool

The allocation and release of connection pool have a great impact on the performance of the system. Reasonable allocation and release can improve the reuse of connections, reduce the overhead of establishing new connections, and speed up the access speed of users.

4. Configuration and maintenance of connection pool

How many connections should be placed in the connection pool to optimize the performance of the system? The system can set the minimum number of connections (minconn) and the maximum number of connections (maxconn) to control the connections in the connection pool. The minimum number of connections is the number of connections created by the connection pool when the system is started. If too many connections are created, the system will start slowly, but the system will respond quickly after creation

3. Frequently asked questions

4. Solutions

In the actual development, there are mature open source connection pools for us to use

DBCP (used by Tomcat)

DBCP is probably the most used open source connection pool, probably because it is easy to configure, and many open source and Tomcat application examples use this connection pool.

This connection pool can set the maximum and minimum connections, connection waiting time and other basic functions. For the configuration of this connection pool, see: DBCP xml

Normally. In specific project applications, it is found that the continuous operation stability of this connection pool is OK, but the speed is slightly slow. Under the pressure of large concurrency, the stability decreases. In addition, connection pool monitoring is not provided

C3p0 (must understand!) (spring and Hibernate)

C3p0 is another open source connection pool, which is also famous in the industry. This connection pool can set the maximum and minimum connections, connection waiting time and other basic functions.

For the configuration of this connection pool, see: c3p0.0 in the attachment package xml。

Normally. In the specific project application, it is found that the continuous operation stability of this connection pool is quite good, and the stability is guaranteed under the pressure of large concurrency. In addition, connection pool monitoring is not provided.

5. Coding practice

JDBC connection pool analysis_ Tencent video

In the actual development, there are mature open source connection pools for us to use

DBCP (used by Tomcat)

DBCP is probably the most used open source connection pool, probably because it is easy to configure, and many open source and Tomcat application examples use this connection pool.

This connection pool can set the maximum and minimum connections, connection waiting time and other basic functions. For the configuration of this connection pool, see: DBCP xml

Normally. In specific project applications, it is found that the continuous operation stability of this connection pool is OK, but the speed is slightly slow. Under the pressure of large concurrency, the stability decreases. In addition, connection pool monitoring is not provided

C3p0 (must understand!) (spring and Hibernate)

C3p0 is another open source connection pool, which is also famous in the industry. This connection pool can set the maximum and minimum connections, connection waiting time and other basic functions.

For the configuration of this connection pool, see: c3p0.0 in the attachment package xml。

Normally. In the specific project application, it is found that the continuous operation stability of this connection pool is quite good, and the stability is guaranteed under the pressure of large concurrency. In addition, connection pool monitoring is not provided.

6. Expand thinking

Third connection pool Druid

Alibaba, Taobao and Alipay dedicated database connection pool,

But it is not only a database connection pool, it also contains a proxydriver,

A series of built-in JDBC component libraries, an SQL parser. Support all JDBC compatible databases,

Including Oracle, mysql, Derby, PostgreSQL, SQL server, H2, etc.

7. Problem thinking

Question: will the connection pool close automatically?

The answer is to be set. For example, in the connection pool diagram of our DBCP

The figure shows the settings for closing the connection pool.

8. References

http://blog.csdn.net/shuaihj/article/details/14223015

9. More discussion

Video link:

https://v.qq.com/x/page/d0567vhdanq.html

Ppt link: https://ptteng.github.io/PPT/PPT-java/JAVA-task1-jdbcpool.html#/8

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance. Come and study with me~

My invitation code:, or you can click this link directly:

Author: mind_ 4a7b link: https://www.jianshu.com/p/beceba363798 Source: the copyright of Jianshu Jianshu belongs to the author. Please contact the author for authorization and indicate the source for any form of reprint.

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

Here is the skill tree · it Academy: a gathering place for beginners to switch to the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>