springboot2.0默认数据库连接池

springboot2.0默认数据库连接池是hikari

hikari

hikari的默认配置

HikariCP-2.7.6-sources.jar!/com/zaxxer/hikari/HikariConfig.java

@SuppressWarnings({"SameParameterValue", "unused"})
public class HikariConfig implements HikariConfigMXBean
{
   private static final Logger LOGGER = LoggerFactory.getLogger(HikariConfig.class);

   private static final char[] ID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
   private static final long CONNECTION_TIMEOUT = SECONDS.toMillis(30);
   private static final long VALIDATION_TIMEOUT = SECONDS.toMillis(5);
   private static final long IDLE_TIMEOUT = MINUTES.toMillis(10);
   private static final long MAX_LIFETIME = MINUTES.toMillis(30);
   private static final int DEFAULT_POOL_SIZE = 10;
   //......

   /**
    * Default constructor
    */
   public HikariConfig()
   {
      dataSourceProperties = new Properties();
      healthCheckProperties = new Properties();

      minIdle = -1;
      maxPoolSize = -1;
      maxLifetime = MAX_LIFETIME;
      connectionTimeout = CONNECTION_TIMEOUT;
      validationTimeout = VALIDATION_TIMEOUT;
      idleTimeout = IDLE_TIMEOUT;
      initializationFailTimeout = 1;
      isAutoCommit = true;

      String systemProp = System.getProperty("hikaricp.configurationFile");
      if (systemProp != null) {
         loadProperties(systemProp);
      }
   }

   @Override
   public void setConnectionTimeout(long connectionTimeoutMs)
   {
      if (sealed) throw new IllegalStateException("The configuration of the pool is sealed once started.  Use HikariConfigMXBean for runtime changes.");

      if (connectionTimeoutMs == 0) {
         this.connectionTimeout = Integer.MAX_VALUE;
      }
      else if (connectionTimeoutMs < 250) {
         throw new IllegalArgumentException("connectionTimeout cannot be less than 250ms");
      }
      else {
         this.connectionTimeout = connectionTimeoutMs;
      }
   }

   @Override
   public void setIdleTimeout(long idleTimeoutMs)
   {
      if (sealed) throw new IllegalStateException("The configuration of the pool is sealed once started.  Use HikariConfigMXBean for runtime changes.");

      if (idleTimeoutMs < 0) {
         throw new IllegalArgumentException("idleTimeout cannot be negative");
      }
      this.idleTimeout = idleTimeoutMs;
   }

   @Override
   public void setMaximumPoolSize(int maxPoolSize)
   {
      if (sealed) throw new IllegalStateException("The configuration of the pool is sealed once started.  Use HikariConfigMXBean for runtime changes.");

      if (maxPoolSize < 1) {
         throw new IllegalArgumentException("maxPoolSize cannot be less than 1");
      }
      this.maxPoolSize = maxPoolSize;
   }

   @Override
   public void setMinimumIdle(int minIdle)
   {
      if (sealed) throw new IllegalStateException("The configuration of the pool is sealed once started.  Use HikariConfigMXBean for runtime changes.");

      if (minIdle < 0) {
         throw new IllegalArgumentException("minimumIdle cannot be negative");
      }
      this.minIdle = minIdle;
   }

   @Override
   public void setValidationTimeout(long validationTimeoutMs)
   {
      if (sealed) throw new IllegalStateException("The configuration of the pool is sealed once started.  Use HikariConfigMXBean for runtime changes.");

      if (validationTimeoutMs < 250) {
         throw new IllegalArgumentException("validationTimeout cannot be less than 250ms");
      }

      this.validationTimeout = validationTimeoutMs;
   }

   public void validate()
   {
      if (poolName == null) {
         poolName = generatePoolName();
      }
      else if (isRegisterMbeans && poolName.contains(":")) {
         throw new IllegalArgumentException("poolName cannot contain ':' when used with JMX");
      }

      // treat empty property as null
      catalog = getNullIfEmpty(catalog);
      connectionInitSql = getNullIfEmpty(connectionInitSql);
      connectionTestQuery = getNullIfEmpty(connectionTestQuery);
      transactionIsolationName = getNullIfEmpty(transactionIsolationName);
      dataSourceClassName = getNullIfEmpty(dataSourceClassName);
      dataSourceJndiName = getNullIfEmpty(dataSourceJndiName);
      driverClassName = getNullIfEmpty(driverClassName);
      jdbcUrl = getNullIfEmpty(jdbcUrl);

      // Check Data Source Options
      if (dataSource != null) {
         if (dataSourceClassName != null) {
            LOGGER.warn("{} - using dataSource and ignoring dataSourceClassName.", poolName);
         }
      }
      else if (dataSourceClassName != null) {
         if (driverClassName != null) {
            LOGGER.error("{} - cannot use driverClassName and dataSourceClassName together.", poolName);
            // NOTE: This exception text is referenced by a Spring Boot FailureAnalyzer, it should not be
            // changed without first notifying the Spring Boot developers.
            throw new IllegalStateException("cannot use driverClassName and dataSourceClassName together.");
         }
         else if (jdbcUrl != null) {
            LOGGER.warn("{} - using dataSourceClassName and ignoring jdbcUrl.", poolName);
         }
      }
      else if (jdbcUrl != null || dataSourceJndiName != null) {
         // ok
      }
      else if (driverClassName != null) {
         LOGGER.error("{} - jdbcUrl is required with driverClassName.", poolName);
         throw new IllegalArgumentException("jdbcUrl is required with driverClassName.");
      }
      else {
         LOGGER.error("{} - dataSource or dataSourceClassName or jdbcUrl is required.", poolName);
         throw new IllegalArgumentException("dataSource or dataSourceClassName or jdbcUrl is required.");
      }

      validateNumerics();

      if (LOGGER.isDebugEnabled() || unitTest) {
         logConfiguration();
      }
   }

   private void validateNumerics()
   {
      if (maxLifetime != 0 && maxLifetime < SECONDS.toMillis(30)) {
         LOGGER.warn("{} - maxLifetime is less than 30000ms, setting to default {}ms.", poolName, MAX_LIFETIME);
         maxLifetime = MAX_LIFETIME;
      }

      if (idleTimeout + SECONDS.toMillis(1) > maxLifetime && maxLifetime > 0) {
         LOGGER.warn("{} - idleTimeout is close to or more than maxLifetime, disabling it.", poolName);
         idleTimeout = 0;
      }

      if (idleTimeout != 0 && idleTimeout < SECONDS.toMillis(10)) {
         LOGGER.warn("{} - idleTimeout is less than 10000ms, setting to default {}ms.", poolName, IDLE_TIMEOUT);
         idleTimeout = IDLE_TIMEOUT;
      }

      if (leakDetectionThreshold > 0 && !unitTest) {
         if (leakDetectionThreshold < SECONDS.toMillis(2) || (leakDetectionThreshold > maxLifetime && maxLifetime > 0)) {
            LOGGER.warn("{} - leakDetectionThreshold is less than 2000ms or more than maxLifetime, disabling it.", poolName);
            leakDetectionThreshold = 0;
         }
      }

      if (connectionTimeout < 250) {
         LOGGER.warn("{} - connectionTimeout is less than 250ms, setting to {}ms.", poolName, CONNECTION_TIMEOUT);
         connectionTimeout = CONNECTION_TIMEOUT;
      }

      if (validationTimeout < 250) {
         LOGGER.warn("{} - validationTimeout is less than 250ms, setting to {}ms.", poolName, VALIDATION_TIMEOUT);
         validationTimeout = VALIDATION_TIMEOUT;
      }

      if (maxPoolSize < 1) {
         maxPoolSize = (minIdle <= 0) ? DEFAULT_POOL_SIZE : minIdle;
      }

      if (minIdle < 0 || minIdle > maxPoolSize) {
         minIdle = maxPoolSize;
      }
   }
}   

springboot的HikariDataSource默认配置的默认值如下

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误信息提示缺少jdbcurl和driverclassname参数。在使用JDBC(Java数据库连接)连接数据库时,必须提供这两个参数。 jdbcurl参数是指数据库的URL,它描述了连接数据库所需的所有信息,如主机名、端口号、数据库名称、用户名和密码等。 driverclassname参数是指数据库驱动程序的类名,它是用于连接特定类型的数据库的类。 因此,如果出现"jdbcurl is required with driverclassname"错误提示,需要检查程序中是否已正确设置了这两个参数。例如,如果使用MySQL数据库,应该设置如下: ``` String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase"; String driverClassName = "com.mysql.jdbc.Driver"; ``` 其中,jdbcUrl是连接MySQL数据库的URL,driverClassName是MySQL数据库的驱动程序类名。 希望这可以帮助您解决问题! ### 回答2: jdbcurl 是在使用 driverclassname 时必需的。JDBC 是一种标准的数据库访问方式,通过 JDBC 驱动可以连接到数据库并进行数据操作。在使用 JDBC 连接数据库时,需要提供 JDBC URL 和 JDBC Driver 的类名。 JDBC URL 是用于标识数据库的连接地址,它包含了数据库的类型、地址、端口、以及其他必要的参数。通过 JDBC URL,我们能够访问到具体的数据库,并进行数据操作。 而 JDBC Driver 的类名是实现了 JDBC 接口规范的驱动程序提供商所提供的类名。这个类名会根据不同的数据库和不同的驱动程序而有所区别。在使用 JDBC 连接数据库时,需要指定具体的 JDBC Driver 类名,这样才能正确地加载驱动程序并与数据库建立连接。 因此,当我们在使用 JDBC 连接数据库时,必须提供正确的 JDBC URL 和对应的 JDBC Driver 类名。如果没有提供 JDBC URL,或者是缺少了对应的 JDBC Driver 类名,就会出现异常提醒"jdbcurl is required with driverclassname",表示需提供 JDBC URL 和 JDBC Driver 的类名。这是因为在没有正确指定连接地址和驱动程序类名的情况下,JDBC 无法知道要连接的数据库是哪个,也无法正确加载对应的数据库驱动程序。 因此,正确设置 JDBC URL 和对应的 JDBC Driver 类名是使用 JDBC 连接数据库时的必要步骤。只有提供了这两个参数,才能正确地建立数据库连接并进行后续的数据操作。 ### 回答3: jdbcurl is required with driverclassname是指在使用JDBC连接数据库时,需要指定数据库驱动程序的类名(driverclassname)以及JDBC连接的URL(jdbcurl)。 在使用JDBC连接数据库时,首先需要加载相应的数据库驱动程序,驱动程序的类名需要通过参数driverclassname指定。每个数据库厂商都提供了自己的数据库驱动程序,如MySQL的驱动程序类名为com.mysql.jdbc.Driver,Oracle的驱动程序类名为oracle.jdbc.OracleDriver等。 而jdbcurl是指JDBC连接数据库时所使用的URL,它包含了一系列的参数以及数据库的连接信息。不同的数据库有不同的URL格式,如MySQL的URL格式为jdbc:mysql://host:port/database,Oracle的URL格式为jdbc:oracle:thin:@host:port:SID等。 在使用JDBC连接数据库时,必须同时指定驱动程序的类名(driverclassname)和JDBC连接的URL(jdbcurl),否则就会出现“jdbcurl is required with driverclassname”的错误提示。 例如,如果要使用MySQL数据库,驱动程序类名为com.mysql.jdbc.Driver,数据库的URL为jdbc:mysql://localhost:3306/test,那么在连接数据库时,需要这样编写代码: Class.forName("com.mysql.jdbc.Driver"); String jdbcUrl = "jdbc:mysql://localhost:3306/test"; Connection connection = DriverManager.getConnection(jdbcUrl, username, password); 其中,Class.forName("com.mysql.jdbc.Driver")用于加载MySQL数据库的驱动程序;getConnection方法用于建立与数据库的连接,其中的jdbcUrl就是JDBC连接的URL。 总之,当在使用JDBC连接数据库时,必须同时提供驱动程序的类名(driverclassname)和JDBC连接的URL(jdbcurl),否则就会出现“jdbcurl is required with driverclassname”的错误提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值