/**
1. @author : gong zhi qiang
2. @data : 2019-08-30 , 22:50
3. @user : snachat
4. @project: designpattern
5. @description :
*/public
abstract
class
pool
protected
void
initdbinfo()
catch
(ioexception e)
this
.drivername = properties.
getproperty
("drivername");
this
.maxconnect = integer.
parseint
(properties.
getproperty
("maxconnect"))
;this
.normalconnect = integer.
parseint
(properties.
getproperty
("normalconnect"))
;this
.username = properties.
getproperty
("username");
this
.password = properties.
getproperty
("password");
this
.url = properties.
getproperty
("url");
}private
void
loaddrivers
(string drivername)
catch
(instantiationexception e)
catch
(illegalacces***ception e)
catch
(classnotfoundexception e)
catch
(sqlexception e)
finally
}/**
* @return
* @throws instantiationerror
* @throws illegalacces***ception
* @throws classnotfoundexception
*/public
static
synchronized pool getinstance()
throws instantiationexception, illegalacces***ception, classnotfoundexception
return instance;
}public
abstract connection getconnection()
;/**
* @param time
* @return
*/public
abstract connection getconnection
(long time)
;/**
* @param connection 將物件返回給連線池
*/public
abstract
void
freeconnection
(connection connection)
;/**
* @return 返回當前空閒連線數
*/public
abstract
intgetnum()
;/**
* @return 返回當前工作的連線數
*/public
abstract
intgetnumactive()
;public
synchronized
void
realse()
catch
(sqlexception e)
}}
public
final
class
dbconnectionpool
extends
pool
return pool;
}/**
* 構造方法,初始化連線池
*/private
dbconnectionpool()
}}/** * @return 從連線池中返回乙個物件
*/public
synchronized connection getconnection()
}catch
(sqlexception e)
}else
if(maxconnect ==
0|| checkedout < maxconnect)
if(connection != null)
numactive++
;return connection;
}/**
* @param time 指定超時,超時將返回 null
* @return 返回乙個connection物件
*/public
synchronized connection getconnection
(long time)
catch
(interruptedexception e)
//超時if(
newdate()
.gettime()
- starttime >= time)
}return connection;
}/**
* @param connection 將物件返回給連線池
*/public
void
freeconnection
(connection connection)
/** * @return 當前的連線池中剩餘的可用連線數量
*/public
intgetnum()
/** * @return 當期使用中的連線的數量
*/public
intgetnumactive()
/** * @return 在沒有超過連線池的最大值maxconnect< /code>的情況下,建立乙個新的連線
*/private connection newconnection()
else
system.out.
println
("連線池建立乙個新的連線");
}catch
(sqlexception e)
finally
return connection;
}/**
* 關閉連線池
*/public
synchronized
void
realse()
catch
(sqlexception e)
freeconnection.
removeallelements()
; numactive =0;
}super
.realse();}}
url=jdbc:mysql:
//localhost:
3306
/musicdb
username=root
password=資料庫的密碼
maxconnect=
100normalconnect =
10drivername=com.mysql.cj.jdbc.driver
pool pool = dbconnectionpool.
getinstance()
; connection connection = pool.
getconnection
(2000);
objects.
requirenonnull
(connection ,
"獲取連線物件為空");
system.out.
println
("當前連線池中空閒的連線物件 = "
+ pool.
getnum()
);system.out.
println
("正在使用的連線的數量 = "
資料庫連線池的實現
一 設計資料庫連線池 將資料庫連線池可以作為乙個容器,在這個容器裡邊含有connection jdbc介面 可以將connection作為乙個管道,每一次執行緒獲取connection 獲取管道 後進行資料庫操作。1 connection的儲存結構選擇linkedlist 2 執行緒釋放管道 從li...
資料庫連線池 Redis連線池
基本原理 在內部物件池中,維護一定數量的資料庫連線,並對外暴露資料庫連線的獲取和返回方法。如外部使用者可通過getconnection方法獲取資料庫連線,使用完畢後再通過releaseconnection方法將連線返回,注意此時的連線並沒有關閉,而是由連線池管理器 並為下一次使用做好準備。2.作用 ...
如何實現乙個連線池?
目錄前言 什麼是連線池?為什麼需要連線池?怎樣做乙個連線池?高階連線池 推薦原始碼 2w1h 是技術領域中一種非常有效的思考和學習方式,即what why和how 堅持 2w1h 可以快速提公升我們的深度思考能力。今天我們通過 2w1h 方式來討論 連線池 什麼是連線池 what 為什麼需要連線池 ...