Java数据库连接池实现原理
https://blog.csdn.net/tuke_tuke/article/details/51532510
?
?
一般來說,Java應(yīng)用程序訪問數(shù)據(jù)庫的過程是:
①裝載數(shù)據(jù)庫驅(qū)動(dòng)程序;
②通過jdbc建立數(shù)據(jù)庫連接;
③訪問數(shù)據(jù)庫,執(zhí)行sql語句;
④斷開數(shù)據(jù)庫連接。
public class DBConnection {
private Connection con; //定義數(shù)據(jù)庫連接類對(duì)象
private PreparedStatement pstm;
private String user="root"; //連接數(shù)據(jù)庫用戶名
private String password="123456"; //連接數(shù)據(jù)庫密碼
private String driverName="com.mysql.jdbc.Driver"; //數(shù)據(jù)庫驅(qū)動(dòng)
private String url="jdbc:mysql://localhost:3306/qingqingtuan";
//連接數(shù)據(jù)庫的URL,后面的是為了防止插入數(shù)據(jù) 庫出現(xiàn)亂碼,?useUnicode=true&characterEncoding=UTF-8
//構(gòu)造函數(shù)
public DBConnection(){
}
/**創(chuàng)建數(shù)據(jù)庫連接*/
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("加載數(shù)據(jù)庫驅(qū)動(dòng)失敗!");
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,user,password); //獲取數(shù)據(jù)庫連接
} catch (SQLException e) {
System.out.println("創(chuàng)建數(shù)據(jù)庫連接失敗!");
con=null;
e.printStackTrace();
}
return con; //返回?cái)?shù)據(jù)庫連接對(duì)象
}
List<Shop> mShopList=new ArrayList<Shop>();
mConnection=new DBConnection().getConnection();
if(mConnection!=null){
try {
String sql="select * from shop";
PreparedStatement pstm=mConnection.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
......//封裝PoPj的操作
}
rs.close();
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(mConnection!=null){
mConnection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
? ? ? ? ? ? ? ? ? ? ?
?
程序開發(fā)過程中,存在很多問題:
首先,每一次web請(qǐng)求都要建立一次數(shù)據(jù)庫連接。建立連接是一個(gè)費(fèi)時(shí)的活動(dòng),每次都得花費(fèi)0.05s~1s的時(shí)間,而且系統(tǒng)還要分配內(nèi)存資源。這個(gè)時(shí)間對(duì)于一次或幾次數(shù)據(jù)庫操作,或許感覺不出系統(tǒng)有多大的開銷。
可是對(duì)于現(xiàn)在的web應(yīng)用,尤其是大型電子商務(wù)網(wǎng)站,同時(shí)有幾百人甚至幾千人在線是很正常的事。在這種情況下,頻繁的進(jìn)行數(shù)據(jù)庫連接操作勢(shì)必占用很多的系統(tǒng)資源,網(wǎng)站的響應(yīng)速度必定下降,嚴(yán)重的甚至?xí)斐煞?wù)器的崩潰。不是危言聳聽,這就是制約某些電子商務(wù)網(wǎng)站發(fā)展的技術(shù)瓶頸問題。其次,對(duì)于每一次數(shù)據(jù)庫連接,使用完后都得斷開。否則,如果程序出現(xiàn)異常而未能關(guān)閉,將會(huì)導(dǎo)致數(shù)據(jù)庫系統(tǒng)中的內(nèi)存泄漏,最終將不得不重啟數(shù)據(jù)庫
? ? ?通過上面的分析,我們可以看出來,“數(shù)據(jù)庫連接”是一種稀缺的資源,為了保障網(wǎng)站的正常使用,應(yīng)該對(duì)其進(jìn)行妥善管理。其實(shí)我們查詢完數(shù)據(jù)庫后,如果不關(guān)閉連接,而是暫時(shí)存放起來,當(dāng)別人使用時(shí),把這個(gè)連接給他們使用。就避免了一次建立數(shù)據(jù)庫連接和斷開的操作時(shí)間消耗。
數(shù)據(jù)庫連接池的基本思想:就是為數(shù)據(jù)庫連接建立一個(gè)“緩沖池”。預(yù)先在緩沖池中放入一定數(shù)量的連接,當(dāng)需要建立數(shù)據(jù)庫連接時(shí),只需從“緩沖池”中取出一個(gè),使用完畢之后再放回去。我們可以通過設(shè)定連接池最大連接數(shù)來防止系統(tǒng)無盡的與數(shù)據(jù)庫連接
創(chuàng)建數(shù)據(jù)庫連接池大概有3個(gè)步驟:
① 創(chuàng)建ConnectionPool實(shí)例,并初始化創(chuàng)建10個(gè)連接,保存在Vector中(線程安全)
②?實(shí)現(xiàn)getConnection()從連接庫中獲取一個(gè)可用的連接
③?returnConnection(conn)?提供將連接放回連接池中方法
?
ConnectionPool.java
數(shù)據(jù)庫連接池類 ConnectionPool.java
/*
這個(gè)例子是根據(jù)POSTGRESQL數(shù)據(jù)庫寫的,
請(qǐng)用的時(shí)候根據(jù)實(shí)際的數(shù)據(jù)庫調(diào)整。
調(diào)用方法如下:
① ConnectionPool connPool
= new ConnectionPool("com.microsoft.jdbc.sqlserver.SQLServerDriver"
,"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDataForTest"
,"Username"
,"Password");
② connPool .createPool();
Connection conn = connPool .getConnection();
connPool.returnConnection(conn);
connPool.refreshConnections();
connPool.closeConnectionPool();
*/
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;
public class ConnectionPool {
private String jdbcDriver = ""; // 數(shù)據(jù)庫驅(qū)動(dòng)
private String dbUrl = ""; // 數(shù)據(jù) URL
private String dbUsername = ""; // 數(shù)據(jù)庫用戶名
private String dbPassword = ""; // 數(shù)據(jù)庫用戶密碼
private String testTable = ""; // 測(cè)試連接是否可用的測(cè)試表名,默認(rèn)沒有測(cè)試表
private int initialConnections = 10; // 連接池的初始大小
private int incrementalConnections = 5;// 連接池自動(dòng)增加的大小
private int maxConnections = 50; // 連接池最大的大小
private Vector connections = null; // 存放連接池中數(shù)據(jù)庫連接的向量 , 初始時(shí)為 null
// 它中存放的對(duì)象為 PooledConnection 型
/**
* 構(gòu)造函數(shù)
*
* @param jdbcDriver
* String JDBC 驅(qū)動(dòng)類串
* @param dbUrl
* String 數(shù)據(jù)庫 URL
* @param dbUsername
* String 連接數(shù)據(jù)庫用戶名
* @param dbPassword
* String 連接數(shù)據(jù)庫用戶的密碼
*
*/
public ConnectionPool(String jdbcDriver, String dbUrl, String dbUsername,
String dbPassword) {
this.jdbcDriver = jdbcDriver;
this.dbUrl = dbUrl;
this.dbUsername = dbUsername;
this.dbPassword = dbPassword;
}
/**
* 返回連接池的初始大小
*
* @return 初始連接池中可獲得的連接數(shù)量
*/
public int getInitialConnections() {
return this.initialConnections;
}
/**
* 設(shè)置連接池的初始大小
*
* @param 用于設(shè)置初始連接池中連接的數(shù)量
*/
public void setInitialConnections(int initialConnections) {
this.initialConnections = initialConnections;
}
/**
* 返回連接池自動(dòng)增加的大小 、
*
* @return 連接池自動(dòng)增加的大小
*/
public int getIncrementalConnections() {
return this.incrementalConnections;
}
/**
* 設(shè)置連接池自動(dòng)增加的大小
*
* @param 連接池自動(dòng)增加的大小
*/
public void setIncrementalConnections(int incrementalConnections) {
this.incrementalConnections = incrementalConnections;
}
/**
* 返回連接池中最大的可用連接數(shù)量
*
* @return 連接池中最大的可用連接數(shù)量
*/
public int getMaxConnections() {
return this.maxConnections;
}
/**
* 設(shè)置連接池中最大可用的連接數(shù)量
*
* @param 設(shè)置連接池中最大可用的連接數(shù)量值
*/
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
/**
* 獲取測(cè)試數(shù)據(jù)庫表的名字
*
* @return 測(cè)試數(shù)據(jù)庫表的名字
*/
public String getTestTable() {
return this.testTable;
}
/**
* 設(shè)置測(cè)試表的名字
*
* @param testTable
* String 測(cè)試表的名字
*/
public void setTestTable(String testTable) {
this.testTable = testTable;
}
/**
*
* 創(chuàng)建一個(gè)數(shù)據(jù)庫連接池,連接池中的可用連接的數(shù)量采用類成員 initialConnections 中設(shè)置的值
*/
public synchronized void createPool() throws Exception {
// 確保連接池沒有創(chuàng)建
// 如果連接池己經(jīng)創(chuàng)建了,保存連接的向量 connections 不會(huì)為空
if (connections != null) {
return; // 如果己經(jīng)創(chuàng)建,則返回
}
// 實(shí)例化 JDBC Driver 中指定的驅(qū)動(dòng)類實(shí)例
Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance());
DriverManager.registerDriver(driver); // 注冊(cè) JDBC 驅(qū)動(dòng)程序
// 創(chuàng)建保存連接的向量 , 初始時(shí)有 0 個(gè)元素
connections = new Vector();
// 根據(jù) initialConnections 中設(shè)置的值,創(chuàng)建連接。
createConnections(this.initialConnections);
// System.out.println(" 數(shù)據(jù)庫連接池創(chuàng)建成功! ");
}
/**
* 創(chuàng)建由 numConnections 指定數(shù)目的數(shù)據(jù)庫連接 , 并把這些連接 放入 connections 向量中
*
* @param numConnections
* 要?jiǎng)?chuàng)建的數(shù)據(jù)庫連接的數(shù)目
*/
private void createConnections(int numConnections) throws SQLException {
// 循環(huán)創(chuàng)建指定數(shù)目的數(shù)據(jù)庫連接
for (int x = 0; x < numConnections; x++) {
// 是否連接池中的數(shù)據(jù)庫連接的數(shù)量己經(jīng)達(dá)到最大?最大值由類成員 maxConnections
// 指出,如果 maxConnections 為 0 或負(fù)數(shù),表示連接數(shù)量沒有限制。
// 如果連接數(shù)己經(jīng)達(dá)到最大,即退出。
if (this.maxConnections > 0
&& this.connections.size() >= this.maxConnections) {
break;
}
// add a new PooledConnection object to connections vector
// 增加一個(gè)連接到連接池中(向量 connections 中)
try {
connections.addElement(new PooledConnection(newConnection()));
} catch (SQLException e) {
System.out.println(" 創(chuàng)建數(shù)據(jù)庫連接失敗! " + e.getMessage());
throw new SQLException();
}
// System.out.println(" 數(shù)據(jù)庫連接己創(chuàng)建 ......");
}
}
/**
* 創(chuàng)建一個(gè)新的數(shù)據(jù)庫連接并返回它
*
* @return 返回一個(gè)新創(chuàng)建的數(shù)據(jù)庫連接
*/
private Connection newConnection() throws SQLException {
// 創(chuàng)建一個(gè)數(shù)據(jù)庫連接
Connection conn = DriverManager.getConnection(dbUrl, dbUsername,
dbPassword);
// 如果這是第一次創(chuàng)建數(shù)據(jù)庫連接,即檢查數(shù)據(jù)庫,獲得此數(shù)據(jù)庫允許支持的
// 最大客戶連接數(shù)目
// connections.size()==0 表示目前沒有連接己被創(chuàng)建
if (connections.size() == 0) {
DatabaseMetaData metaData = conn.getMetaData();
int driverMaxConnections = metaData.getMaxConnections();
// 數(shù)據(jù)庫返回的 driverMaxConnections 若為 0 ,表示此數(shù)據(jù)庫沒有最大
// 連接限制,或數(shù)據(jù)庫的最大連接限制不知道
// driverMaxConnections 為返回的一個(gè)整數(shù),表示此數(shù)據(jù)庫允許客戶連接的數(shù)目
// 如果連接池中設(shè)置的最大連接數(shù)量大于數(shù)據(jù)庫允許的連接數(shù)目 , 則置連接池的最大
// 連接數(shù)目為數(shù)據(jù)庫允許的最大數(shù)目
if (driverMaxConnections > 0
&& this.maxConnections > driverMaxConnections) {
this.maxConnections = driverMaxConnections;
}
}
return conn; // 返回創(chuàng)建的新的數(shù)據(jù)庫連接
}
/**
* 通過調(diào)用 getFreeConnection() 函數(shù)返回一個(gè)可用的數(shù)據(jù)庫連接 , 如果當(dāng)前沒有可用的數(shù)據(jù)庫連接,并且更多的數(shù)據(jù)庫連接不能創(chuàng)
* 建(如連接池大小的限制),此函數(shù)等待一會(huì)再嘗試獲取。
*
* @return 返回一個(gè)可用的數(shù)據(jù)庫連接對(duì)象
*/
public synchronized Connection getConnection() throws SQLException {
// 確保連接池己被創(chuàng)建
if (connections == null) {
return null; // 連接池還沒創(chuàng)建,則返回 null
}
Connection conn = getFreeConnection(); // 獲得一個(gè)可用的數(shù)據(jù)庫連接
// 如果目前沒有可以使用的連接,即所有的連接都在使用中
while (conn == null) {
// 等一會(huì)再試
// System.out.println("Wait");
wait(250);
conn = getFreeConnection(); // 重新再試,直到獲得可用的連接,如果
// getFreeConnection() 返回的為 null
// 則表明創(chuàng)建一批連接后也不可獲得可用連接
}
return conn;// 返回獲得的可用的連接
}
/**
* 本函數(shù)從連接池向量 connections 中返回一個(gè)可用的的數(shù)據(jù)庫連接,如果 當(dāng)前沒有可用的數(shù)據(jù)庫連接,本函數(shù)則根據(jù)
* incrementalConnections 設(shè)置 的值創(chuàng)建幾個(gè)數(shù)據(jù)庫連接,并放入連接池中。 如果創(chuàng)建后,所有的連接仍都在使用中,則返回 null
*
* @return 返回一個(gè)可用的數(shù)據(jù)庫連接
*/
private Connection getFreeConnection() throws SQLException {
// 從連接池中獲得一個(gè)可用的數(shù)據(jù)庫連接
Connection conn = findFreeConnection();
if (conn == null) {
// 如果目前連接池中沒有可用的連接
// 創(chuàng)建一些連接
createConnections(incrementalConnections);
// 重新從池中查找是否有可用連接
conn = findFreeConnection();
if (conn == null) {
// 如果創(chuàng)建連接后仍獲得不到可用的連接,則返回 null
return null;
}
}
return conn;
}
/**
* 查找連接池中所有的連接,查找一個(gè)可用的數(shù)據(jù)庫連接, 如果沒有可用的連接,返回 null
*
* @return 返回一個(gè)可用的數(shù)據(jù)庫連接
*/
private Connection findFreeConnection() throws SQLException {
Connection conn = null;
PooledConnection pConn = null;
// 獲得連接池向量中所有的對(duì)象
Enumeration enumerate = connections.elements();
// 遍歷所有的對(duì)象,看是否有可用的連接
while (enumerate.hasMoreElements()) {
pConn = (PooledConnection) enumerate.nextElement();
if (!pConn.isBusy()) {
// 如果此對(duì)象不忙,則獲得它的數(shù)據(jù)庫連接并把它設(shè)為忙
conn = pConn.getConnection();
pConn.setBusy(true);
// 測(cè)試此連接是否可用
if (!testConnection(conn)) {
// 如果此連接不可再用了,則創(chuàng)建一個(gè)新的連接,
// 并替換此不可用的連接對(duì)象,如果創(chuàng)建失敗,返回 null
try {
conn = newConnection();
} catch (SQLException e) {
System.out.println(" 創(chuàng)建數(shù)據(jù)庫連接失敗! " + e.getMessage());
return null;
}
pConn.setConnection(conn);
}
break; // 己經(jīng)找到一個(gè)可用的連接,退出
}
}
return conn;// 返回找到到的可用連接
}
/**
* 測(cè)試一個(gè)連接是否可用,如果不可用,關(guān)掉它并返回 false 否則可用返回 true
*
* @param conn
* 需要測(cè)試的數(shù)據(jù)庫連接
* @return 返回 true 表示此連接可用, false 表示不可用
*/
private boolean testConnection(Connection conn) {
try {
// 判斷測(cè)試表是否存在
if (testTable.equals("")) {
// 如果測(cè)試表為空,試著使用此連接的 setAutoCommit() 方法
// 來判斷連接否可用(此方法只在部分?jǐn)?shù)據(jù)庫可用,如果不可用 ,
// 拋出異常)。注意:使用測(cè)試表的方法更可靠
conn.setAutoCommit(true);
} else {// 有測(cè)試表的時(shí)候使用測(cè)試表測(cè)試
// check if this connection is valid
Statement stmt = conn.createStatement();
stmt.execute("select count(*) from " + testTable);
}
} catch (SQLException e) {
// 上面拋出異常,此連接己不可用,關(guān)閉它,并返回 false;
closeConnection(conn);
return false;
}
// 連接可用,返回 true
return true;
}
/**
* 此函數(shù)返回一個(gè)數(shù)據(jù)庫連接到連接池中,并把此連接置為空閑。 所有使用連接池獲得的數(shù)據(jù)庫連接均應(yīng)在不使用此連接時(shí)返回它。
*
* @param 需返回到連接池中的連接對(duì)象
*/
public void returnConnection(Connection conn) {
// 確保連接池存在,如果連接沒有創(chuàng)建(不存在),直接返回
if (connections == null) {
System.out.println(" 連接池不存在,無法返回此連接到連接池中 !");
return;
}
PooledConnection pConn = null;
Enumeration enumerate = connections.elements();
// 遍歷連接池中的所有連接,找到這個(gè)要返回的連接對(duì)象
while (enumerate.hasMoreElements()) {
pConn = (PooledConnection) enumerate.nextElement();
// 先找到連接池中的要返回的連接對(duì)象
if (conn == pConn.getConnection()) {
// 找到了 , 設(shè)置此連接為空閑狀態(tài)
pConn.setBusy(false);
break;
}
}
}
/**
* 刷新連接池中所有的連接對(duì)象
*
*/
public synchronized void refreshConnections() throws SQLException {
// 確保連接池己創(chuàng)新存在
if (connections == null) {
System.out.println(" 連接池不存在,無法刷新 !");
return;
}
PooledConnection pConn = null;
Enumeration enumerate = connections.elements();
while (enumerate.hasMoreElements()) {
// 獲得一個(gè)連接對(duì)象
pConn = (PooledConnection) enumerate.nextElement();
// 如果對(duì)象忙則等 5 秒 ,5 秒后直接刷新
if (pConn.isBusy()) {
wait(5000); // 等 5 秒
}
// 關(guān)閉此連接,用一個(gè)新的連接代替它。
closeConnection(pConn.getConnection());
pConn.setConnection(newConnection());
pConn.setBusy(false);
}
}
/**
* 關(guān)閉連接池中所有的連接,并清空連接池。
*/
public synchronized void closeConnectionPool() throws SQLException {
// 確保連接池存在,如果不存在,返回
if (connections == null) {
System.out.println(" 連接池不存在,無法關(guān)閉 !");
return;
}
PooledConnection pConn = null;
Enumeration enumerate = connections.elements();
while (enumerate.hasMoreElements()) {
pConn = (PooledConnection) enumerate.nextElement();
// 如果忙,等 5 秒
if (pConn.isBusy()) {
wait(5000); // 等 5 秒
}
// 5 秒后直接關(guān)閉它
closeConnection(pConn.getConnection());
// 從連接池向量中刪除它
connections.removeElement(pConn);
}
// 置連接池為空
connections = null;
}
/**
* 關(guān)閉一個(gè)數(shù)據(jù)庫連接
*
* @param 需要關(guān)閉的數(shù)據(jù)庫連接
*/
private void closeConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println(" 關(guān)閉數(shù)據(jù)庫連接出錯(cuò): " + e.getMessage());
}
}
/**
* 使程序等待給定的毫秒數(shù)
*
* @param 給定的毫秒數(shù)
*/
private void wait(int mSeconds) {
try {
Thread.sleep(mSeconds);
} catch (InterruptedException e) {
}
}
/**
*
* 內(nèi)部使用的用于保存連接池中連接對(duì)象的類 此類中有兩個(gè)成員,一個(gè)是數(shù)據(jù)庫的連接,另一個(gè)是指示此連接是否 正在使用的標(biāo)志。
*/
class PooledConnection {
Connection connection = null;// 數(shù)據(jù)庫連接
boolean busy = false; // 此連接是否正在使用的標(biāo)志,默認(rèn)沒有正在使用
// 構(gòu)造函數(shù),根據(jù)一個(gè) Connection 構(gòu)告一個(gè) PooledConnection 對(duì)象
public PooledConnection(Connection connection) {
this.connection = connection;
}
// 返回此對(duì)象中的連接
public Connection getConnection() {
return connection;
}
// 設(shè)置此對(duì)象的,連接
public void setConnection(Connection connection) {
this.connection = connection;
}
// 獲得對(duì)象連接是否忙
public boolean isBusy() {
return busy;
}
// 設(shè)置對(duì)象的連接正在忙
public void setBusy(boolean busy) {
this.busy = busy;
}
}
}
ConnectionPoolUtils.java
/*連接池工具類,返回唯一的一個(gè)數(shù)據(jù)庫連接池對(duì)象,單例模式*/
public class ConnectionPoolUtils {
private ConnectionPoolUtils(){};//私有靜態(tài)方法
private static ConnectionPool poolInstance = null;
public static ConnectionPool GetPoolInstance(){
if(poolInstance == null) {
poolInstance = new ConnectionPool(
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8",
"root", "123456");
try {
poolInstance.createPool();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return poolInstance;
}
}
ConnectionPoolTest.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectionTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
/*使用連接池創(chuàng)建100個(gè)連接的時(shí)間*/
/*// 創(chuàng)建數(shù)據(jù)庫連接庫對(duì)象
ConnectionPool connPool = new ConnectionPool("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/test", "root", "123456");
// 新建數(shù)據(jù)庫連接庫
connPool.createPool();*/
ConnectionPool connPool=ConnectionPoolUtils.GetPoolInstance();//單例模式創(chuàng)建連接池對(duì)象
// SQL測(cè)試語句
String sql = "Select * from pet";
// 設(shè)定程序運(yùn)行起始時(shí)間
long start = System.currentTimeMillis();
// 循環(huán)測(cè)試100次數(shù)據(jù)庫連接
for (int i = 0; i < 100; i++) {
Connection conn = connPool.getConnection(); // 從連接庫中獲取一個(gè)可用的連接
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
// System.out.println("查詢結(jié)果" + name);
}
rs.close();
stmt.close();
connPool.returnConnection(conn);// 連接使用完后釋放連接到連接池
}
System.out.println("經(jīng)過100次的循環(huán)調(diào)用,使用連接池花費(fèi)的時(shí)間:"+ (System.currentTimeMillis() - start) + "ms");
// connPool.refreshConnections();//刷新數(shù)據(jù)庫連接池中所有連接,即不管連接是否正在運(yùn)行,都把所有連接都釋放并放回到連接池。注意:這個(gè)耗時(shí)比較大。
connPool.closeConnectionPool();// 關(guān)閉數(shù)據(jù)庫連接池。注意:這個(gè)耗時(shí)比較大。
// 設(shè)定程序運(yùn)行起始時(shí)間
start = System.currentTimeMillis();
/*不使用連接池創(chuàng)建100個(gè)連接的時(shí)間*/
// 導(dǎo)入驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
for (int i = 0; i < 100; i++) {
// 創(chuàng)建連接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
}
rs.close();
stmt.close();
conn.close();// 關(guān)閉連接
}
System.out.println("經(jīng)過100次的循環(huán)調(diào)用,不使用連接池花費(fèi)的時(shí)間:"
+ (System.currentTimeMillis() - start) + "ms");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
---------------------
作者:tuke_tuke
來源:CSDN
原文:https://blog.csdn.net/tuke_tuke/article/details/51532510?utm_source=copy
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
轉(zhuǎn)載于:https://www.cnblogs.com/hfultrastrong/p/9789556.html
總結(jié)
以上是生活随笔為你收集整理的Java数据库连接池实现原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postman测试传入json
- 下一篇: mac 安装淘宝镜像报错之坑