欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

JDBC自定義連接池的示例分析

這篇文章主要為大家展示了“JDBC自定義連接池的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JDBC自定義連接池的示例分析”這篇文章吧。

創(chuàng)新互聯(lián)專注于長葛網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供長葛營銷型網(wǎng)站建設(shè),長葛網(wǎng)站制作、長葛網(wǎng)頁設(shè)計、長葛網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造長葛網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供長葛網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

開發(fā)中,"獲得連接"和"釋放資源"是非常消耗系統(tǒng)資源的,為了解決此類性能問題可以采用連接池技術(shù)來共享連接Connection。

1、概述

用池來管理Connection,這樣可以重復(fù)使用Connection.這樣我們就不用創(chuàng)建Connection,用池來管理Connection對象,當(dāng)使用完Connection對象后,將Connection對象歸還給池,這樣后續(xù)還可以從池中獲取Connection對象,可以重新再利用這個連接對象啦。

java為數(shù)據(jù)庫連接池提供了公共接口:javax.sql.DataSource,各個廠商需要讓自己的連接池實現(xiàn)這個接口。
常見的連接池:DBCP,C3P0

2、自定義連接池

編寫自定義連接池

1、創(chuàng)建連接池并實現(xiàn)接口javax.sql.DataSource,并使用接口中的getConnection()方法

2、提供一個集合,用于存放連接,可以采用LinkedList

3、后面程序如果需要,可以調(diào)用實現(xiàn)類getConnection(),并從list中獲取鏈接。為保證當(dāng)前連接只能提供給一個線程使用,所以我們需要將連接先從連接池中移除

4、當(dāng)用戶用完連接后,將連接歸還到連接池中

3、自定義連接池采用裝飾者設(shè)計模式

public class ConnectionPool implements Connection {

  private Connection connection;
  private LinkedList<Connection> pool;

  public ConnectionPool(Connection connection, LinkedList<Connection> pool){
    this.connection=connection;
    this.pool=pool;
  }

  @Override
  public PreparedStatement prepareStatement(String sql) throws SQLException {
    return connection.prepareStatement(sql);
  }

  @Override
  public void close() throws SQLException {
    pool.add(connection);

  }
  @Override
  public Statement createStatement() throws SQLException {
    return null;
  }



  @Override
  public CallableStatement prepareCall(String sql) throws SQLException {
    return null;
  }

  @Override
  public String nativeSQL(String sql) throws SQLException {
    return null;
  }

  @Override
  public void setAutoCommit(boolean autoCommit) throws SQLException {

  }

  @Override
  public boolean getAutoCommit() throws SQLException {
    return false;
  }

  @Override
  public void commit() throws SQLException {

  }

  @Override
  public void rollback() throws SQLException {

  }



  @Override
  public boolean isClosed() throws SQLException {
    return false;
  }

  @Override
  public DatabaseMetaData getMetaData() throws SQLException {
    return null;
  }

  @Override
  public void setReadOnly(boolean readOnly) throws SQLException {

  }

  @Override
  public boolean isReadOnly() throws SQLException {
    return false;
  }

  @Override
  public void setCatalog(String catalog) throws SQLException {

  }

  @Override
  public String getCatalog() throws SQLException {
    return null;
  }

  @Override
  public void setTransactionIsolation(int level) throws SQLException {

  }

  @Override
  public int getTransactionIsolation() throws SQLException {
    return 0;
  }

  @Override
  public SQLWarning getWarnings() throws SQLException {
    return null;
  }

  @Override
  public void clearWarnings() throws SQLException {

  }

  @Override
  public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
    return null;
  }

  @Override
  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
    return null;
  }

  @Override
  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
    return null;
  }

  @Override
  public Map<String, Class<?>> getTypeMap() throws SQLException {
    return null;
  }

  @Override
  public void setTypeMap(Map<String, Class<?>> map) throws SQLException {

  }

  @Override
  public void setHoldability(int holdability) throws SQLException {

  }

  @Override
  public int getHoldability() throws SQLException {
    return 0;
  }

  @Override
  public Savepoint setSavepoint() throws SQLException {
    return null;
  }

  @Override
  public Savepoint setSavepoint(String name) throws SQLException {
    return null;
  }

  @Override
  public void rollback(Savepoint savepoint) throws SQLException {

  }

  @Override
  public void releaseSavepoint(Savepoint savepoint) throws SQLException {

  }

  @Override
  public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    return null;
  }

  @Override
  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    return null;
  }

  @Override
  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
    return null;
  }

  @Override
  public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
    return null;
  }

  @Override
  public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
    return null;
  }

  @Override
  public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
    return null;
  }

  @Override
  public Clob createClob() throws SQLException {
    return null;
  }

  @Override
  public Blob createBlob() throws SQLException {
    return null;
  }

  @Override
  public NClob createNClob() throws SQLException {
    return null;
  }

  @Override
  public SQLXML createSQLXML() throws SQLException {
    return null;
  }

  @Override
  public boolean isValid(int timeout) throws SQLException {
    return false;
  }

  @Override
  public void setClientInfo(String name, String value) throws SQLClientInfoException {

  }

  @Override
  public void setClientInfo(Properties properties) throws SQLClientInfoException {

  }

  @Override
  public String getClientInfo(String name) throws SQLException {
    return null;
  }

  @Override
  public Properties getClientInfo() throws SQLException {
    return null;
  }

  @Override
  public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
    return null;
  }

  @Override
  public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
    return null;
  }

  @Override
  public void setSchema(String schema) throws SQLException {

  }

  @Override
  public String getSchema() throws SQLException {
    return null;
  }

  @Override
  public void abort(Executor executor) throws SQLException {

  }

  @Override
  public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {

  }

  @Override
  public int getNetworkTimeout() throws SQLException {
    return 0;
  }

  @Override
  public <T> T unwrap(Class<T> iface) throws SQLException {
    return null;
  }

  @Override
  public boolean isWrapperFor(Class<?> iface) throws SQLException {
    return false;
  }
}

DataSourcePool

public class DataSourcePool implements DataSource {
  //1.創(chuàng)建1個容器用于存儲Connection對象
  private static LinkedList<Connection> pool = new LinkedList<Connection>();

  //2.創(chuàng)建5個連接放到容器中去
  static{
    for (int i = 0; i < 5; i++) {
      Connection conn = JDBCUtils.getConnection();
      //放入池子中connection對象已經(jīng)經(jīng)過改造了
      ConnectionPool connectionPool = new ConnectionPool(conn, pool);
      pool.add(connectionPool);
    }
  }

  /**
   * 重寫獲取連接的方法
   */
  @Override
  public Connection getConnection() throws SQLException {
    Connection conn = null;
    //3.使用前先判斷
    if(pool.size()==0){
      //4.池子里面沒有,我們再創(chuàng)建一些
      for (int i = 0; i < 5; i++) {
        conn = JDBCUtils.getConnection();
        //放入池子中connection對象已經(jīng)經(jīng)過改造了
        ConnectionPool connectionPool = new ConnectionPool(conn, pool);
        pool.add(connectionPool);
      }
    }
    //5.從池子里面獲取一個連接對象Connection
    conn = pool.remove(0);
    return conn;
  }


  @Override
  public Connection getConnection(String username, String password) throws SQLException {
    return null;
  }

  @Override
  public <T> T unwrap(Class<T> iface) throws SQLException {
    return null;
  }

  @Override
  public boolean isWrapperFor(Class<?> iface) throws SQLException {
    return false;
  }

  @Override
  public PrintWriter getLogWriter() throws SQLException {
    return null;
  }

  @Override
  public void setLogWriter(PrintWriter out) throws SQLException {

  }

  @Override
  public void setLoginTimeout(int seconds) throws SQLException {

  }

  @Override
  public int getLoginTimeout() throws SQLException {
    return 0;
  }

  @Override
  public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    return null;
  }
}

測試代碼如下

 @Test
  public void test1(){
    Connection conn = null;
    PreparedStatement pstmt = null;
    // 1.創(chuàng)建自定義連接池對象
    DataSource dataSource = new DataSourcePool();
    try {
      // 2.從池子中獲取連接
      conn = dataSource.getConnection();
      String sql = "insert into USER values(?,?)";
      //3.必須在自定義的connection類中重寫prepareStatement(sql)方法
      pstmt = conn.prepareStatement(sql);
      pstmt.setString(1, "李四");
      pstmt.setString(2, "1234");
      int rows = pstmt.executeUpdate();
      System.out.println("rows:"+rows);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      JDBCUtils.relase(conn, pstmt, null);
    }
  }

以上是“JDBC自定義連接池的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前標(biāo)題:JDBC自定義連接池的示例分析
當(dāng)前地址:http://aaarwkj.com/article16/iidodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化網(wǎng)站設(shè)計公司、品牌網(wǎng)站設(shè)計品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)
国产日韩精品一区二区三区在线| 人妻少妇精品一区毛二区| 亚洲91无专砖码高清观看| 国产精品 亚洲精品| 久久综合午夜福利视频| 国产精品黄色片在线观看 | 在线免费观看欧美黄片| 欧美成人高清在线播放| 国产丝袜肉丝在线播放| 国产日韩亚洲欧美色片| 老牛av一区二区三区| 四虎精品视频在线播放| 欧美日韩国产另类在线视频| 国产九色av在线一区尤物| 亚洲综合中文字幕精品| 国产性色精品一区二区| 高潮少妇水多毛多av| 加勒比人妻一区二区三区| 免费av在线观看日韩| av电影在线中文字幕| av中文字幕一二三区| 人妻少妇一区二区三区四区| 久久精品国产亚洲av高清观看| 国产精品日本一区二区| 啊啊舒服爽用力爱我视频| 日韩精品一区二区国产 | 国产夫妻性生活视频播放| 国产日韩传媒在线观看| 97久久精品国产成人影院| 欧美日韩精品成人大片| 一区二区不卡日韩av| 日韩精品一区二区在线天天狠天| 久久偷拍女生厕所尿尿| 久久热在线观看免费高清| 亚洲精品色婷婷一区二区| 日韩50岁老女人骚色| 亚洲国产精品视频中文字幕| 女人的天堂亚洲的天堂欧美| 成人欧美黑人一区二区| 黄片视频免费在线播放大全| 欧美精品欧美精品一区二区 |