Database connection pool (Druid (Druid))

The necessity of JDBC database connection pool

When developing database-based web programs, the traditional model basically follows the following steps

Establish a database connection in the main program (such as servlet, beans)

Perform sql operations

Disconnect from the database  

This mode of development has problems:

Ordinary JDBC database connections are obtained using DriverManager. Every time a connection is established to the database, the Connection must be loaded into the memory, and then the user name and password are verified (it takes 0.05s to 1s). When a database connection is required, one is requested from the database, and the connection is disconnected after the execution is completed. Such an approach will consume a lot of resources and time. Database connection resources are not well reused. If hundreds or even thousands of people are online at the same time, frequent database connection operations will take up a lot of system resources, and even cause the server to crash.

For each database connection, it must be disconnected after use. Otherwise, if the program fails to close due to an exception, it will cause a memory leak in the database system, which will eventually cause the database to be restarted. (Recall: What is a Java memory leak?)

This kind of development cannot control the number of connection objects created, and system resources will be allocated without consideration. If there are too many connections, it may also cause memory leaks and server crashes.

 Database connection pool technology

In order to solve the database connection problem in traditional development, database connection pool technology can be used. The basic idea of ​​the database connection pool: is to establish a "buffer pool" for the database connection. Put a certain number of connections in the buffer pool in advance. When a database connection needs to be established, you only need to take one out of the "buffer pool" and put it back after use.

The database connection pool is responsible for allocating, managing, and releasing database connections, which allows an application to reuse an existing database connection instead of re-establishing one.

When the database connection pool is initialized, a certain number of database connections will be created and placed in the connection pool. The number of these database connections is set by the minimum number of database connections. Regardless of whether these database connections are used or not, the connection pool will always be guaranteed to have at least this many connections. The maximum number of database connections in the connection pool limits the maximum number of connections that the connection pool can occupy. When the number of connections requested by the application from the connection pool exceeds the maximum number of connections, these requests will be added to the waiting queue.

 

working principle: 

 

Advantages of database connection pool technology

1. Resource reuse Since the database connection can be reused, it avoids the frequent creation and release of a large amount of performance overhead caused by the connection. On the basis of reducing system consumption, on the other hand, it also increases the stability of the system operating environment.

2. Faster system response speed During the initialization process of the database connection pool, several database connections are often created and placed in the connection pool for standby. At this point, the initialization of the connection has been completed. For business request processing, the existing available connections are directly used to avoid the time overhead of the database connection initialization and release process, thereby reducing the system response time

3. New resource allocation method For a system where multiple applications share the same database, the database connection pool can be configured at the application layer to limit the maximum number of available database connections for an application, preventing an application from monopolizing all database resources

4. Unified connection management to avoid database connection leaks In a relatively complete database connection pool implementation, the occupied connection can be forcibly recovered according to the pre-occupied timeout setting, thus avoiding possible resource leakage in conventional database connection operations

Various open source database connection pools

The database connection pool of JDBC is represented by javax.sql.DataSource. DataSource is just an interface, which is usually implemented by servers (Weblogic, WebSphere, Tomcat), and some open source organizations also provide implementations:

DBCP is a database connection pool provided by Apache. The tomcat server comes with a dbcp database connection pool. The speed is faster than c3p0, but because of its own BUG, ​​Hibernate3 no longer provides support.

C3P0 is a database connection pool provided by an open source organization. It is relatively slow and stable. hibernate official recommendation

Proxool is an open source project database connection pool under sourceforge. It has the function of monitoring the status of the connection pool, and its stability is a little worse than that of c3p0.

BoneCP is a database connection pool provided by an open source organization, fast

Druid is a database connection pool provided by Ali. It is said to be a database connection pool that combines the advantages of DBCP, C3P0 and Proxool, but the speed is not sure whether it is as fast as BoneCP.

DataSource is usually called a data source, it contains two parts of connection pool and connection pool management, it is customary to call DataSource a connection pool

DataSource is used to replace DriverManager to obtain Connection, which is fast and can greatly improve the database access speed. Special note: Data source and database connection are different, there is no need to create multiple data sources, it is a factory for generating database connections, so the entire application only needs one data source.

When the database access is over, the program still closes the database connection as before: conn.close(); but conn.close() does not close the physical connection to the database, it only releases the database connection and returns it to the database connection pool.

Druid (Druid) database connection pool

Druid is an implementation of a database connection pool on the Alibaba open source platform. It combines the advantages of C3P0, DBCP, Proxool and other DB pools, and at the same time adds log monitoring, which can monitor the execution of DB pool connections and SQL very well. It can be said that The DB connection pool for monitoring can be said to be one of the best connection pools at present.

public class TestDruid {
 public static void main(String[] args) throws Exception {
Properties pro = new Properties(); 

pro.load(TestDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
 DataSource ds = DruidDataSourceFactory.createDataSource(pro);
 Connection conn = ds.getConnection();
 System.out.println(conn);
 }
}

 

url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true

username=root
password=123456

driverClassName=com.mysql.jdbc.Driver
initialSize=10

maxActive=20

maxWait=1000

filters=wall

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/127470489