JDBC編程學習筆記之數據庫連接池的實現(xiàn)

在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等等
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末蕾盯,一起剝皮案震驚了整個濱河市幕屹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌级遭,老刑警劉巖望拖,帶你破解...
    沈念sama閱讀 207,248評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異挫鸽,居然都是意外死亡说敏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評論 2 381
  • 文/潘曉璐 我一進店門丢郊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盔沫,“玉大人,你說我怎么就攤上這事枫匾〖艿” “怎么了?”我有些...
    開封第一講書人閱讀 153,443評論 0 344
  • 文/不壞的土叔 我叫張陵干茉,是天一觀的道長侈贷。 經常有香客問我,道長等脂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,475評論 1 279
  • 正文 為了忘掉前任撑蚌,我火速辦了婚禮上遥,結果婚禮上,老公的妹妹穿的比我還像新娘争涌。我一直安慰自己粉楚,他們只是感情好,可當我...
    茶點故事閱讀 64,458評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著模软,像睡著了一般伟骨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上燃异,一...
    開封第一講書人閱讀 49,185評論 1 284
  • 那天携狭,我揣著相機與錄音,去河邊找鬼回俐。 笑死逛腿,一個胖子當著我的面吹牛,可吹牛的內容都是我干的仅颇。 我是一名探鬼主播单默,決...
    沈念sama閱讀 38,451評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼忘瓦!你這毒婦竟也來了搁廓?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,112評論 0 261
  • 序言:老撾萬榮一對情侶失蹤耕皮,失蹤者是張志新(化名)和其女友劉穎境蜕,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體明场,經...
    沈念sama閱讀 43,609評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡汽摹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,083評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了苦锨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逼泣。...
    茶點故事閱讀 38,163評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖舟舒,靈堂內的尸體忽然破棺而出拉庶,到底是詐尸還是另有隱情,我是刑警寧澤秃励,帶...
    沈念sama閱讀 33,803評論 4 323
  • 正文 年R本政府宣布氏仗,位于F島的核電站,受9級特大地震影響夺鲜,放射性物質發(fā)生泄漏皆尔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,357評論 3 307
  • 文/蒙蒙 一币励、第九天 我趴在偏房一處隱蔽的房頂上張望慷蠕。 院中可真熱鬧,春花似錦食呻、人聲如沸流炕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,357評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽每辟。三九已至剑辫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間渠欺,已是汗流浹背妹蔽。 一陣腳步聲響...
    開封第一講書人閱讀 31,590評論 1 261
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留峻堰,地道東北人讹开。 一個月前我還...
    沈念sama閱讀 45,636評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像捐名,于是被迫代替她去往敵國和親旦万。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,925評論 2 344

推薦閱讀更多精彩內容

  • JDBC概述 在Java中镶蹋,數據庫存取技術可分為如下幾類:JDBC直接訪問數據庫成艘、JDO技術、第三方O/R工具贺归,如...
    usopp閱讀 3,530評論 3 75
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理淆两,服務發(fā)現(xiàn),斷路器拂酣,智...
    卡卡羅2017閱讀 134,601評論 18 139
  • 本文包括傳統(tǒng)JDBC的缺點連接池原理自定義連接池開源數據庫連接池DBCP連接池C3P0連接池Tomcat內置連接池...
    廖少少閱讀 16,727評論 0 37
  • 對于一個簡單的數據庫應用秋冰,由于對于數據庫的訪問不是很頻繁。這時可以簡單地在需要訪問數據庫時婶熬,就新創(chuàng)建一個連接剑勾,用完...
    奇哥威武閱讀 1,114評論 0 8
  • 7月2日虽另,在線上參加了永澄老師在舉行2017年的《鉆石行動——半年計劃制定》活動,看活動報名分布饺谬,我還是新疆三人之...
    點滴中成長閱讀 351評論 0 10