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

什么是數(shù)據(jù)庫連接池

連接池是創(chuàng)建和管理一個連接的緩沖池的技術,這些連接準備好被任何需要它們的線程使用

數(shù)據(jù)庫連接池有什么用

數(shù)據(jù)庫連接池負責分配,管理和釋放數(shù)據(jù)庫連接,
它允許應用程序重復使用一個現(xiàn)有的數(shù)據(jù)庫連接,
而不是在重新建立一個,
釋放空間時間超過最大空閑時間的數(shù)據(jù)庫連接來避免因為沒有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫鏈接遺漏。
這項技術明顯提高對數(shù)據(jù)庫操作的性能。

有哪些可用的連接池

  • 現(xiàn)在很多web服務器都提供了DataSoruce的實現(xiàn),即連接池的實現(xiàn),
    通常我們把DataSource的實現(xiàn),,數(shù)據(jù)源中包含了數(shù)據(jù)庫連接池的實現(xiàn)
  • DBCP數(shù)據(jù)庫連接池
  • C3P0 數(shù)據(jù)庫連接池

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

  • DBCP 是 Apache 軟件基金組織下的開源連接池實現(xiàn),使用DBCP數(shù)據(jù)源,應用程序應在系統(tǒng)中增加如下兩個 jar 文件
    • Commons-dbcp.jar:連接池的實現(xiàn)
    • Commons-pool.jar:連接池實現(xiàn)的依賴庫

DBCP 實例代碼

1,src 添加文件dbcpconfig.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=

initialSize=10

maxActive=50

maxIdle=20

minIdle=5

maxWait=60000


connectionProperties=useUnicode=true;characterEncoding=utf8

defaultAutoCommit=true

defaultReadOnly=

defaultTransactionIsolation=REPEATABLE_READ
2,DBCPUtil 封裝
public class DBCPUtil {
    private static DataSource ds = null;
    static {
        Properties prop = new Properties();
        try {
            prop.load(DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
            ds = BasicDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("服務器忙");
        }

    }

    public static void release(Connection conn, Statement stmt, ResultSet rs) {

        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }

}   
3,DBCPUtil 使用
Connection conn = DBCPUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
    ps = conn.prepareStatement("select * from user");
    rs = ps.executeQuery();
    while (rs.next()) {
        System.out.println(rs.getString(1));
        System.out.println(rs.getString(2));
        System.out.println(rs.getString(3));
        System.out.println(rs.getString(4));
        System.out.println(rs.getString(5));
        System.out.println(rs.getString(6));
        System.out.println(rs.getString(7));
    }
} catch (SQLException e) {
    e.printStackTrace();
} finally {
    DBCPUtil.release(conn, ps, rs);
}

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

  • 使用C3P0需要添加 c3p0-0.9.1.2.jar
1,src 添加配置文件c3p0-config.xml
<?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/demo?useUnicode=true&characterEncoding=UTF-8</property>
    <property name="user">root</property>
    <property name="password"></property>
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>

  </default-config>
</c3p0-config>  
C3P0Util 封裝
public class C3P0Util {
//得到一個數(shù)據(jù)源
private static DataSource dataSource = new ComboPooledDataSource();
public static Connection getConnection(){
    try {
        return dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("服務器錯誤");
    }
}

public static DataSource getDataSource() {
    return dataSource;
}

public static void setDataSource(DataSource dataSource) {
    C3P0Util.dataSource = dataSource;
}

public static void release(Connection conn, Statement stmt, ResultSet rs) {

    if (rs != null) {
        try {
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        rs = null;
    }
    if (stmt != null) {
        try {
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        stmt = null;
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        conn = null;
    }
}

}

C3P0Util 使用
PreparedStatement ps = null;
    ResultSet rs = null;
    Connection conn = C3P0Util.getConnection();
    try {
        ps = conn.prepareStatement("select * from user");
        rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
            System.out.println(rs.getString(4));
            System.out.println(rs.getString(5));
            System.out.println(rs.getString(6));
            System.out.println(rs.getString(7));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        C3P0Util.release(conn, ps, rs);
    }
優(yōu)化jdbc
  • 從連接數(shù)據(jù)庫優(yōu)化
  • 查詢輸出結果優(yōu)化

2016.10.29

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 前言 數(shù)據(jù)庫連接池在Java數(shù)據(jù)庫相關中間件產(chǎn)品群中,應該算是底層最基礎的一類產(chǎn)品,作為企業(yè)應用開發(fā)必不可少的組件...
    許da廣閱讀 7,455評論 2 27
  • 聲明:本欄目所使用的素材都是凱哥學堂VIP學員所寫,學員有權匿名,對文章有最終解釋權;凱哥學堂旨在促進VIP學員互...
    凱哥學堂閱讀 1,144評論 0 0
  • 最原始的數(shù)據(jù)庫連接就是我們打開一個連接,使用過后再關閉該鏈接來釋放資源。頻繁的新建打開再關閉連接對jvm和數(shù)據(jù)庫都...
    野柳閱讀 6,592評論 1 11
  • 素梅的婚姻已經(jīng)只剩一張紙了,離已經(jīng)迫在眉睫了。 五年前,37歲的她認識了32歲的男人。 他們在一次酒后發(fā)生了關系,...
    林姑娘與波斯貓閱讀 586評論 1 4
  • 我只是寂寞。我想找個人聊天,找個人陪伴。 她太過反復無常, 我只是想做朋友, 這是本質, 其他是表面。 我和她絕交...
    駁悖閱讀 163評論 0 0

友情鏈接更多精彩內容