JDBC編程學(xué)習(xí)筆記之數(shù)據(jù)庫連接池的實現(xiàn)

在JDBC編程的時候,獲取到一個數(shù)據(jù)庫連接資源是很寶貴的,倘若數(shù)據(jù)庫訪問量超大,而數(shù)據(jù)庫連接資源又沒能得到及時的釋放,就會導(dǎo)致系統(tǒng)的崩潰甚至宕機。造成的損失將會是巨大的。再看有了數(shù)據(jù)庫連接池的JDBC,就會較好的解決資源的創(chuàng)建與連接問題,其主要還是針對于連接資源使用層面的改進。下面我就談一談我對數(shù)據(jù)庫連接池的理解。


數(shù)據(jù)庫連接池理論基礎(chǔ)


對于創(chuàng)建一個數(shù)據(jù)庫連接池,需要做好準備工作。原理就是先實現(xiàn)DataSource接口,覆蓋里面的getConnection()方法,這個方法是我們最為關(guān)注的。既然是池,就不會是一個連接對象,因此需要使用集合來保存這些個連接對象。
但是,最為關(guān)鍵的是,開發(fā)人員在使用完連接對象后通常會調(diào)用conn.close(),方法來釋放數(shù)據(jù)庫連接資源,這將會把數(shù)據(jù)庫連接返還給數(shù)據(jù)庫,而不是數(shù)據(jù)庫連接池,因此,數(shù)據(jù)庫連接池的存在就沒了意義了。所以我們要增強close方法,來保證數(shù)據(jù)庫連接資源返還給數(shù)據(jù)庫連接池。


創(chuàng)建數(shù)據(jù)庫連接池


public class JDCBCPool implements DataSource {

    /*
     * 既然是一個數(shù)據(jù)庫連接池,就必須有許多的連接,所以需要使用一個集合類保存這些連接 (non-Javadoc)
     * 
     * @see javax.sql.CommonDataSource#getLogWriter()
     */
    private static  LinkedList<Connection> list = new LinkedList<Connection>();

    // 創(chuàng)建一個配置文件,用于讀取相應(yīng)配置文件中保存的數(shù)據(jù)信息
    private static Properties config = new Properties();

    /*
     * 在這里向數(shù)據(jù)庫申請一批數(shù)據(jù)庫連接 為了只加載一次驅(qū)動程序,因此在靜態(tài)代碼塊中進行聲明,這樣驅(qū)動就只會加載一次
     */
    static {

        try {
            config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
            // Class.forName("com.mysql.jdbc.Driver");
            Class.forName(config.getProperty("DRIVER"));
            try {
                //申請十個數(shù)據(jù)庫連接對象,也就是數(shù)據(jù)庫連接池的容量是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("數(shù)據(jù)庫忙,請待會再試試吧!");
        }
        //需要注意的是,不能使用get方式(這個方法知識返回一個引用而已),
        //應(yīng)該在獲取的同時,刪除掉這個鏈接,之后再還回來.現(xiàn)在注意是返還給數(shù)據(jù)庫連接池?。?!
        Connection conn = list.removeFirst();
        MyConnection myconn = new MyConnection(conn);

        //從這里開始返回的就是一個數(shù)據(jù)庫連接池對象的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;
    }
}

不難看出,數(shù)據(jù)庫連接池相對于普通的數(shù)據(jù)庫連接的創(chuàng)建,并沒有什么特別難寫的部分。

增強close方法,保證數(shù)據(jù)庫連接資源用完后返還給連接池


要想增強close方法的功能,一般來說我們有如下幾種方式。

  • 創(chuàng)建子類,覆蓋close方法,但是創(chuàng)建子類的時候要實現(xiàn)的部分可能會特別多,因此并不常用
  • 使用包裝設(shè)計模式
  • 使用動態(tài)代理方式

今天我就來實現(xiàn)一下怎么使用包裝設(shè)計模式來實現(xiàn)close方法的增強。

包裝設(shè)計模式實現(xiàn)close方法的增強


要實現(xiàn)包裝設(shè)計模式,思路很簡單。

  • 創(chuàng)建一個類,實現(xiàn)與被增強對象相同的接口
  • 將被增強對象作為一個成員變量加到這個類中
  • 定義一個構(gòu)造方法,并把被增強對象作為參數(shù)傳進來
  • 覆蓋要增強的方法,這里是close方法
  • 對于不想進行增強的方法,借助于被增強對象實現(xiàn)即可。

下面是基于包裝設(shè)計模式實現(xiàn)的增強的MyConnection類:

class MyConnection implements Connection {

        private Connection conn ;
        
        public MyConnection(Connection conn ){
            this.conn = conn;
        }
        
        /**
         * 自定義的包裝設(shè)計模式類,增強close方法,
         * 將數(shù)據(jù)庫鏈接資源返還給數(shù)據(jù)庫連接池,而不是數(shù)據(jù)庫
         */
        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();
        }
        
        
        
    }

一個簡單的數(shù)據(jù)庫連接池的實現(xiàn)案例


說是一個案例,其實也可以作為一個工具類作為今后程序開發(fā)的使用,這將會大大的提升數(shù)據(jù)庫連接資源的使用。需要注意的是,里面的驅(qū)動程序是基于我自己的數(shù)據(jù)庫來書寫的,進行使用時記得修改一下配置文件db.properties.文件中的內(nèi)容即可。
db.properties中內(nèi)容如下:

#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;

/**
 * 數(shù)據(jù)庫連接池工具類
 * 
 * @author Summer
 *
 */
public class JDCBCPool implements DataSource {

    /*
     * 既然是一個數(shù)據(jù)庫連接池,就必須有許多的連接,所以需要使用一個集合類保存這些連接 (non-Javadoc)
     * 
     * @see javax.sql.CommonDataSource#getLogWriter()
     */
    private static  LinkedList<Connection> list = new LinkedList<Connection>();

    // 創(chuàng)建一個配置文件,用于讀取相應(yīng)配置文件中保存的數(shù)據(jù)信息
    private static Properties config = new Properties();

    /*
     * 在這里向數(shù)據(jù)庫申請一批數(shù)據(jù)庫連接 為了只加載一次驅(qū)動程序,因此在靜態(tài)代碼塊中進行聲明,這樣驅(qū)動就只會加載一次
     */
    static {

        try {
            config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
            // Class.forName("com.mysql.jdbc.Driver");
            Class.forName(config.getProperty("DRIVER"));
            try {
                //申請十個數(shù)據(jù)庫連接對象,也就是數(shù)據(jù)庫連接池的容量是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("數(shù)據(jù)庫忙,請待會再試試吧!");
        }
        //需要注意的是,不能使用get方式(這個方法知識返回一個引用而已),
        //應(yīng)該在獲取的同時,刪除掉這個鏈接,之后再還回來.現(xiàn)在注意是返還給數(shù)據(jù)庫連接池!??!
        Connection conn = list.removeFirst();
        MyConnection myconn = new MyConnection(conn);

        //從這里開始返回的就是一個數(shù)據(jù)庫連接池對象的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)方法結(jié)束
    
    /**
     * 包裝設(shè)計模式實現(xiàn)流程:
     * 1.創(chuàng)建一個類,實現(xiàn)與被增強對象相同的接口
     * 2.將被增強對象當做自定義類的一個成員變量
     * 3.定義一個構(gòu)造方法,將被增強對象傳遞進去
     * 4.增強想要增強的方法,進行覆蓋即可
     * 5.對于不像被增強的方法,調(diào)用被增強對象的方法進行處理即可
     * @author Summer
     *
     */
    
///////////////////////////////////////////////////////////////////////使用包裝設(shè)計模式,增強close方法的自定義類開始
    class MyConnection implements Connection {

        private Connection conn ;
        
        public MyConnection(Connection conn ){
            this.conn = conn;
        }
        
        /**
         * 自定義的包裝設(shè)計模式類,增強close方法,
         * 將數(shù)據(jù)庫鏈接資源返還給數(shù)據(jù)庫連接池,而不是數(shù)據(jù)庫
         */
        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();
        }
        
        
        
    }
/////////////////////////////////////////////////////////////////////////////包裝設(shè)計模式結(jié)束
}


總結(jié)


  • 在實際的開發(fā)過程中,數(shù)據(jù)庫連接池使用的很廣泛,我們應(yīng)該加以掌握。
  • 數(shù)據(jù)庫連接對象應(yīng)從數(shù)據(jù)庫連接池中獲取,使用完之后再返還給數(shù)據(jù)庫連接池
  • 使用包裝設(shè)計模式進行方法的增強是較為合理的,但是更為常用的是開源的數(shù)據(jù)庫連接池,即動態(tài)代理方式。如DBCP等等
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容