溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么配置使用HikariCP數(shù)據(jù)源

發(fā)布時間:2021-10-19 17:19:11 來源:億速云 閱讀:173 作者:iii 欄目:編程語言

這篇文章主要介紹“怎么配置使用HikariCP數(shù)據(jù)源”,在日常操作中,相信很多人在怎么配置使用HikariCP數(shù)據(jù)源問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么配置使用HikariCP數(shù)據(jù)源”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

使用場景

HikariCP 這個數(shù)據(jù)源號稱是最快的數(shù)據(jù)源。 其實個人認(rèn)為 性能肯定還是在SQL語句和數(shù)據(jù)庫。而且 這個數(shù)據(jù)源功能其實并不多。
個人不喜歡使用 將其作為主數(shù)據(jù)源。
druid 數(shù)據(jù)源對比

但是 如果將 HikariCP 作為讀取 讀取第三方 數(shù)據(jù)庫 也就是多數(shù)據(jù)源來 使用,個人認(rèn)為是非常適合的。

多數(shù)據(jù)源配置

package com.door.remote.dataSource;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.db.Db;
import com.door.common.constants.DbConst;
import com.door.common.constants.biz.dr.DbSetType;
import com.door.entity.dr.AcDrDbSource;
import com.door.utils.db.CfsDatabase;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 數(shù)據(jù)源管理器
 *
 * @author oumin
 * @date 2021/1/23 10:01
 */
@Component
@Log4j2
public class DataSourceManager {

  // 24 小時超時
  private static final long timeOut = 24 * 60 * 60 * 1000L;

  private final Object createLock = new Object();

  private static final TimedCache<String, DataSource> timedDataSourceCache =
      CacheUtil.newTimedCache(timeOut);

  @PostConstruct // 初始化方法的注解方式  等同與init-method=init
  public void init() {

    // 啟動定時任務(wù),每 xx 毫秒 檢查一次過期
    timedDataSourceCache.schedulePrune(timeOut);
  }

  /**
   * 獲取數(shù)據(jù)源
   *
   * @return
   */
  public DataSource getAndMaybePut(CfsDatabase cfsDatabase) {
    String key = cfsDatabase.toString();
    DataSource curr = timedDataSourceCache.get(key);
    if (curr == null) {
      synchronized (this.createLock) {
        curr = timedDataSourceCache.get(key);
        if (curr == null) {
          timedDataSourceCache.put(key, this.createDataSource(cfsDatabase));
        }
        return timedDataSourceCache.get(key);
      }
    } else {
      return curr;
    }
  }

  private DataSource createDataSource(CfsDatabase cfsDatabase) {
    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setDriverClassName(DbConst.getDbDriverByDbType(cfsDatabase.getDbType()));

    if (DbSetType.DB_MYSQL.getName().equalsIgnoreCase(cfsDatabase.getDbType())) {
      // 兼容 MYSQL8 時區(qū),和 指定連接超時  connectTimeout=20000  ,  和 不使用 SSL:    characterEncoding=utf- 8&useSSL=false&serverTimezone=Hongkong
      String timeZone = cfsDatabase.getTimeZone();
      String url = cfsDatabase.getUrl();
      String urlDb = "";
      if (StringUtils.isNoneBlank(timeZone)) {
        urlDb = DbConst.MYSQL_URL + url + DbConst.MYSQL_STR + timeZone + DbConst.MYSQL_TIME_OUT;
      } else {
        // 如果timeZone 為空,mysql就用默認(rèn)的時區(qū)
        urlDb = DbConst.MYSQL_URL + url + DbConst.MYSQL_STR_ZOOR + DbConst.MYSQL_TIME_OUT;
      }
      dataSource.setJdbcUrl(urlDb);
    } else {
      dataSource.setJdbcUrl(cfsDatabase.getUrl());
    }

    dataSource.setUsername(cfsDatabase.getUserName());
    dataSource.setPassword(cfsDatabase.getPassword());
    //    dataSource.setPassword(PasswordKey.getDecodePassword(source.getDatasourcePassword()));
    dataSource.setMaximumPoolSize(10);
    dataSource.setMinimumIdle(1);

    // 默認(rèn) 30S
    //    dataSource.setConnectionTimeout(source.getConnTimeout());

    // 修復(fù) 連接超時異常
    // 默認(rèn)false . 目前業(yè)務(wù)都是查詢而已
    //    dataSource.setReadOnly(true); 只讀模式,如果數(shù)據(jù)庫支持 只讀模式才有優(yōu)化效果,否則沒有

    // 單位都是毫秒以下的
    dataSource.setIdleTimeout(60 * 1000L);
    dataSource.setValidationTimeout(3 * 1000L);
    dataSource.setMaxLifetime(300 * 1000L);

    // mysql 和 Oracle 使用 SELECT 1 FROM DUAL  ,其他 數(shù)據(jù)庫 使用  select 1
    // 雖然 官網(wǎng) 不推薦配置這個 TestQuery 而是在數(shù)據(jù)庫支持JDBC4規(guī)范的情況下就不要配置。但是避免麻煩還是配置加上了
    dataSource.setConnectionTestQuery(DbConst.getTestSQL(cfsDatabase.getDbType()));
    dataSource.setPoolName("數(shù)據(jù)源ID" + cfsDatabase.getSourceId());
    return dataSource;
  }

  /**
   * 測試SQL
   *
   * @param sql 測試SQL
   * @param dbSource 測試數(shù)據(jù)源
   */
  public void testSQL(String sql, AcDrDbSource dbSource) throws SQLException {

    log.info("測試SQL :{}", sql);
    CfsDatabase database = CfsDatabase.converterByDataSource(dbSource);

    DataSource dataSource = this.getAndMaybePut(database);

    Db.use(dataSource).query(sql);
  }
}

到此,關(guān)于“怎么配置使用HikariCP數(shù)據(jù)源”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI