当前位置:网站首页>Database connection pool & Druid usage
Database connection pool & Druid usage
2022-07-27 05:19:00 【New an object_】
Introduction to database connection pool
● The database connection pool is a container , To be responsible for the distribution of 、 Manage database connections
● It allows applications to reuse an existing database connection , Instead of building a new
● Release database connection with idle time exceeding the maximum idle time to avoid missing database connection caused by no free database connection
● benefits :
* Resource reuse
* Improve system response speed
* Avoid missing database connections
Database connection pool implementation
● Standard interface :DataSource
* official (SUN) Database connection pool standard interface provided , This interface is implemented by a third-party organization .
* function : Get the connection 
● Common database connection pool :
*DBCP
*C3P0
*Druid
●Druid( Druid )
*Druid Connection pool is an open source database connection pool project of Alibaba
* Powerful , Excellent performance , yes Java One of the best database connection pools in language
Driud Use steps
1、 Import jar package druid-1.1.12.jar
Database connection pool druid Of jar Download from Bao official website : https://mvnrepository.com/artifact/com.alibaba/druid
* Put it jar Import the package into the current module directory and set the current module to be valid 
2、 Define configuration file
* Properties of a profile : https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8
(druid.properties The configuration file )
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/byshop?serverTimezone=PRC
username=root
password=123456
// Initialize the number of connections ( Initial application in container 5 individual )
initialSize=5
// Minimum number of connection pools
minIdle=1
// maximum connection
maxActive=10
// Maximum waiting time (10 Second ) An error will be reported after timeout
maxWait=10000
timeBetweenEvictionRunsMillis=6000
// The minimum time that a connection remains idle without being evicted
minEvictableIdleTimeMillis=300000
testWhileIdle=true
// Used to check whether the connection is valid SQL, The requirement is a query statement , Commonly used select 'x'. If validationQuery by null,testOnBorrow、testOnReturn、testWhileIdle It doesn't work .
validationQuery=select 1
3、 Load profile
4、 Get database connection pool object
5、 Get the connection
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;
public class Druid {
public static void main(String[] args) throws Exception {
// Load profile ——Properties Object load configuration file
Properties prop=new Properties();
prop.load(new FileInputStream("src/druid.properties"));
// Get connection pool object
DataSource dataSource=DruidDataSourceFactory.createDataSource(prop);
// Get the corresponding database connection
Connection conn=dataSource.getConnection();
System.out.println(conn);
}
}
result :(JDBC A connection object of )
边栏推荐
- pyside2____1.安装和案列
- 牛客剑指offer--JZ12 矩阵中的路径
- Read write separation and master-slave synchronization
- Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill
- SSM framework integration
- Typescript details
- Raspberry pie RTMP streaming local camera image
- Introduction to dynamic memory functions (malloc free calloc realloc)
- File processing (IO)
- 34. Analyze flexible.js
猜你喜欢

JVM Part 1: memory and garbage collection part 3 - runtime data area - overview and threads

Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill

Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”

探寻通用奥特能平台安全、智能、性能的奥秘!

Detailed description of binary search tree

Li Kou achieved the second largest result

支付流程如何测试?

String class
随机推荐
文件处理(IO)
Alphabetic order problem
The interface can automatically generate E and other asynchronous access or restart,
Why is count (*) slow
C中文件I/O的使用
Standard dialog qmessagebox
Another skill is to earn 30000 yuan a month+
二、MySQL高级
MySQL storage engine and its differences
Counting Nodes in a Binary Search Tree
JVM上篇:内存与垃圾回收篇八--运行时数据区-方法区
String class
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
简化JDBC的MyBits框架
求组合数(最强优化)
JVM上篇:内存与垃圾回收篇十四--垃圾回收器
牛客剑指offer--JZ12 矩阵中的路径
B1021 个位数统计
Deep Qt5 signal slot new syntax
JVM上篇:内存与垃圾回收篇--运行时数据区四-程序计数器