What is a database connection pool?

In Java, database access technologies can be divided into the following categories:

  • JDBC directly access the database
  • JDO technology
  • Third-party O/R tools, such as Hibernate, ibatis, mybatis, etc.

JDBC is the cornerstone of java to access the database, JDO, Hibernate, etc. just better encapsulate JDBC.
JDBC (Java Database Connectivity) is a public interface (a set of APIs) that is independent of a specific database management system and a general SQL database access and operation . It defines a standard Java class library used to access the database, (java.sql, javax .sql) Use this library to easily access database resources in a standard way. JDBC provides a unified way to access different databases , shielding some detailed problems for developers. The goal of JDBC is to enable Java programmers to use JDBC to connect to any database system that provides a JDBC driver, so that programmers do not need to know too much about the characteristics of a particular database system, which greatly simplifies and speeds up the development process.
Insert picture description here
Of course, you can use the drivers of different database vendors without using a unified JDBC interface, but this will not reflect the portability of Java.
JDBC is a set of interfaces provided by sun company for database operations. Java programmers only need to program for this set of interfaces. Different database vendors need to provide different implementations for this set of interfaces. The collection of different implementations is the driver of different databases. -----Interface-oriented programming

That is,
JDBC: a set of interface
JDBC drivers for database operations : different implementations need to be provided for this set of interfaces. The collection of different implementations is the driver of different databases. For example, the use of mysql requires a mysql-driven jar package

Database connection pool c3p0, dhcp

When using and developing database-based web programs, the traditional mode (JDBC) basically follows the following steps:

  1. Establish a database connection in the main program (such as servlet, beans)
  2. Perform sql operations
  3. Disconnect the database connection

However, there are problems with this mode of development:
ordinary JDBC database connections are obtained using DriverManager. Each 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~1s) time). When a database connection is needed, one is requested from the database, and the connection is disconnected after the execution is complete. This way will consume a lot of resources and time. Database connection resources have not been 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 may even cause server crashes.
For each database connection, it must be disconnected after use. Otherwise, if the program fails to shut down due to an abnormality, it will cause a memory leak in the database system and eventually cause the database to be restarted.
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.
In order to solve the database connection problem in traditional development, database connection pool technology can be used.
The basic idea of ​​database connection pool is to establish a "buffer pool" for database connections. Put a certain number of connections in the buffer pool in advance. When you need to establish a database connection, 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. It allows applications to reuse an existing database connection instead of re-establishing one.
When the database connection pool is initialized, a certain number of database connections are 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, the connection pool will always ensure that there are at least this number of connections. The maximum number of database connections in the connection pool limits the maximum number of connections that this 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.

The database connection pool of JDBC is represented by javax.sql.DataSource. DataSource is just an interface. The interface is usually implemented by the server (Weblogic, WebSphere, Tomcat). There are also some open source organizations that provide implementation:

  • DBCP database connection pool
  • C3P0 database connection pool

DataSource is usually called a data source, which includes two parts: connection pool and connection pool management. It is customary to call DataSource as connection pool.
DataSource is used to replace DriverManager to obtain Connection, which is fast and can greatly improve database access. speed.
DHCP data source

DBCP is an open source connection pool implementation under the Apache Software Foundation. This connection pool relies on another open source system under this organization: Common-pool. If you want to use this connection pool implementation, you should add the following two jar files to the system:
Commons -dbcp.jar: the realization of the connection pool
Commons-pool.jar: the connection pool of the dependent library
Tomcat realized by the connection pool is realized by this connection pool. The database connection pool can be used in combination with the application server, or can be used independently by the application.

The data source is different from the database connection. There is no need to create multiple data sources. It is the factory that generates the database connection, 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 the above code does not close the physical connection to the database, it just releases the database connection and returns it to the database connection pool.

There are two ways to create database connections for the two connection pools

public class DataSourceTest {
    
    
	
	//使用 C3P0 方式二:
	@Test
	public void test4() throws SQLException{
    
    
		DataSource ds = new ComboPooledDataSource("helloc3p0");
		
		Connection conn = ds.getConnection();
		
		System.out.println(conn);
	}
	
	//使用 C3P0 方式一:
	@Test
	public void test3() throws Exception{
    
    
		ComboPooledDataSource cpds = new ComboPooledDataSource();
		cpds.setDriverClass("com.mysql.jdbc.Driver");
		cpds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test");
		cpds.setUser("root");
		cpds.setPassword("123456");
	 
		Connection conn = cpds.getConnection();
		
		System.out.println(conn);
	}
	
	//使用 DBCP 方式二:
	@Test
	public void test2() throws Exception{
    
    
		Properties pros = new Properties();
		pros.load(DataSourceTest.class.getClassLoader().getResourceAsStream("dbcp.properties"));
		
		DataSource ds = BasicDataSourceFactory.createDataSource(pros);
		
		Connection conn = ds.getConnection();
		
		System.out.println(conn);
	}
	
	//使用 DBCP 方式一:
	@Test
	public void test1() throws SQLException{
    
    
		BasicDataSource bds = new BasicDataSource();
		bds.setDriverClassName("com.mysql.jdbc.Driver");
		bds.setUrl("jdbc:mysql://127.0.0.1:3306/test");
		bds.setUsername("root");
		bds.setPassword("123456");
		
		bds.setInitialSize(10);
		bds.setMaxActive(10);
		
		Connection conn = bds.getConnection();
		
		System.out.println(conn);
		
		//将连接放回到连接池中
		conn.close();
	}
 
}

Using the c3p0 method of the second method requires a configuration file (of course you can also use a property file), and the name of the configuration file must be c3p0-config.xml, because the bottom layer of the database manufacturer encapsulates data based on key-value, so it will be based on this key To perform matching reading, it must be placed under the path of src, because reading is performed by way of a class loader. The use of dhcp in the second method requires a property file which is roughly the same as the property file of jdbc, so I won’t go into details.

c3p0-config.xml

<c3p0-config>
  <named-config name="helloc3p0"> 
  
  	<!-- 连接数据库的四个字符串 -->
  	<property name="driverClass">com.mysql.jdbc.Driver</property>
  	<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/test</property>
  	<property name="user">root</property>
  	<property name="password">123456</property>
  
  	<!-- 若连接池满了一次增长几个 -->
    <property name="acquireIncrement">5</property>
    
    <!-- 连接池初始大小 -->
    <property name="initialPoolSize">10</property>
    
    <!-- 连接池中最小连接数 -->
    <property name="minPoolSize">5</property>
    
    <!-- 连接池中最大连接数 -->
    <property name="maxPoolSize">10</property>
 
	<!-- 整个连接池中最多管理的 Statement 的个数 -->
    <property name="maxStatements">10</property> 
    
    <!-- 连接池中每个连接最多管理的 Statement 的个数 -->
    <property name="maxStatementsPerConnection">2</property>
 
  </named-config>
</c3p0-config>

Note: The name of the configuration file must be consistent with the parameters to be passed in DataSource ds = new ComboPooledDataSource("helloc3p0");

Guess you like

Origin blog.csdn.net/WA_MC/article/details/113526437