温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么配置使用HikariCP数据源

发布时间:2021-10-19 17:19:11 来源:亿速云 阅读:160 作者:iii 栏目:编程语言

这篇文章主要介绍“怎么配置使用HikariCP数据源”,在日常操作中,相信很多人在怎么配置使用HikariCP数据源问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么配置使用HikariCP数据源”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

使用场景

HikariCP 这个数据源号称是最快的数据源。 其实个人认为 性能肯定还是在SQL语句和数据库。而且 这个数据源功能其实并不多。
个人不喜欢使用 将其作为主数据源。
druid 数据源对比

但是 如果将 HikariCP 作为读取 读取第三方 数据库 也就是多数据源来 使用,个人认为是非常适合的。

多数据源配置

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;

/**
 * 数据源管理器
 *
 * @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() {

    // 启动定时任务,每 xx 毫秒 检查一次过期
    timedDataSourceCache.schedulePrune(timeOut);
  }

  /**
   * 获取数据源
   *
   * @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 时区,和 指定连接超时  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就用默认的时区
        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);

    // 默认 30S
    //    dataSource.setConnectionTimeout(source.getConnTimeout());

    // 修复 连接超时异常
    // 默认false . 目前业务都是查询而已
    //    dataSource.setReadOnly(true); 只读模式,如果数据库支持 只读模式才有优化效果,否则没有

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

    // mysql 和 Oracle 使用 SELECT 1 FROM DUAL  ,其他 数据库 使用  select 1
    // 虽然 官网 不推荐配置这个 TestQuery 而是在数据库支持JDBC4规范的情况下就不要配置。但是避免麻烦还是配置加上了
    dataSource.setConnectionTestQuery(DbConst.getTestSQL(cfsDatabase.getDbType()));
    dataSource.setPoolName("数据源ID" + cfsDatabase.getSourceId());
    return dataSource;
  }

  /**
   * 测试SQL
   *
   * @param sql 测试SQL
   * @param dbSource 测试数据源
   */
  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);
  }
}

到此,关于“怎么配置使用HikariCP数据源”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI