Configuration (knobs, baby!)
HikariCP comes with sane defaults that perform well in most deployments without additional tweaking. Every property is optional, except for the "essentials" marked below.
HikariCP uses milliseconds for all time values.
HikariCP relies on accurate timers for both performance and reliability. It is imperative that your server is synchronized with a time-source such as an NTP server. Especially if your server is running within a virtual machine. Why? Read more here. Do not rely on hypervisor settings to "synchronize" the clock of the virtual machine. Configure time-source synchronization inside the virtual machine. If you come asking for support on an issue that turns out to be caused by lack time synchronization, you will be taunted publicly on Twitter.
Essentials
dataSourceClassName
This is the name of the DataSource
class provided by the JDBC driver. Consult the documentation for your specific JDBC driver to get this class name, or see the table below. Note XA data sources are not supported. XA requires a real transaction manager like bitronix. Note that you do not need this property if you are using jdbcUrl
for "old-school" DriverManager-based JDBC driver configuration. Default: none
- or -
jdbcUrl
This property directs HikariCP to use "DriverManager-based" configuration. We feel that DataSource-based configuration (above) is superior for a variety of reasons (see below), but for many deployments there is little significant difference. When using this property with "old" drivers, you may also need to set the driverClassName
property, but try it first without. Note that if this property is used, you may still use DataSource properties to configure your driver and is in fact recommended over driver parameters specified in the URL itself. Default: none
username
This property sets the default authentication username used when obtaining Connections from the underlying driver. Note that for DataSources this works in a very deterministic fashion by calling DataSource.getConnection(*username*, password)
on the underlying DataSource. However, for Driver-based configurations, every driver is different. In the case of Driver-based, HikariCP will use this username
property to set a user
property in the Properties
passed to the driver's DriverManager.getConnection(jdbcUrl, props)
call. If this is not what you need, skip this method entirely and call addDataSourceProperty("username", ...)
, for example. Default: none
password
This property sets the default authentication password used when obtaining Connections from the underlying driver. Note that for DataSources this works in a very deterministic fashion by calling DataSource.getConnection(username, *password*)
on the underlying DataSource. However, for Driver-based configurations, every driver is different. In the case of Driver-based, HikariCP will use this password
property to set a password
property in the Properties
passed to the driver's DriverManager.getConnection(jdbcUrl, props)
call. If this is not what you need, skip this method entirely and call addDataSourceProperty("pass", ...)
, for example. Default: none
Frequently used
autoCommit
This property controls the default auto-commit behavior of connections returned from the pool. It is a boolean value. Default: true
⌚connectionTimeout
This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
客户端等待连接池中的时间,如果超过这个参数设置的时间还没有可用的时间就会抛错SQLException。这个时间最小不能小于250毫秒。默认的是30000毫秒(30秒)
⌚idleTimeout
This property controls the maximum amount of time that a connection is allowed to sit idle in the pool. This setting only applies when minimumIdle
is defined to be less than maximumPoolSize
. Idle connections will not be retired once the pool reaches minimumIdle
connections. Whether a connection is retired as idle or not is subject to a maximum variation of +30 seconds, and average variation of +15 seconds. A connection will never be retired as idle before this timeout. A value of 0 means that idle connections are never removed from the pool. The minimum allowed value is 10000ms (10 seconds). Default: 600000 (10 minutes)
这个属性是控制连接池中连接在空闲状态下的最大时间存活时间。但是这个设置尽在minimumIdle
小于maximumPoolSize 的情况下才生效。如果连接池中的空闲连接达到了minimumIdle数量时,空闲连接将不会被销毁。无论连接被当成空闲连接被销毁,xxx这不理解。一个连接将不会被销毁在它超时之前。0的意思是空闲连接永远不会被销毁。设置的最小值是10秒,默认是10分钟。
⌚maxLifetime
This property controls the maximum lifetime of a connection in the pool. An in-use connection will never be retired, only when it is closed will it then be removed. On a connection-by-connection basis, minor negative attenuation is applied to avoid mass-extinction in the pool. We strongly recommend setting this value, and it should be several seconds shorter than any database or infrastructure imposed connection time limit. A value of 0 indicates no maximum lifetime (infinite lifetime), subject of course to the idleTimeout
setting. Default: 1800000 (30 minutes)
这个属性控制连接池中的连接最大生存时间,一个被使用的连接永远不会被销毁,只有在它关闭的时候他才会被销毁。在一个连接一个连接的基础上,小的负衰减用于避免池内的大灭绝。我们强烈建议设置这个值,而且它应该比任何数据库或基础设施所规定的连接时间限制短几秒.值0表示没有最大生存期(无限生存期),当然这取决于idleTimeout设置。默认值:1800000(30分钟)
connectionTestQuery
If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that do not support the JDBC4 Connection.isValid() API
. This is the query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive. Again, try running the pool without this property, HikariCP will log an error if your driver is not JDBC4 compliant to let you know. Default: none
minimumIdle
This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. If the idle connections dip below this value and total connections in the pool are less than maximumPoolSize
, HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSize
maximumPoolSize
This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend. A reasonable value for this is best determined by your execution environment. When the pool reaches this size, and no idle connections are available, calls to getConnection() will block for up to connectionTimeout
milliseconds before timing out. Please read about pool sizing. Default: 10
metricRegistry
This property is only available via programmatic configuration or IoC container. This property allows you to specify an instance of a Codahale/Dropwizard MetricRegistry
to be used by the pool to record various metrics. See the Metrics wiki page for details. Default: none
healthCheckRegistry
This property is only available via programmatic configuration or IoC container. This property allows you to specify an instance of a Codahale/Dropwizard HealthCheckRegistry
to be used by the pool to report current health information. See the Health Checks wiki page for details. Default: none
poolName
This property represents a user-defined name for the connection pool and appears mainly in logging and JMX management consoles to identify pools and pool configurations. Default: auto-generated
Infrequently used
⌚initializationFailTimeout
This property controls whether the pool will "fail fast" if the pool cannot be seeded with an initial connection successfully. Any positive number is taken to be the number of milliseconds to attempt to acquire an initial connection; the application thread will be blocked during this period. If a connection cannot be acquired before this timeout occurs, an exception will be thrown. This timeout is applied after the connectionTimeout
period. If the value is zero (0), HikariCP will attempt to obtain and validate a connection. If a connection is obtained, but fails validation, an exception will be thrown and the pool not started. However, if a connection cannot be obtained, the pool will start, but later efforts to obtain a connection may fail. A value less than zero will bypass any initial connection attempt, and the pool will start immediately while trying to obtain connections in the background. Consequently, later efforts to obtain a connection may fail. Default: 1
isolateInternalQueries
This property determines whether HikariCP isolates internal pool queries, such as the connection alive test, in their own transaction. Since these are typically read-only queries, it is rarely necessary to encapsulate them in their own transaction. This property only applies if autoCommit
is disabled. Default: false
allowPoolSuspension
This property controls whether the pool can be suspended and resumed through JMX. This is useful for certain failover automation scenarios. When the pool is suspended, calls to getConnection()
will not timeout and will be held until the pool is resumed. Default: false
readOnly
This property controls whether Connections obtained from the pool are in read-only mode by default. Note some databases do not support the concept of read-only mode, while others provide query optimizations when the Connection is set to read-only. Whether you need this property or not will depend largely on your application and database. Default: false
registerMbeans
This property controls whether or not JMX Management Beans ("MBeans") are registered or not. Default: false
catalog
This property sets the default catalog for databases that support the concept of catalogs. If this property is not specified, the default catalog defined by the JDBC driver is used. Default: driver default
connectionInitSql
This property sets a SQL statement that will be executed after every new connection creation before adding it to the pool. If this SQL is not valid or throws an exception, it will be treated as a connection failure and the standard retry logic will be followed. Default: none
driverClassName
HikariCP will attempt to resolve a driver through the DriverManager based solely on the jdbcUrl
, but for some older drivers the driverClassName
must also be specified. Omit this property unless you get an obvious error message indicating that the driver was not found. Default: none
transactionIsolation
This property controls the default transaction isolation level of connections returned from the pool. If this property is not specified, the default transaction isolation level defined by the JDBC driver is used. Only use this property if you have specific isolation requirements that are common for all queries. The value of this property is the constant name from the Connection
class such as TRANSACTION_READ_COMMITTED
, TRANSACTION_REPEATABLE_READ
, etc. Default: driver default
⌚validationTimeout
This property controls the maximum amount of time that a connection will be tested for aliveness. This value must be less than the connectionTimeout
. Lowest acceptable validation timeout is 250 ms. Default: 5000
⌚leakDetectionThreshold
This property controls the amount of time that a connection can be out of the pool before a message is logged indicating a possible connection leak. A value of 0 means leak detection is disabled. Lowest acceptable value for enabling leak detection is 2000 (2 seconds). Default: 0
➡dataSource
This property is only available via programmatic configuration or IoC container. This property allows you to directly set the instance of the DataSource
to be wrapped by the pool, rather than having HikariCP construct it via reflection. This can be useful in some dependency injection frameworks. When this property is specified, the dataSourceClassName
property and all DataSource-specific properties will be ignored. Default: none
schema
This property sets the default schema for databases that support the concept of schemas. If this property is not specified, the default schema defined by the JDBC driver is used. Default: driver default
➡threadFactory
This property is only available via programmatic configuration or IoC container. This property allows you to set the instance of the java.util.concurrent.ThreadFactory
that will be used for creating all threads used by the pool. It is needed in some restricted execution environments where threads can only be created through a ThreadFactory
provided by the application container. Default: none
➡scheduledExecutor
This property is only available via programmatic configuration or IoC container. This property allows you to set the instance of the java.util.concurrent.ScheduledExecutorService
that will be used for various internally scheduled tasks. If supplying HikariCP with a ScheduledThreadPoolExecutor
instance, it is recommended that setRemoveOnCancelPolicy(true)
is used.Default: none
相关推荐
HikariCP的配置也很灵活,可以通过Java代码、属性文件或者Spring XML配置来设定连接池的参数,如最大连接数、最小连接数、连接超时时间、空闲连接存活时间等。合理的配置可以优化数据库资源的使用,防止过多的并发...
接下来,需要在配置文件(如`application.properties`)中设置HikariCP的相关参数,例如: ```properties # 数据源配置 spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari....
7. **最佳实践**:为了获得最佳性能,应根据应用的实际情况调整HikariCP的配置参数,如连接超时、最大空闲时间等。同时,定期检查日志,监控连接池的状态,以便及时发现和解决问题。 8. **安全性**:使用HikariCP时...
5. **配置选项**:HikariCP提供了丰富的配置参数,允许开发者根据应用需求调整连接池的行为,如最大连接数、连接超时时间等。 HikariCPUtil.java可能是包含示例代码的文件,它可能展示了如何在Java程序中配置和使用...
在HikariCP v3.4.5这个版本中,我们可以深入探讨其在数据库连接管理、性能优化以及配置选项等方面的知识点。 首先,HikariCP的核心特性在于它的高速度。它通过最小化锁的使用、优化的对象池设计以及高效的线程安全...
- 配置:通过Java代码或者配置文件(如application.properties)来设置HikariCP的各项参数,如最大连接数、连接超时时间等。 - 初始化:在应用启动时初始化HikariCP连接池,然后通过连接池获取和归还数据库连接。 ...
4. 简洁API:HikariCP的配置简单明了,开发者可以轻松集成并调整参数以适应不同场景。 三、源码解析 HikariCP的源代码组织清晰,主要由以下几个关键组件构成: - HikariPool:连接池的核心类,负责连接的管理和调度...
- 配置是关键:正确配置HikariCP的各项参数可以极大地影响性能。例如,`connectionTimeout`设定连接超时时间,`idleTimeout`设置连接空闲多久后被回收,`maximumPoolSize`则限制了最大并发连接数。 - 初始化和关闭...
在项目中配置HikariCP,你通常需要指定数据库连接信息,例如URL、用户名、密码,以及连接池的一些参数,如最大连接数、最小连接数、连接超时时间等。配置可以写在应用的配置文件中,如`application.properties`或`...
2. **配置连接池**:在应用程序中创建一个配置对象,设置数据库URL、用户名、密码、驱动类名等参数,并指定HikariCP的一些性能调优参数。 3. **初始化连接池**:使用配置对象创建HikariDataSource实例,这是...
在Spring Boot中配置HikariCP主要是通过application.properties或application.yml文件。以下是一个基础配置的例子: ```yaml spring: datasource: url: jdbc:h2:mem:demodb username: sa password: hikari ...
4. **灵活配置**:用户可以通过配置参数调整连接池的大小、超时时间、最大空闲时间等,以适应不同的应用需求。 5. **健康检查**:HikariCP具有连接健康检查功能,能检测并迅速回收有问题的连接,保证应用的稳定性。 ...
在SpringBoot项目中,可以通过配置`application.properties`或`application.yml`来启用和配置HikariCP。 3. **Thymeleaf**: Thymeleaf是一个现代服务器端XML/XHTML/HTML5模板引擎,可与Spring集成,用于构建Web...
2. 配置连接池:设置数据源、最大连接数、超时时间等参数。 3. 初始化连接池:在应用启动时,调用HikariDataSource的initialize方法。 4. 获取与释放连接:通过HikariDataSource的getConnection方法获取连接,并在...
1. 简单易用:HikariCP 的 API 简洁明了,易于配置和使用。 2. 高效性能:HikariCP 具有极低的 CPU 和内存开销,具有出色的性能表现。 3. 透明度:HikariCP 对于应用开发者来说是完全透明的,无需其内部运作原理。 4...
本文将深入探讨HikariCP的核心特性、工作原理、配置与优化以及其在实际应用中的优势。 首先,HikariCP被赞誉为“性能之王”,这主要得益于它的设计目标——最小化JVM内存占用和最大化数据库连接的复用率。它通过...
2. **配置参数**:根据应用需求调整连接池的配置参数,如最大、最小连接数,超时时间等。 3. **异常处理**:正确处理数据库连接异常,如连接丢失或超时,以保证应用的健壮性。 4. **监控和日志**:启用连接池的...
2. 配置连接池:在应用的配置文件(如application.properties)中,设置HikariCP的参数,如数据库URL、用户名、密码、最大连接数等。 3. 创建数据源:在代码中创建HikariDataSource实例,然后通过它获取数据库连接。...
在Spring Boot中,通过配置HikariCP,可以优化数据库连接的管理和使用,提升整体系统的性能。 至于标签中的"spring",这是Spring框架的总称,包括Spring Core、Spring MVC等多个模块,Spring Boot就是基于Spring...