Use connection pool to manage JDBC connections-DBCP data source and C3P0 data source

I wrote earlier how Java connects to a database simply, and obtains a connection through DriverManager, but it is rarely used in practice, because of low efficiency, the use of connection pools can greatly improve program performance

First, the principle of the database connection pool

When using a database, you need to connect to the database first. The original connection method is to obtain a connection through DriverManager . A database connection object corresponds to a physical database connection . Each time the database is operated, a physical connection will be opened, and then closed after use. Frequently Opening and closing will cause low system performance , as shown in the figure:

So how do we solve the inefficiency caused by frequent opening and closing of database connections when using the database?

The database connection pool is solved in this way:

when the application starts, the system actively establishes enough database connections and forms these connections into a pool . Each time it is used, there is no need to reopen the connection, and it is taken directly from the connection pool Some connections are used, they are used up and do not need to be closed , just return to the connection pool. Using the connection pool, the efficiency of the program has been greatly improved.

Second, the use of database connection pool

JDBC's database connection pool is represented by Javax.sql.DataSource. DataSource is only an interface . It is provided by commercial servers and other commonly used open source organizations (such as DBCP, C3P0). DataSource is usually called a data source , including Connection pool and connection pool management, but it is customary to refer to DataSource as connection pool

1. DBCP data source

The connection pool implementation needs to rely on two jar packages. For all the first steps, you need to add jar packages first :

Use the DBCP data source to obtain the database connection method:

	//创建数据源对象
	BasicDataSource bds = new BasicDataSource(); 
	//设置连接池所需的驱动
	bds.setDriverClassName("com.mysql.jdbc.Driver");
	//设置连接数据库的URL
	bds.setUrl("jdbc:mysql://localhost:3306/csdn");
	//设置连接数据库的用户名
	bds.setUsername("root");
	//设置连接数据库的密码
	bds.setPassword("root");
	//设置连接池的初始连接数
	bds.setInitialSize(5);
	//设置连接池最多有多少个活动的连接数
	bds.setMaxActive(20);
	//设置连接池中最多有多少空闲的连接
	bds.setMinIdle(2);

Note: The entire application only needs one data source , which means that the above code is only executed once

Optimization: You can set the above bds as a static member variable and initialize the data source object when the application starts. When thereis a need to obtain a database connection in the program, directly access the bds object and obtain a database connection

Then get the database connection through DataSource :

	//通过数据源获得数据库的连接
	Connection con = bds.getConnection();
	......
	//释放连接
	con.close();
2. C3P0 data source

Compared with the previous DBCP data source, C3P0 data source performance is better , it is recommended to use , because C3P0 can automatically clean up the Connection is no longer used , you can also automatically clean up Statement and ResultSet .

Similarly, if you use the C3P0 connection pool, you need to increase the jar package: c3p0-0.9.1.2.jar

Use the C3P0 data source to obtain the database connection method :

	//创建连接池实例
	ComboPooleDataSource cds = new ComboPooleDataSource();
	//设置连接池所需的驱动
	cds.setDriverClass("com.mysql.jdbc.Driver");
	//设置连接数据库的URL
	cds.setjdbcUrl("jdbc:mysql://localhost:3306/csdn");
	//设置连接数据库的用户名
	cds.setUser("root");
	//设置连接数据库的密码
	cds.setPassword("root");
	//设置连接池的初始连接数
	cds.setInitialPoolSize(20);
	//设置最大连接数
	cds.setMaxPoolSize(30);
	//设置最小连接数
	cds.setMinPoolSize(3);
	//设置连接池里的缓存 Statement 的最大数
	cds.setMaxStatement(150);

The way to create a C3P0 connection pool is basically similar to the previous method to create a DBCP connection pool. After obtaining the C3P0 connection pool, the same connection to the database is obtained :

	//获得数据库的连接
	Connection con = cds.getConnection();
	//同样的释放连接
	con.close();

Note: At this time, the release of the database connection , but did not close the database of the physical connection , only the connection release, releases the connection to the pool, other users may also use the connection.

Published 7 original articles · won 8 · visited 1268

Guess you like

Origin blog.csdn.net/tanghaixu/article/details/105622680