当前位置:网站首页>Usage and usage instructions of JDBC connection pool
Usage and usage instructions of JDBC connection pool
2022-07-05 15:17:00 【Add gold development】
We all know through jdbc Link to Will consume a lot of connections , Every connection consumes resources , close 》 Opening it will consume resources , Build up... Frequently 、 Close the connection , It will greatly reduce the performance of the system , Because the use of connections becomes the bottleneck of system performance .
Advantages brought by database connection pool technology :
1. Resource reuse
2. Faster system response time
3. New means of resource allocation
4. Unified connection management , Avoid database connection leaks
Principle diagram
Using process :
jdbc.properties
DriverClass = com.mysql.jdbc.Driver
JdbcUrl = jdbc\:mysql\://ID\:3306/htmldata?useUnicode\=true&characterEncoding\=UTF-8
User = root
Password = 123
MaxPoolSize = 20
MinPoolSize = 2
InitialPoolSize = 5
MaxStatements = 30
MaxIdleTime =100
C3P0Mysql.java
package class_name;
import java.sql.Connection;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Mysql {
private ComboPooledDataSource cpds;
private static C3P0Mysql c3P0Properties;
static{
c3P0Properties = new C3P0Mysql();
}
public C3P0Mysql() {
try {
cpds = new ComboPooledDataSource();
// Load profile
Properties props = new Properties();
props.load(C3P0Mysql.class.getClassLoader().getResourceAsStream("jdbc.properties"));
cpds.setDriverClass(props.getProperty("DriverClass"));
cpds.setJdbcUrl(props.getProperty("JdbcUrl"));
cpds.setUser(props.getProperty("User"));
cpds.setPassword(props.getProperty("Password"));
cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
cpds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
cpds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
cpds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
cpds.setMaxIdleTime(Integer.parseInt(props.getProperty("MaxIdleTime")));
} catch (Exception e) {
e.printStackTrace();
}
}
public static C3P0Mysql getInstance(){
return c3P0Properties;
}
public Connection getConnection(){
Connection conn = null;
try {
conn = cpds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connection = C3P0Mysql.c3P0Properties.getConnection();
System.out.println(" Connected successfully ");
}
}
边栏推荐
- CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
- Long list optimized virtual scrolling
- Ctfshow web entry command execution
- 30岁汇源,要换新主人了
- Can I pass the PMP Exam in 20 days?
- Magic methods and usage in PHP (PHP interview theory questions)
- GPS original coordinates to Baidu map coordinates (pure C code)
- Using tensorboard to visualize the training process in pytoch
- 1330: [example 8.3] minimum steps
- Bugku's Eval
猜你喜欢
CODING DevSecOps 助力金融企业跑出数字加速度
Bugku's eyes are not real
Talking about how dataset and dataloader call when loading data__ getitem__ () function
Fr exercise topic - simple question
P1451 calculate the number of cells / 1329: [example 8.2] cells
Bugku telnet
Mysql---- function
华为哈勃化身硬科技IPO收割机
Detailed explanation of QT creator breakpoint debugger
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
随机推荐
JMeter performance test: serveragent resource monitoring
sql server char nchar varchar和nvarchar的区别
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
【jvm】运算指令
【华为机试真题详解】欢乐的周末
Hongmeng system -- Analysis from the perspective of business
Can I pass the PMP Exam in 20 days?
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
ICML 2022 | 探索语言模型的最佳架构和训练方法
Aike AI frontier promotion (7.5)
Un week - end heureux
CODING DevSecOps 助力金融企业跑出数字加速度
Crud de MySQL
Fr exercise topic - simple question
漫画:优秀的程序员具备哪些属性?
Leetcode: Shortest Word Distance II
JS bright blind your eyes date selector
Thymeleaf uses background custom tool classes to process text