一二驰、模擬連接池
1、目的:連接池產(chǎn)品的復(fù)用機制如何設(shè)計的
2矿酵、連接池類--FastConnectionPool
3坏瘩、創(chuàng)建一個數(shù)據(jù)庫連接池--LinkedList
4漠魏、初始10個連接對象--循環(huán)添加10個連接對象
5柱锹、來一個客戶端禁熏,從連接池里取--(取出并移除連接池對象)
6、當(dāng)客戶用完了胧华,則不是真的銷毀矩动,而是放入到連接池中
FastConnectPool.java
/**
* 1. 創(chuàng)建一個數(shù)據(jù)庫連接池
2. 初始10個連接對象---放入集合(池)
3. 來一個客戶端悲没,從連接池里取
4. 當(dāng)客戶用完了男图,則不是真的銷毀逊笆,而是放入到連接池中
*
*/
public class FastConnectPool {
//1. 創(chuàng)建一個數(shù)據(jù)庫連接池
//2. 初始10個連接對象---放入集合(池)
//使用LinkedList难裆,調(diào)用它內(nèi)部派生的方法
public static LinkedList<Connection> list = new LinkedList<>();
static{
for(int i=0;i<10;i++){
list.add(DBUtils.getConnection());
}
}
public Connection getConnection(){
//1.要判斷池子中是否有連接對象;沒有則重新創(chuàng)建連接池
//2.判斷是否超出了最大連接數(shù)據(jù)
//3.判斷是否超時
return list.removeFirst(); //拿到元素,并從集合中移除
}
public void release(Connection conn) {
list.addLast(conn);? //在集合后面追加
}
}
Test.java
/**
* 模擬連接池的操作:
*
*/
public class Test {
public static void main(String[] args) {
FastConnectPool pool = new FastConnectPool();
System.out.println(FastConnectPool.list.size());? //10
Connection conn = pool.getConnection();
System.out.println(FastConnectPool.list.size()); //9
//如何回收
pool.release(conn);
System.out.println(FastConnectPool.list.size()); //10
//說明: 為什么需要建立DataSource的標(biāo)準(zhǔn)偏化?
//方便更換連接池,做擴展及維護
/*DbcpPool pool2 = new DbcpPool();
pool2.getConn();
pool2.closeAll();*/
}
}
二驶冒、建立DataSource標(biāo)準(zhǔn)
1骗污、為什么需要建立標(biāo)準(zhǔn)
1)因為不同的連接池有不同實現(xiàn)需忿,不方便管理
2)換連接池產(chǎn)品時蜡歹,變動太大
2月而、如何建立標(biāo)準(zhǔn)的
1)新建連接池類(FastConnectionPool)實現(xiàn)DataSource接口
2)獲取連接對象時父款,獲取的是包裝類的連接對象
3)關(guān)閉時,進行了連接對象的回收
3世杀、為什么包裝連接對象可以回收到連接池
1)獲取連接對象時玫坛,返回包裝連接對象包晰,用接口接收沒問題
2)包裝類對象關(guān)閉連接時伐憾,其內(nèi)部是通過Connection對象去回收的
ConnectionWrapper.java
public class ConnectionWrapper implements Connection {
private LinkedList<Connection> list;
private Connection conn;
public ConnectionWrapper(LinkedList<Connection> list, Connection conn) {
this.conn = conn;
this.list = list;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql);
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void commit() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void rollback() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void close() throws SQLException {
//在此處寫成回收即可
System.out.println("回收.....");
list.addLast(conn);? //在集合后面追加
}
@Override
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
// TODO Auto-generated method stub
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
// TODO Auto-generated method stub
}
@Override
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setSchema(String schema) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public String getSchema() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void abort(Executor executor) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
}
FastConnectPool.java
/**
* 1. 創(chuàng)建一個數(shù)據(jù)庫連接池
2. 初始10個連接對象---放入集合(池)
3. 來一個客戶端乡话,從連接池里取
4. 當(dāng)客戶用完了,則不是真的銷毀诬像,而是放入到連接池中
*
*/
public class FastConnectPool implements DataSource {
//1. 創(chuàng)建一個數(shù)據(jù)庫連接池
//2. 初始10個連接對象---放入集合(池)
//使用LinkedList坏挠,調(diào)用它內(nèi)部派生的方法
private static LinkedList<Connection> list = new LinkedList<>();
static{
for(int i=0;i<10;i++){
list.add(DBUtils.getConnection());
}
}
public Connection getConnection(){
//1.要判斷池子中是否有連接對象邪乍;沒有則重新創(chuàng)建連接池
//2.判斷是否超出了最大連接數(shù)據(jù)
//3.判斷是否超時
Connection conn = list.removeFirst();
//設(shè)計模式: 裝飾者模式
//包裝后得到更強大的類庇楞,最終的實現(xiàn)依然是原生對象去處理的
ConnectionWrapper wrapper = new ConnectionWrapper(list,conn);
return wrapper; //拿到元素姐刁,并從集合中移除
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
Test.java
/**
* 建立DataSouce標(biāo)準(zhǔn):
* 1.自定義的連接池產(chǎn)品要實現(xiàn)DataSouce
* 2.在獲取連接對象時壁拉,返回的是包裝類的連接對象--(就是Connection的實現(xiàn)類對象)
* 3.在連接的包裝類中重寫close方法--回收
*
*/
public class Test {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new FastConnectPool();
Connection conn = dataSource.getConnection();
//dataSource.release();? err 實現(xiàn)類派生的方法不能調(diào)用
//((FastConnectPool)dataSource).release();? //不能強轉(zhuǎn)柏靶,強轉(zhuǎn)后就不是面向接口了
PreparedStatement prst = conn.prepareStatement("insert into t_user(name,password) values('uu','123')");
int result = prst.executeUpdate();
System.out.println(result);
conn.close();? //直接會關(guān)閉連接對象--->本質(zhì)變回收
}
}