數(shù)據(jù)庫(kù)連接池(c3p0,druid)

2021-04-07_182251.jpg

數(shù)據(jù)庫(kù)連接池

  • 概念:其實(shí)就是一個(gè)容器(集合),存放數(shù)據(jù)庫(kù)連接的容器
    當(dāng)系統(tǒng)初始化好后,容器被創(chuàng)建,容器中會(huì)申請(qǐng)一些連接對(duì)象
    當(dāng)用戶來(lái)訪問(wèn)數(shù)據(jù)庫(kù)時(shí),從容器中獲取連接對(duì)象
    用戶訪問(wèn)完之后,會(huì)將連接對(duì)象歸還給容器。
  • 優(yōu)勢(shì):節(jié)約資源,用戶訪問(wèn)高效
  • 實(shí)現(xiàn)
    標(biāo)準(zhǔn)接口:DataSource javax.sql包下的方法
    獲取連接getConnection();
    歸還連接Connection.close()
    如果連接對(duì)象Connection是從連接池中獲取的,那么調(diào)用Connection.close()方法,則不會(huì)再關(guān)閉連接了。而是歸還連接

一般我們不去實(shí)現(xiàn)它,有數(shù)據(jù)庫(kù)廠商來(lái)實(shí)現(xiàn),例如
C3P0:數(shù)據(jù)庫(kù)連接池技術(shù)
Druid:數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)技術(shù),由阿里巴巴提供的

C3P0數(shù)據(jù)庫(kù)連接池技術(shù)

  • 步驟
    1.導(dǎo)入jar包c3p0-0.9.5.2.jar,mchange-commons-java-0.2.12
    2.定義配置文件:
    名稱一定為c3p0.propertiesc3p0-config.xml;
    路徑直接將文件放在src目錄下即可
    3.創(chuàng)建核心對(duì)象 數(shù)據(jù)庫(kù)連接池對(duì)象 ComboPooledDataSource
    4.獲取連接:getConnection
public class c3p0Demp2 {
    public static void main(String[] args) throws SQLException {
        //1.獲取DataSource,使用默認(rèn)配置
        DataSource ds = new ComboPooledDataSource();
        //2.獲取連接
        for(int i=1;i<=11;i++){
            //獲取連接池中符合的最大連接數(shù)的10個(gè)對(duì)象,過(guò)10等3s報(bào)錯(cuò)
            Connection conn = ds.getConnection();
            System.out.println(i+":"+conn);
            if(i==5){
                conn.close();//歸還連接到連接池中,第11個(gè)可以給其服務(wù)
            }
        }
        testNamedConfig();
    }

    //使用c3p0-config.xml配置文件中name是”otherc3p0“的數(shù)據(jù)庫(kù)連接池
    public static void testNamedConfig() throws SQLException {
        //1.1獲取DataSource,使用指定名稱配置
        DataSource ds = new ComboPooledDataSource("otherc3p0");
        //2.獲取連接
        for(int i=1;i<=10;i++){//此時(shí)10個(gè)以超出otherc3p0限制的8個(gè)
            Connection conn = ds.getConnection();
            System.out.println(i+":"+conn);
        }
    }
}

Druid數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)技術(shù)

  • 基本使用步驟
    1.導(dǎo)入jar包druid-1.0.9.jar
    2.定義配置文件:
    名稱一定為.properties形式的,可以叫任意名稱
    文件可以放在任意目錄下
    3.加載配置文件,properties方式
    4.獲取數(shù)據(jù)庫(kù)連接池對(duì)象:通過(guò)工廠類來(lái)獲取 DruidDataSourceFactory
    5.獲取連接:getConnection
//1.導(dǎo)入jar包
        //2.定義配置文件
        //3.加載配置文件
        Properties pro = new Properties();
        InputStream is = DriuidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);
        //4.獲取連接池對(duì)象
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        //5.獲取連接
        Connection conn = ds.getConnection();
        System.out.println(conn);
  • 定義工具類
    1.定義一個(gè)類JDBCUtils
    2.通過(guò)靜態(tài)代碼塊加載配置文件,初始化連接池對(duì)象
    3.提供3種方法
    獲取連接方法:通過(guò)數(shù)據(jù)庫(kù)連接池獲取連接
    釋放資源
    獲取連接池的方法
/*
Druid連接池工具類
 */
public class JDBCUtils {
    //1.定義成員變量 DataSource
    private static DataSource ds;
    static{
        try {
            //1.加載配置文件
            Properties pro=new Properties();
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);
            //2.獲取DataSource
            ds= DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
    獲取連接
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
    /*
    釋放資源
     */
    public static void close(Statement stmt, Connection conn){
        /*if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }*/
        close(null,stmt,conn);//直接利用重載的方法
    }
    //重載一下
    public static void close(ResultSet rs,Statement stmt, Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    /*
    獲取連接池方法
     */
    public static DataSource getDataSource(){
        return ds;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容