在JDBC編程的時候千扔,獲取到一個數據庫連接資源是很寶貴的砸烦,倘若數據庫訪問量超大,而數據庫連接資源又沒能得到及時的釋放听盖,就會導致系統(tǒng)的崩潰甚至宕機胀溺。造成的損失將會是巨大的裂七。再看有了數據庫連接池的JDBC,就會較好的解決資源的創(chuàng)建與連接問題仓坞,其主要還是針對于連接資源使用層面的改進背零。下面我就談一談我對數據庫連接池的理解。
數據庫連接池理論基礎
對于創(chuàng)建一個數據庫連接池无埃,需要做好準備工作徙瓶。原理就是先實現(xiàn)DataSource接口,覆蓋里面的getConnection()方法嫉称,
這個方法是我們最為關注的侦镇。既然是池,就不會是一個連接對象织阅,因此需要使用集合來保存這些個連接對象壳繁。
但是,最為關鍵的是荔棉,開發(fā)人員在使用完連接對象后通常會調用conn.close(),方法來釋放數據庫連接資源闹炉,這將會把數據庫連接返還給數據庫,而不是數據庫連接池润樱,因此渣触,數據庫連接池的存在就沒了意義了。所以我們要增強close方法壹若,來保證數據庫連接資源返還給數據庫連接池昵观。
創(chuàng)建數據庫連接池
public class JDCBCPool implements DataSource {
/*
* 既然是一個數據庫連接池,就必須有許多的連接舌稀,所以需要使用一個集合類保存這些連接 (non-Javadoc)
*
* @see javax.sql.CommonDataSource#getLogWriter()
*/
private static LinkedList<Connection> list = new LinkedList<Connection>();
// 創(chuàng)建一個配置文件,用于讀取相應配置文件中保存的數據信息
private static Properties config = new Properties();
/*
* 在這里向數據庫申請一批數據庫連接 為了只加載一次驅動程序灼擂,因此在靜態(tài)代碼塊中進行聲明壁查,這樣驅動就只會加載一次
*/
static {
try {
config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
// Class.forName("com.mysql.jdbc.Driver");
Class.forName(config.getProperty("DRIVER"));
try {
//申請十個數據庫連接對象,也就是數據庫連接池的容量是10
for(int i=0 ;i<10;i++){
Connection conn = DriverManager.getConnection(config.getProperty("URL"),
config.getProperty("USER"),config.getProperty("PASSWORD"));
list.add(conn);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
throw new ExceptionInInitializerError();
}
}
@Override
public Connection getConnection() throws SQLException {
if(list.size()<=0){
throw new RuntimeException("數據庫忙剔应,請待會再試試吧睡腿!");
}
//需要注意的是,不能使用get方式(這個方法知識返回一個引用而已),
//應該在獲取的同時峻贮,刪除掉這個鏈接席怪,之后再還回來.現(xiàn)在注意是返還給數據庫連接池!O丝亍挂捻!
Connection conn = list.removeFirst();
MyConnection myconn = new MyConnection(conn);
//從這里開始返回的就是一個數據庫連接池對象的conn
return myconn;
}
/////////////////////////////////////////////////////////////////////////datasource接口的實現(xiàn)方法開始
@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;
}
}
不難看出,數據庫連接池相對于普通的數據庫連接的創(chuàng)建船万,并沒有什么特別難寫的部分刻撒。
增強close方法骨田,保證數據庫連接資源用完后返還給連接池
要想增強close方法的功能,一般來說我們有如下幾種方式声怔。
- 創(chuàng)建子類态贤,覆蓋close方法,但是創(chuàng)建子類的時候要實現(xiàn)的部分可能會特別多醋火,因此并不常用
- 使用包裝設計模式
- 使用動態(tài)代理方式
今天我就來實現(xiàn)一下怎么使用包裝設計模式來實現(xiàn)close方法的增強悠汽。
包裝設計模式實現(xiàn)close方法的增強
要實現(xiàn)包裝設計模式,思路很簡單芥驳。
- 創(chuàng)建一個類柿冲,實現(xiàn)與被增強對象相同的接口
- 將被增強對象作為一個成員變量加到這個類中
- 定義一個構造方法,并把被增強對象作為參數傳進來
- 覆蓋要增強的方法晚树,這里是close方法
- 對于不想進行增強的方法姻采,借助于被增強對象實現(xiàn)即可。
下面是基于包裝設計模式實現(xiàn)的增強的MyConnection類:
class MyConnection implements Connection {
private Connection conn ;
public MyConnection(Connection conn ){
this.conn = conn;
}
/**
* 自定義的包裝設計模式類爵憎,增強close方法慨亲,
* 將數據庫鏈接資源返還給數據庫連接池,而不是數據庫
*/
public void close(){
list.add(conn);
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return this.conn.unwrap(iface);
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return this.conn.isWrapperFor(iface);
}
@Override
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStatement();
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql);
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareCall(sql);
}
@Override
public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return this.conn.nativeSQL(sql);
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub
this.conn.setAutoCommit(autoCommit);
}
@Override
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getAutoCommit();
}
@Override
public void commit() throws SQLException {
// TODO Auto-generated method stub
this.conn.commit();
}
@Override
public void rollback() throws SQLException {
// TODO Auto-generated method stub
this.conn.rollback();
}
@Override
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return this.conn.isClosed();
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getMetaData();
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub
this.conn.setReadOnly(readOnly);
}
@Override
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return this.conn.isReadOnly();
}
@Override
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub
this.conn.setCatalog(catalog);
}
@Override
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getCatalog();
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub
this.conn.setTransactionIsolation(level);
}
@Override
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getTransactionIsolation();
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getWarnings();
}
@Override
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
this.conn.clearWarnings();
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStatement(resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency);
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getTypeMap();
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub
this.conn.setTypeMap(map);
}
@Override
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub
this.conn.setHoldability(holdability);
}
@Override
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getHoldability();
}
@Override
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return this.conn.setSavepoint();
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return this.conn.setSavepoint(name);
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
this.conn.rollback(savepoint);
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
this.conn.releaseSavepoint(savepoint);
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, columnIndexes);
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, columnNames);
}
@Override
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createClob();
}
@Override
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createBlob();
}
@Override
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createNClob();
}
@Override
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createSQLXML();
}
@Override
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
return this.conn.isValid(timeout);
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
// TODO Auto-generated method stub
this.conn.setClientInfo(name, value);
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
// TODO Auto-generated method stub
this.conn.setClientInfo(properties);
}
@Override
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return this.conn.getClientInfo(name);
}
@Override
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getClientInfo();
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
// TODO Auto-generated method stub
return this.conn.createArrayOf(typeName, elements);
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStruct(typeName, attributes);
}
@Override
public void setSchema(String schema) throws SQLException {
// TODO Auto-generated method stub
this.conn.setSchema(schema);
}
@Override
public String getSchema() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getSchema();
}
@Override
public void abort(Executor executor) throws SQLException {
// TODO Auto-generated method stub
this.conn.abort(executor);
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
// TODO Auto-generated method stub
this.conn.setNetworkTimeout(executor, milliseconds);
}
@Override
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getNetworkTimeout();
}
}
一個簡單的數據庫連接池的實現(xiàn)案例
說是一個案例,其實也可以作為一個工具類作為今后程序開發(fā)的使用,這將會大大的提升數據庫連接資源的使用屹徘。需要注意的是掌逛,里面的驅動程序是基于我自己的數據庫來書寫的,進行使用時記得修改一下配置文件db.properties.文件中的內容即可装盯。
db.properties中內容如下:
#when use this db.properties ,need to change the database name
DRIVER = com.mysql.jdbc.Driver
URL = jdbc:mysql://localhost:3306/test
USER = root
PASSWORD = mysql
下面就是JDBCPool工具類的實現(xiàn):
package utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.sql.DataSource;
/**
* 數據庫連接池工具類
*
* @author Summer
*
*/
public class JDCBCPool implements DataSource {
/*
* 既然是一個數據庫連接池,就必須有許多的連接,所以需要使用一個集合類保存這些連接 (non-Javadoc)
*
* @see javax.sql.CommonDataSource#getLogWriter()
*/
private static LinkedList<Connection> list = new LinkedList<Connection>();
// 創(chuàng)建一個配置文件碍舍,用于讀取相應配置文件中保存的數據信息
private static Properties config = new Properties();
/*
* 在這里向數據庫申請一批數據庫連接 為了只加載一次驅動程序,因此在靜態(tài)代碼塊中進行聲明邑雅,這樣驅動就只會加載一次
*/
static {
try {
config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
// Class.forName("com.mysql.jdbc.Driver");
Class.forName(config.getProperty("DRIVER"));
try {
//申請十個數據庫連接對象片橡,也就是數據庫連接池的容量是10
for(int i=0 ;i<10;i++){
Connection conn = DriverManager.getConnection(config.getProperty("URL"),
config.getProperty("USER"),config.getProperty("PASSWORD"));
list.add(conn);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
throw new ExceptionInInitializerError();
}
}
@Override
public Connection getConnection() throws SQLException {
if(list.size()<=0){
throw new RuntimeException("數據庫忙,請待會再試試吧淮野!");
}
//需要注意的是捧书,不能使用get方式(這個方法知識返回一個引用而已),
//應該在獲取的同時,刪除掉這個鏈接骤星,之后再還回來.現(xiàn)在注意是返還給數據庫連接池>伞!洞难!
Connection conn = list.removeFirst();
MyConnection myconn = new MyConnection(conn);
//從這里開始返回的就是一個數據庫連接池對象的conn
return myconn;
}
/////////////////////////////////////////////////////////////////////////datasource接口的實現(xiàn)方法開始
@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;
}
/////////////////////////////////////////////////////////////////////////datasource接口的實現(xiàn)方法結束
/**
* 包裝設計模式實現(xiàn)流程:
* 1.創(chuàng)建一個類舆吮,實現(xiàn)與被增強對象相同的接口
* 2.將被增強對象當做自定義類的一個成員變量
* 3.定義一個構造方法,將被增強對象傳遞進去
* 4.增強想要增強的方法,進行覆蓋即可
* 5.對于不像被增強的方法歪泳,調用被增強對象的方法進行處理即可
* @author Summer
*
*/
///////////////////////////////////////////////////////////////////////使用包裝設計模式萝勤,增強close方法的自定義類開始
class MyConnection implements Connection {
private Connection conn ;
public MyConnection(Connection conn ){
this.conn = conn;
}
/**
* 自定義的包裝設計模式類,增強close方法呐伞,
* 將數據庫鏈接資源返還給數據庫連接池敌卓,而不是數據庫
*/
public void close(){
list.add(conn);
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return this.conn.unwrap(iface);
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return this.conn.isWrapperFor(iface);
}
@Override
public Statement createStatement() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStatement();
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql);
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareCall(sql);
}
@Override
public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return this.conn.nativeSQL(sql);
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub
this.conn.setAutoCommit(autoCommit);
}
@Override
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getAutoCommit();
}
@Override
public void commit() throws SQLException {
// TODO Auto-generated method stub
this.conn.commit();
}
@Override
public void rollback() throws SQLException {
// TODO Auto-generated method stub
this.conn.rollback();
}
@Override
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return this.conn.isClosed();
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getMetaData();
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub
this.conn.setReadOnly(readOnly);
}
@Override
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return this.conn.isReadOnly();
}
@Override
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub
this.conn.setCatalog(catalog);
}
@Override
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getCatalog();
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub
this.conn.setTransactionIsolation(level);
}
@Override
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getTransactionIsolation();
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getWarnings();
}
@Override
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
this.conn.clearWarnings();
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStatement(resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency);
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getTypeMap();
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub
this.conn.setTypeMap(map);
}
@Override
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub
this.conn.setHoldability(holdability);
}
@Override
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getHoldability();
}
@Override
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return this.conn.setSavepoint();
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return this.conn.setSavepoint(name);
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
this.conn.rollback(savepoint);
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
this.conn.releaseSavepoint(savepoint);
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, columnIndexes);
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return this.conn.prepareStatement(sql, columnNames);
}
@Override
public Clob createClob() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createClob();
}
@Override
public Blob createBlob() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createBlob();
}
@Override
public NClob createNClob() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createNClob();
}
@Override
public SQLXML createSQLXML() throws SQLException {
// TODO Auto-generated method stub
return this.conn.createSQLXML();
}
@Override
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
return this.conn.isValid(timeout);
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
// TODO Auto-generated method stub
this.conn.setClientInfo(name, value);
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
// TODO Auto-generated method stub
this.conn.setClientInfo(properties);
}
@Override
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return this.conn.getClientInfo(name);
}
@Override
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getClientInfo();
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
// TODO Auto-generated method stub
return this.conn.createArrayOf(typeName, elements);
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
// TODO Auto-generated method stub
return this.conn.createStruct(typeName, attributes);
}
@Override
public void setSchema(String schema) throws SQLException {
// TODO Auto-generated method stub
this.conn.setSchema(schema);
}
@Override
public String getSchema() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getSchema();
}
@Override
public void abort(Executor executor) throws SQLException {
// TODO Auto-generated method stub
this.conn.abort(executor);
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
// TODO Auto-generated method stub
this.conn.setNetworkTimeout(executor, milliseconds);
}
@Override
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return this.conn.getNetworkTimeout();
}
}
/////////////////////////////////////////////////////////////////////////////包裝設計模式結束
}
總結
- 在實際的開發(fā)過程中,數據庫連接池使用的很廣泛伶氢,我們應該加以掌握趟径。
- 數據庫連接對象應從數據庫連接池中獲取,使用完之后再返還給數據庫連接池
- 使用包裝設計模式進行方法的增強是較為合理的癣防,但是更為常用的是開源的數據庫連接池蜗巧,即動態(tài)代理方式。如DBCP等等