我遇到了HikariCP,我对基准测试感到惊讶,我想尝试它而不是我的默认选择C3P0,令我惊讶的是,我努力获得configurations 可能是因为配置不同基于您正在使用哪种技术堆栈组合。
我已经设置了Spring Boot 项目和JPA, Web, Security 启动器(使用Spring Initializer)以使用PostgreSQL 作为数据库,HikariCP 作为连接池。
我使用Gradle 作为构建工具,我想分享对我有用的以下假设:
- Spring Boot Starter JPA(Web 和安全 - 可选)
- Gradle 也可以构建
- 使用数据库(即模式、用户、数据库)运行和设置 PostgreSQL
如果您使用的是Gradle,则需要以下build.gradle;如果您使用的是maven,则需要等效的pom.xml
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
group = 'com'
version = '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-aop')
// Exclude the tomcat-jdbc since it's used as default for connection pooling
// This can also be achieved by setting the spring.datasource.type to HikariCP
// datasource see application.properties below
compile('org.springframework.boot:spring-boot-starter-data-jpa') {
exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
}
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.postgresql:postgresql')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
// Download HikariCP but, exclude hibernate-core to avoid version conflicts
compile('com.zaxxer:HikariCP:2.5.1') {
exclude group: 'org.hibernate', module: 'hibernate-core'
}
// Need this in order to get the HikariCPConnectionProvider
compile('org.hibernate:hibernate-hikaricp:5.2.11.Final') {
exclude group: 'com.zaxxer', module: 'HikariCP'
exclude group: 'org.hibernate', module: 'hibernate-core'
}
}
上面build.gradle中有一堆排除,那是因为
- 首先排除,指示gradle在下载
spring-boot-starter-data-jpa依赖项时排除jdbc-tomcat连接池。这也可以通过设置spring.datasource.type=com.zaxxer.hikari.HikariDataSource 来实现,但是,如果我不需要它,我不想要额外的依赖
- 第二次排除,指示gradle在下载
com.zaxxer依赖时排除hibernate-core,这是因为hibernate-core已经被Spring Boot下载了,我们不想最终得到不同的版本。
- 第三个排除,指示 gradle 在下载
hibernate-hikaricp 模块时排除 hibernate-core 模块,这是使 HikariCP 使用 org.hibernate.hikaricp.internal.HikariCPConnectionProvider 作为连接提供程序而不是弃用的 com.zaxxer.hikari.hibernate.HikariConnectionProvider 所需要的
一旦我弄清楚了 build.gradle 以及要保留和不保留的内容,我就准备将 datasource 配置复制/粘贴到我的 application.properties 中,并希望一切都能顺利进行,但事实并非如此我偶然发现了以下问题
- Spring boot 无法找到数据库详细信息(即 url、驱动程序),因此无法设置 jpa 和 hibernate(因为我没有正确命名属性键值)
- HikariCP 回退到
com.zaxxer.hikari.hibernate.HikariConnectionProvider
- 在指示 Spring 在自动配置 hibernate/jpa 时使用新的连接提供程序后,HikariCP 失败了,因为它在
application.properties 中寻找一些 key/value 并抱怨 dataSource, dataSourceClassName, jdbcUrl。我不得不调试到HikariConfig, HikariConfigurationUtil, HikariCPConnectionProvider 并发现HikariCP 找不到来自application.properties 的属性,因为它的名称不同。
无论如何,这是我必须依靠反复试验并确保HikariCP 能够选择属性(即,数据库详细信息的数据源以及池属性)以及 Sping Boot 的行为预期,我最终得到了以下application.properties 文件。
server.contextPath=/
debug=true
# Spring data source needed for Spring boot to behave
# Pre Spring Boot v2.0.0.M6 without below Spring Boot defaults to tomcat-jdbc connection pool included
# in spring-boot-starter-jdbc and as compiled dependency under spring-boot-starter-data-jpa
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:postgresql://localhost:5432/somedb
spring.datasource.username=dbuser
spring.datasource.password=dbpassword
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
# Without below HikariCP uses deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider
# Surprisingly enough below ConnectionProvider is in hibernate-hikaricp dependency and not hibernate-core
# So you need to pull that dependency but, make sure to exclude it's transitive dependencies or you will end up
# with different versions of hibernate-core
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
# JPA specific configs
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql=true
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.default_schema=dbschema
spring.jpa.properties.hibernate.search.autoregister_listeners=false
spring.jpa.properties.hibernate.bytecode.use_reflection_optimizer=false
# Enable logging to verify that HikariCP is used, the second entry is specific to HikariCP
logging.level.org.hibernate.SQL=DEBUG
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
如上所示,配置根据以下命名模式分为几类
-
spring.datasource.x(Spring 自动配置会选择这些,HikariCP 也会选择)
-
spring.datasource.hikari.x(HikariCP 选择这些来设置池,记下 camelCase 字段名称)
-
spring.jpa.hibernate.connection.provider_class(指示 Spring 使用新的 HibernateConnectionProvider)
-
spring.jpa.properties.hibernate.x(Spring用来自动配置JPA,用下划线记下字段名)
很难找到说明如何使用上述属性文件以及如何命名属性的教程、帖子或资源。好吧,你有它。
将上面的application.properties 和build.gradle(或至少类似)放入 Spring Boot JPA 项目版本(1.5.8)应该像一个魅力一样工作并连接到您的预配置数据库(即在我的情况下它是HikariCP & Spring 都从 spring.datasource.url 中找出要使用哪个数据库驱动程序的 PostgreSQL)。
我没有看到创建 DataSource bean 的必要性,这是因为 Spring Boot 只需查看 application.properties 就可以为我做所有事情,这很简洁。
HikariCP 的 github wiki 中的 article 展示了如何使用 JPA 设置 Spring Boot,但缺乏解释和细节。
以上两个文件也可以作为公共要点https://gist.github.com/rhamedy/b3cb936061cc03acdfe21358b86a5bc6