MySql中的數(shù)據(jù)庫連接池詳解
MySql數(shù)據(jù)庫連接池
1、概念
JDBC數(shù)據(jù)連接池:在做開發(fā)是不會(huì)單獨(dú)寫一個(gè)連接,都是使用數(shù)據(jù)庫連接池。
2、為什么會(huì)出現(xiàn)數(shù)據(jù)庫連接池
一個(gè)項(xiàng)目中,會(huì)有很多的用戶訪問,如果是和之前一樣單次連接,那么每次連接數(shù)據(jù)庫都要?jiǎng)?chuàng)建數(shù)據(jù)庫連接對(duì)象,來n個(gè)用戶九創(chuàng)建n個(gè),這樣的高并發(fā),服務(wù)器受不了。而且用完后關(guān)閉連接,浪費(fèi)資源,如果在關(guān)閉的時(shí)候出現(xiàn)異常未能關(guān)閉連接,就會(huì)出現(xiàn)內(nèi)存泄漏(對(duì)象無法回收)
如果沒有池化技術(shù),就相當(dāng)于銀行開門,然后業(yè)務(wù)員服務(wù)你一個(gè)人,然后關(guān)門,下個(gè)人來了再開門,這樣很浪費(fèi)資源
使用了數(shù)據(jù)庫連接池之后,我們?cè)陂_發(fā)中就不需要寫連接數(shù)據(jù)庫代碼了
3、原理
- 和線程池類似,規(guī)定了最大的承載量,比如有留出了5個(gè)連接對(duì)象,那么第六個(gè)人就需要排隊(duì)
- 如果使用完畢去關(guān)閉數(shù)據(jù)庫連接對(duì)象,不會(huì)真的關(guān)閉,只是被連接池回收,然后給排隊(duì)的下一個(gè)人使用
- 相當(dāng)于銀行開門,開門以后業(yè)務(wù)員服務(wù)客戶,沒有客戶的時(shí)候就等待,然后到點(diǎn)關(guān)門,業(yè)務(wù)員不再工作
4、數(shù)據(jù)庫連接池的提供商
數(shù)據(jù)庫連接池的有很多,比較熱門的有:
- DBCP
- 是tomcat自帶的,相對(duì)于C3P0來說速率較快,但是不穩(wěn)定
- C3P0
- 速率比較慢,但是非常穩(wěn)定
- Druid(德魯伊)
- 是阿里提供,最常用的,它結(jié)合了DBCP和C3P0各自的優(yōu)點(diǎn)
5、DataSource數(shù)據(jù)源
- 實(shí)現(xiàn)接口DataSource就可以編寫數(shù)據(jù)源
- 通過DataSource替換了DriverManager,相當(dāng)于在各個(gè)數(shù)據(jù)庫廠商提供的驅(qū)動(dòng)的基礎(chǔ)上,再次進(jìn)行包裝
6、DBCP
- 導(dǎo)入jar包
- java中使用
public class DBCPTest { private static DataSource dataSource = null; public static void DbcpTest() { try { // 讀取文件配置 InputStream config = DBCPTest.class.getClassLoader().getResourceAsStream("resources/config.properties"); Properties prop = new Properties(); prop.load(config); // 創(chuàng)建數(shù)據(jù)源 工廠模式 dataSource = BasicDataSourceFactory.createDataSource(prop); // 從數(shù)據(jù)源中獲取連接 Connection connection = dataSource.getConnection(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
- 配置文件
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true username=root password=19981104
7、C3P0
- 導(dǎo)入jar包
- java中使用
public static void main(String[] args) throws PropertyVetoException, SQLException { // 實(shí)例化C3P0提供的連接池 ComboPooledDataSource cpds = new ComboPooledDataSource(); // 加載當(dāng)前使用的數(shù)據(jù)庫 cpds.setDriverClass("com.mysql.cj.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true"); cpds.setUser("root"); cpds.setPassword("19981104"); // 通過Datasource數(shù)據(jù)源獲得連接對(duì)象 Connection connection = cpds.getConnection(); // 設(shè)置初始化連接池中的連接對(duì)象 cpds.setInitialPoolSize(2); // 也可以通過加載配置文件使用數(shù)據(jù)庫 // 在實(shí)例化時(shí),去加載配置文件 // 這里的配置文件名是xml中的named-config的name ComboPooledDataSource cpds2 = new ComboPooledDataSource("intergalactoApp"); Connection connection2 = cpds2.getConnection(); System.out.println(connection2); }
配置XMl
- XML是一個(gè)文本標(biāo)記語言,就是使用標(biāo)簽對(duì)組成的語言,進(jìn)行記錄文本信息
- XML文件主要的作用就是標(biāo)記存儲(chǔ)內(nèi)容的
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 在這下面配置數(shù)據(jù)庫信息 --> <named-config name="intergalactoApp"> <!-- 配置驅(qū)動(dòng),url,user和password --> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/school?useSSL=true</property> <property name="user">root</property> <property name="password">password</property> <property name="acquireIncrement">50</property> <property name="initialPoolSize">100</property> <property name="minPoolSize">50</property> <property name="maxPoolSize">1000</property> <!-- intergalactoApp adopts a different approach to configuring statement caching --> <property name="maxStatements">0</property> <property name="maxStatementsPerConnection">5</property> </named-config> </c3p0-config>
8、Druid(德魯伊)
- 導(dǎo)入jar包
- java中使用
public static void main(String[] agrs) throws Exception { DruidDataSource dataSource = new DruidDataSource(); // 不管是誰想要連接數(shù)據(jù)庫服務(wù)器,都需要用戶名,密碼,url,driver // dataSource.setDriverClassName(driverClass); // dataSource.setUrl(jdbcUrl); // 讀取配置文件 InputStream config = DruidTest.class.getClassLoader().getResourceAsStream("resources/config.properties"); Properties prop = new Properties(); prop.load(config); // 使用工廠模式 -- 提供了生產(chǎn)數(shù)據(jù)源對(duì)象的工廠 // 讀取Druid讀取配置文件 DataSource dataSource2 = DruidDataSourceFactory.createDataSource(prop); // 獲得連接對(duì)象 Connection connection = dataSource2.getConnection(); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL 5.5/5.6/5.7及以上版本安裝包安裝時(shí)如何選擇安裝路徑
最近mysql官方網(wǎng)站的安裝包從5.5-5.7起都是新版的安裝界面,各種環(huán)境要求支持,看樣子以后老點(diǎn)的系統(tǒng)安裝都?jí)蛸M(fèi)勁的了,這里腳本之家小編特為大家整理一下安裝步驟與方法2016-04-04解決Mysql主從錯(cuò)誤:could not find first log&nbs
這篇文章主要介紹了解決Mysql主從錯(cuò)誤:could not find first log file name in binary問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12MySQL數(shù)據(jù)庫表的合并及分區(qū)方式
這篇文章主要介紹了MySQL數(shù)據(jù)庫表的合并及分區(qū)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08mysql實(shí)現(xiàn)將date字段默認(rèn)值設(shè)置為CURRENT_DATE
這篇文章主要介紹了mysql實(shí)現(xiàn)將date字段默認(rèn)值設(shè)置為CURRENT_DATE問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07