数据库连接池_DataSource_数据源(简单介绍C3P0和Druid)
文章目錄
- 概念
- 好處
- 實現(xiàn)
- C3P0
- 基本使用
- 配置文件
- druid
- 下載 jar 包
- 配置文件
- 演示代碼
- 定義工具類
- 示例代碼
概念
數(shù)據(jù)庫連接池其實就是一個容器,而這個容器其實就是一個集合,這個集合存放著多個數(shù)據(jù)庫連接對象。
系統(tǒng)在初始化的時候,將創(chuàng)建一定數(shù)量的數(shù)據(jù)庫連接放到連接池中。當(dāng)應(yīng)用程序要訪問數(shù)據(jù)庫的時候,會向連接池請求連接對象,訪問結(jié)束后會將連接對象還給連接池。當(dāng)應(yīng)用程序向連接池請求的連接數(shù)超過最大連接數(shù)量時,這些請求將被加入到等待隊列中。
數(shù)據(jù)庫連接池也叫數(shù)據(jù)源,英文名叫 DataSource。
比方說創(chuàng)建連接池對象后,會創(chuàng)建 30 個連接對象放入連接池(連接池是一個集合)中,那么請求過來要和數(shù)據(jù)庫取得連接,就只能從連接池獲取連接對象和數(shù)據(jù)庫通訊,用完后再放回連接池中,也就是說所有的用戶請求共享池中30個連接對象,可以同時保持30個連接,如果連接池沒有對象了,其它的連接請求只能處于等待中直到池中有連接對象為止。
好處
節(jié)約資源、數(shù)據(jù)庫訪問高效
以前每個請求就創(chuàng)建一個連接對象,那么100 萬個請求就創(chuàng)建 100 萬個連接對象,數(shù)據(jù)庫可能無法承受;另外如果很多連接對象創(chuàng)建完后不關(guān)閉,那么就會嚴(yán)重占用服務(wù)器內(nèi)存,當(dāng)請求數(shù)量急劇上升,數(shù)據(jù)庫就會崩潰掉。
因為這樣的方式創(chuàng)建的連接對象是無法提供給其它請求使用,沒有關(guān)閉就閑置在內(nèi)存中,即便同一個用戶發(fā)送相同的請求還是會創(chuàng)建一個新的連接對象,造成嚴(yán)重的資源浪費。另外頻繁創(chuàng)建新的連接對象會耗費沒有必要的時間和資源!
實現(xiàn)
標(biāo)準(zhǔn)接口:DataSource,在 javax. sql 包下的。
提供的方法:
1.獲取連接:getConnection()
2.歸還連接:close()。如果連接對象 Connection 是從連接池中獲取的,那么調(diào)用 close() 方法則不會再關(guān)閉連接了。而是歸還連接
一般我們不去實現(xiàn)它,由數(shù)據(jù)庫廠商來實現(xiàn)。
1.C3P0:數(shù)據(jù)庫連接池實現(xiàn)技術(shù)
2.Druid:數(shù)據(jù)庫連接池實現(xiàn)技術(shù),由阿里巴巴提供的,全球最好的連接池技術(shù)之一
C3P0
使用步驟:
1.導(dǎo)入jar包(兩個) c3p0-0.9.5.2.jar 和 mchange - commons -java-0.2.12.jar,注意別忘了導(dǎo)入數(shù)據(jù)庫驅(qū)動包
2.定義配置文件:
名稱: c3p0.properties 或者 c3p0- config.xml
路徑:將文件放在src目錄下即可。
3.創(chuàng)建數(shù)據(jù)庫連接池對象 ComboPool edDataSource
4.獲取連接:getConnection
基本使用
public static void main(String[] args) {// 創(chuàng)建連接池對象DataSource ds = new ComboPooledDataSource();// 獲取連接對象try {Connection conn = ds.getConnection();System.out.println(conn);} catch (SQLException e) {e.printStackTrace();}}配置文件
<?xml version="1.0" encoding="utf-8"?> <c3p0-config><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password">123</property><!--連接池初始化的時候創(chuàng)建5個連接對象--><property name="initialPoolSize">5</property><!--連接池中最多存放10個連接對象--><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property></default-config><!--其它連接配置--><named-config name="otherc3p0"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password">lwx83129LWX@!</property><property name="initialPoolSize">5</property><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property></named-config> </c3p0-config><named-config> 可以配置其它數(shù)據(jù)庫連接信息,那么在 Java 代碼中,可以指定名稱來獲取對應(yīng)的數(shù)據(jù)庫連接配置信息創(chuàng)建連接池對象,從而達(dá)到動態(tài)切換數(shù)據(jù)庫的目的。
// 創(chuàng)建連接池對象 DataSource ds = new ComboPooledDataSource("otherc3p0");druid
數(shù)據(jù)庫連接池實現(xiàn)技術(shù),由阿里巴巴提供的
步驟:
1.導(dǎo)入 jar 包 druid-1.2.8.jar
2.定義配置文件:是 properties 形式的??梢越腥我饷Q,可以放在任意目錄下,需要手動指定文件路徑
3.加載配置文件
4.獲取數(shù)據(jù)庫連接池對緣:通過工廠類 DruidDataSourceFactory 來獲取
5.獲取連接對象:getConnection
下載 jar 包
下載地址:https://repo1.maven.org/maven2/com/alibaba/druid/
配置文件
# driverClassName=com.mysql.jdbc.Driver 這個驅(qū)動類已經(jīng)過時了 # driverClassName=com.mysql.cj.jdbc.Driver 這個驅(qū)動類是最新的,但是驅(qū)動程序通過SPI自動注冊,通常不需要手動加載驅(qū)動程序類。 # 如果是數(shù)據(jù)庫在本地,且端口號是3306,則可以簡寫成:jdbc:mysql:///test url=jdbc:mysql://localhost:3306/test username=root password=123# 連接池初始化時會生成5個連接對象 initialSize=5 # 連接池中最多10個連接對象,已經(jīng)達(dá)到10個連接對象了,若繼續(xù)向連接池申請連接對象會報錯 maxActive=10# 等待時間,向連接池申請連接對象時,最多等待3秒種,3秒內(nèi)沒有獲取到連接對象會報錯 maxWait=3000演示代碼
public static void main(String[] args) {// 導(dǎo)入jar包// 定義配置文件// 加載配置文件Properties prop = new Properties();Class<DruidDemo01> dd = DruidDemo01.class;InputStream is = dd.getClassLoader().getResourceAsStream("druid.properties");try {prop.load(is);// 創(chuàng)建連接池對象DataSource ds = DruidDataSourceFactory.createDataSource(prop);Connection conn = ds.getConnection();System.out.println(conn);} catch (Exception e) {e.printStackTrace();}}定義工具類
1.定義一個類 JDBCUtils
2.提供靜態(tài)代碼塊加載配置文件,初始化連接池對象
3.提供方法
3.1.獲取連接對象方法:通過數(shù)據(jù)庫連接池獲取連接對象
3.2.釋放資源,歸還連接對象
3.3.獲取連接池對象的方法
示例代碼
package priv.lwx.javaex.datasource.util;import com.alibaba.druid.pool.DruidDataSourceFactory;import javax.sql.DataSource; import java.io.InputStream; import java.sql.*; import java.util.Properties;/*** 使用druid連接池實現(xiàn)的JDBC工具類** @author liaowenxiong* @date 2021/11/18 15:30*/public class JDBCUtils {// 聲明靜態(tài)變量private static DataSource ds;// 通過靜態(tài)代碼塊初始化連接池static {// 1.加載配置文件InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");Properties prop = new Properties();try {prop.load(is);// 2.獲取連接池對象ds = DruidDataSourceFactory.createDataSource(prop);} catch (Exception e) {e.printStackTrace();}}/*** 獲取連接對象** @return java.sql.Connection* @author liaowenxiong* @date 2021/11/18 15:40*/public static Connection getConnection() throws SQLException {return ds.getConnection();}/*** 關(guān)閉語句對象和連接對象** @param stmt 被關(guān)閉的語句對象* @param conn 被關(guān)閉的連接對象* @return void* @author liaowenxiong* @date 2021/11/16 17:57*/public static void close(Statement stmt, Connection conn) {if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/*** 關(guān)閉語句對象,釋放語句對象資源** @param stmt 關(guān)閉的語句對象* @return 無* @author liaowenxiong* @date 2021/11/17 18:08*/public static void close(Statement stmt) {if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}}public static void close(ResultSet rs, Statement stmt, Connection conn) {if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/*** 釋放一個連接對象,多個語句對象資源** @param conn 釋放的連接對象* @param stmts 釋放的多個語句對象* @return 無* @author liaowenxiong* @date 2021/11/17 18:09*/public static void close(Connection conn, Statement... stmts) {if (stmts != null && stmts.length != 0) {for (int i = 0; i < stmts.length; i++) {Statement stmt = stmts[i];close(stmt);}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/*** 獲取連接池** @return javax.sql.DataSource* @author liaowenxiong* @date 2021/11/18 15:53*/public static DataSource getDataSource() {return ds;}public static void main(String[] args) throws SQLException {} }總結(jié)
以上是生活随笔為你收集整理的数据库连接池_DataSource_数据源(简单介绍C3P0和Druid)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5999元起!iPhone 15系列售价
- 下一篇: 使用JDBC连接数据库时,SQL语句中提