当前位置:网站首页>Database connection pool and dbutils tool
Database connection pool and dbutils tool
2022-06-12 07:56:00 【Watermelon 77】
Database connection pool
What is a database connection pool :
In the use of JDBC When programming , Each time you access the database, you need to create an object , After the access is completed, you also need to disconnect ( Destroy object ). To avoid frequent creation of database connections , Put forward the database connection pool technology .
The application does not need to create and disconnect objects , Instead, it is left to the database connection pool management .
Database connection pool disconnected , Not destroy objects , Instead, return the connection object to the connection pool .
The database connection pool is responsible for allocation 、 Manage and release database connections , It allows applications to reuse existing database connections , Instead of re establishing .
Schematic diagram of using database connection pool to operate database :
advantage : Manage connection objects through database connection pool , Reduce the creation and destruction of database connection objects , Improved database access efficiency .
Basic parameters of database connection pool :
1. Initialization size : The number of initial connection objects in the database connection pool
2. maximum connection : The maximum number of connection objects in the database connection pool
3. Maximum waiting time : When the user fetches the connection object through the connection pool , The world that needs to wait
4. Maximum free connection : Database connection pool , When user access tends to stabilize , Maximum number of idle connections
5. Minimum free connection : At the peak of user access , The minimum number of remaining connection objects in the database connection pool .
DataSource Interface
In order to get the database connection object (Connection),JDBC Provides javax.sql.DataSource Interface , It is responsible for establishing a connection with the database , And defines the return value Connection Object method :
Connection getConnection()
Connection getConnection(String username,String password)
Habitually realized javax.sql.DataSource The class of an interface is called a data source , That is, the source of the data . All the information of establishing database connection is stored in the data source .
DBCP data source
DBCP Data source connection pool (DataBase Connection Pool), By apache Database connection pool technology provided by the organization , Free open source .
In project development , Use DBCP Basic steps :
Premise import DBCP Of jar package :commons-dbcp.jar,commons-pool.jar
DBCP Get the connection object : Set connection parameters . Four connection parameters :driverName,url,username,password
package cn.itcast.chapter10.example;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class Example01 {
public static DataSource ds = null;
static {
// obtain DBCP The data source implements class objects
BasicDataSource bds = new BasicDataSource();
// Set the four connection parameters required to connect to the database
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jbdc:mysql://localhost:3306/jdbc");
bds.setUsername("root");
bds.setPassword(null);
// Set connection pool parameters
bds.setInitialSize(5); // Initialize parameters
bds.setMaxIdle(10);
ds = bds;
}
public static void main(String[] args) throws SQLException {
// Database connection pool gets connection object
Connection con = ds.getConnection();
// Get connection parameters
DatabaseMetaData metaData = con.getMetaData();
System.out.println(metaData.getURL());
System.out.println(metaData.getUserName());
System.out.println(metaData.getDriverName());
}
}
BasicDataSource Common methods of class :
C3P0 data source
C3P0 Data source is one of the most popular database connection pool technologies , Its performance is more superior , It also provides a framework for later data hibernate Good support , It's open source and free .
In project development , Use C3P0 Basic steps :
Premise import C3P0 Of jar package , Provides C3P0 Core class methods for
Set connection parameters : Four connection parameters :driverName,url,username,password
package cn.itcast.chapter10.example;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class Example02 {
public static DataSource ds = null;
// initialization C3P0 data source
static {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
// Set four connection parameters
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc");
cpds.setUser("root");
cpds.setPassword("");
// Set connection pool parameters
cpds.setInitialPoolSize(5);
cpds.setMaxPoolSize(15);
ds = cpds;
} catch (PropertyVetoException e) {
// TODO Automatically generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
System.out.println(ds.getConnection());
}
}
ComboPooledDataSource Class :
DBUtils Tools
DBUtils Tool is introduced : By apache Provide . yes commons Component one . It's right jdbc Simple encapsulation , Reduce the code for operating the database . Before using , Import required DBUtils Of jar package .
QueryRunner class
QueryRunner Class is DBUtils Core classes under components , Usually and ResultSetHandler Interface with , Yes jdbc The code is simply encapsulated , Reduce the large amount of code for operating the database .
ResultSetHandler Interface
ResultSetHandler The interface is used to handle ResultSet Result set , It can convert the data in the result set into different forms .
ResultSetHandler Provides a set of common implementation classes , As follows :
边栏推荐
- 最新hbuilderX编辑uni-app项目运行于夜神模拟器
- LeetCode笔记:Weekly Contest 296
- 2021.10.27-28 scientific research log
- Architecture and performance analysis of convolutional neural network
- 2021.10.24-25 scientific research log
- qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
- Voice assistant - those classification models used in the assistant
- 20220525 RCNN--->Faster RCNN
- Voice assistant - Qu - ner and intention slot model
- Utilize user tag data
猜你喜欢

System service configuration service - detailed version

Voice assistant - Introduction and interaction process

Symfony 2: multiple and dynamic database connections

Some summaries of mathematical modeling competition in 2022

经典论文回顾:Palette-based Photo Recoloring

Voice assistant - potential skills and uncalled call technique mining

Generalized semantic recognition based on semantic similarity

Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy

Voice assistant -- Architecture and design of Instruction Assistant

tar之多线程解压缩
随机推荐
Voice assistant - DM - distribution and sorting
Pytorch installation (GPU) in Anaconda (step on pit + fill pit)
10 lessons from the recommended system
移动端、安卓、IOS兼容性面试题
Rnorm function of R language generates positive distribution data, calculates descriptive statistical summary information of vector data using sum function of epidisplay package, and visualizes ordere
vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)
Voice assistant - Multi round conversation (process implementation)
Topic 1 Single_Cell_analysis(3)
最新hbuilderX编辑uni-app项目运行于夜神模拟器
Voice assistant - potential skills and uncalled call technique mining
In depth learning, the parameter quantity (param) in the network is calculated. The appendix contains links to floating point computations (flops).
20220524 深度学习技术点
Voice assistant - future trends
Conda创建虚拟环境报错,问题解决
R语言使用caTools包的sample.split函数将机器学习数据集划分为训练集和测试集
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
Arrangement of statistical learning knowledge points -- maximum likelihood estimation (MLE) and maximum a posteriori probability (map)
20220524 deep learning technology points
20220607. face recognition
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water