巴发 cp304697449my2462295my

Oh My思密达[第78期]韩女星叫嚣没钱就去中国挣_综艺_高清1080P在线观看_腾讯视频java - How do I configure HikariCP in my Spring Boot app in my application.properties files? - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I'm trying to set up HikariCP in my Spring Boot (1.2.0.M1) app so I can test using it in place of Tomcat DBCP. I'd like to configure the connection pool in my application.properties file like I was doing with Tomcat, but I can't figure out how I should be doing it. All examples I've found show either JavaConfig style, or using a separate HikariCP properties file. Can someone help me figure out the property names to configure it in application.properties? I'd like to also switch from using the driverClassName approach to the DataSourceClassName approach since it looks cleaner and is recommended. Is this also possible in my application.properties file(s)?
Here's what I had for Tomcat DBCP (just some basic config, not fully flushed out)
spring.datasource.validation-query=SELECT 1
spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=5
spring.datasource.test-on-borrow=true
spring.datasource.test-on-return=true
And I'm currently using driverClassName and jdbc url to set up the connection:
spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.driverClassName=com.mysql.jdbc.Driver
@Configuration
@ConfigurationProperties(prefix = "params.datasource")
public class JpaConfig extends HikariConfig {
public DataSource dataSource() throws SQLException {
return new HikariDataSource(this);
application.yml
datasource:
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/myDb
username: login
password: password
maximumPoolSize: 5
UPDATED! Since version Spring Boot 1.3.0 :
Just add HikariCP to dependecies
Configure application.yml
application.yml
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:TEST
driver-class-name: org.h2.Driver
username: username
password: password
idle-timeout: 10000
UPDATED! Since version Spring Boot 2.0.0 :
The default connection pool has changed from Tomcat to Hikari :)
You could simply make use of application.yml/application.properties only. There is no need to explicitly create any DataSource Bean
You need to exclude tomcat-jdbc as mentioned by ydemartino
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-jdbc&/artifactId&
&exclusions&
&exclusion&
&groupId&org.apache.tomcat&/groupId&
&artifactId&tomcat-jdbc&/artifactId&
&/exclusion&
&/exclusions&
&/dependency&
As you won't create DataSource bean, you have to explicitly specify using Hikari through spring.datasource.type with value com.zaxxer.hikari.HikariDataSource in application.yml / application.properties
datasource:
connection-test-query: SELECT 1 FROM DUAL
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/myDb
username: login
password: password
type: com.zaxxer.hikari.HikariDataSource
In your application.yml / application.properties, you could configure Hikari specific parameters such as pool size etc in spring.datasource.hikari.*
I came across HikariCP and I was amazed by the benchmarks and I wanted to try it instead of my default choice C3P0 and to my surprise I struggled to get the configurations right probably because the configurations differ based on what combination of tech stack you are using.
I have setup Spring Boot project with JPA, Web, Security starters (Using ) to use PostgreSQL as a database with HikariCP as connection pooling.
I have used Gradle as build tool and I would like to share what worked for me for the following assumptions:
Spring Boot Starter JPA (Web & Security - optional)
Gradle build too
PostgreSQL running and setup with a database (i.e. schema, user, db)
You need the following build.gradle if you are using Gradle or equivalent pom.xml if you are using maven
buildscript {
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'
There are a bunch of excludes in the above build.gradle and that's because
First exclude, instructs gradle that exclude the jdbc-tomcat connection pool when downloading the spring-boot-starter-data-jpa dependencies. This can be achieved by setting up the spring.datasource.type=com.zaxxer.hikari.HikariDataSource also but, I don't want an extra dependency if I don't need it
Second exclude, instructs gradle to exclude hibernate-core when downloading com.zaxxer dependency and that's because hibernate-core is already downloaded by Spring Boot and we don't want to end up with different versions.
Third exclude, instructs gradle to exclude hibernate-core when downloading hibernate-hikaricp module which is needed in order to make HikariCP use org.hibernate.hikaricp.internal.HikariCPConnectionProvider as connection provider instead of deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider
Once I figured out the build.gradle and what to keep and what to not, I was ready to copy/paste a datasource configuration into my application.properties and expected everything to work with flying colors but, not really and I stumbled upon the following issues
Spring boot failing to find out database details (i.e. url, driver) hence, not able to setup jpa and hibernate (because I didn't name the property key values right)
HikariCP falling back to com.zaxxer.hikari.hibernate.HikariConnectionProvider
After instructing Spring to use new connection-provider for when auto-configuring hibernate/jpa then HikariCP failed because it was looking for some key/value in the application.properties and was complaining about dataSource, dataSourceClassName, jdbcUrl. I had to debug into HikariConfig, HikariConfigurationUtil, HikariCPConnectionProvider and found out that HikariCP could not find the properties from application.properties because it was named differently.
Anyway, this is where I had to rely on trial and error and make sure that HikariCP is able to pick the properties (i.e. data source that's db details, as well as pooling properties) as well as Sping Boot behave as expected and I ended up with the following application.properties file.
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
As shown above the configurations are divided into categories based on following naming patterns
spring.datasource.x (Spring auto-configure will pick these, so will HikariCP)
spring.datasource.hikari.x (HikariCP picks these to setup the pool, make a note of the camelCase field names)
spring.jpa.hibernate.connection.provider_class (Instructs Spring to use new HibernateConnectionProvider)
spring.jpa.properties.hibernate.x (Used by Spring to auto-configure JPA, make a note of the field names with underscores)
It's hard to come across a tutorial or post or some resource that shows how the above properties file is used and how the properties should be named. Well, there you have it.
Throwing the above application.properties with build.gradle (or at least similar) into a Spring Boot JPA project version (1.5.8) should work like a charm and connect to your pre-configured database (i.e. in my case it's PostgreSQL that both HikariCP & Spring figure out from the spring.datasource.url on which database driver to use).
I did not see the need to create a DataSource bean and that's because Spring Boot is capable of doing everything for me just by looking into application.properties and that's neat.
in HikariCP's github
shows how to setup Spring Boot with JPA but, lacks explanation and details.
The above two file is also availble as a public gist
3,60011431
You don't need redundant code for putting property values to variables. You can set properties with a properties file directly.
Put hikari.properties file in the classpath.
driverClassName=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/myDb
connectionTestQuery=SELECT 1
maximumPoolSize=20
username=...
password=...
And make a datasource bean like this.
@Bean(destroyMethod = "close")
public DataSource dataSource() throws SQLException {
HikariConfig config = new HikariConfig("/hikari.properties");
HikariDataSource dataSource = new HikariDataSource(config);
return dataS
7,819136196
This works for my boot application in case it helps. This class tells you what properties the config object is looking for:
I think multiple datasources could be supporting by adding datasource_whatever to the property keys in the source config file. Cheers!
@Configuration
class DataSourceConfig {
@Value('${spring.datasource.username}')
@Value('${spring.datasource.password}')
@Value('${spring.datasource.url}')
private String dataSourceU
@Value('${spring.datasource.dataSourceClassName}')
private String dataSourceClassN
@Value('${spring.datasource.connectionTimeout}')
private int connectionT
@Value('${spring.datasource.maxLifetime}')
private int maxL
public DataSource primaryDataSource() {
Properties dsProps = [url: dataSourceUrl, user: user, password: password]
Properties configProps = [
connectionTestQuery: 'select 1 from dual',
connectionTimeout: connectionTimeout,
dataSourceClassName: dataSourceClassName,
dataSourceProperties: dsProps,
maxLifetime: maxLifetime
// A default max pool size of 10 seems reasonable for now, so no need to configure for now.
HikariConfig hc = new HikariConfig(configProps)
HikariDataSource ds = new HikariDataSource(hc)
You can use the dataSourceClassName approach, here is an example with MySQL.
(Tested with spring boot 1.3 and 1.4)
First you need to exclude tomcat-jdbc from the classpath as it will be picked in favor of hikaricp.
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-jdbc&/artifactId&
&exclusions&
&exclusion&
&groupId&org.apache.tomcat&/groupId&
&artifactId&tomcat-jdbc&/artifactId&
&/exclusion&
&/exclusions&
&/dependency&
application.properties
spring.datasource.dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.datasource.dataSourceProperties.serverName=localhost
spring.datasource.dataSourceProperties.portNumber=3311
spring.datasource.dataSourceProperties.databaseName=mydb
spring.datasource.username=root
spring.datasource.password=root
Then just add
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
I created a test project here:
Here is the good news. HikariCP is the default connection pool now with Spring Boot 2.0.0.
The default database pooling technology in Spring Boot 2.0 has been switched from Tomcat Pool to HikariCP. We’ve found that Hakari offers superior performance, and many of our users prefer it over Tomcat Pool.
So it turns out that almost all the default settings for HikariCP work for me except the number of DB connections. I set that property in my application.properties:
spring.datasource.maximumPoolSize=20
And Andy Wilkinson is correct as far as I can tell in that you can't use the dataSourceClassName configuration approach for HikariCP with Spring Boot.
you can't use dataSourceClassName approach in application.properties configurations as said by @Andy Wilkinson. if you want to have dataSourceClassName anyway you can use Java Config as:
@Configuration
@ComponentScan
class DataSourceConfig {
@Value("${spring.datasource.username}")
@Value("${spring.datasource.password}")
@Value("${spring.datasource.url}")
private String dataSourceU
@Value("${spring.datasource.dataSourceClassName}")
private String dataSourceClassN
@Value("${spring.datasource.poolName}")
private String poolN
@Value("${spring.datasource.connectionTimeout}")
private int connectionT
@Value("${spring.datasource.maxLifetime}")
private int maxL
@Value("${spring.datasource.maximumPoolSize}")
private int maximumPoolS
@Value("${spring.datasource.minimumIdle}")
private int minimumI
@Value("${spring.datasource.idleTimeout}")
private int idleT
public DataSource primaryDataSource() {
Properties dsProps = new Properties();
dsProps.put("url", dataSourceUrl);
dsProps.put("user", user);
dsProps.put("password", password);
dsProps.put("prepStmtCacheSize",250);
dsProps.put("prepStmtCacheSqlLimit",2048);
dsProps.put("cachePrepStmts",Boolean.TRUE);
dsProps.put("useServerPrepStmts",Boolean.TRUE);
Properties configProps = new Properties();
configProps.put("dataSourceClassName", dataSourceClassName);
configProps.put("poolName",poolName);
configProps.put("maximumPoolSize",maximumPoolSize);
configProps.put("minimumIdle",minimumIdle);
configProps.put("minimumIdle",minimumIdle);
configProps.put("connectionTimeout", connectionTimeout);
configProps.put("idleTimeout", idleTimeout);
configProps.put("dataSourceProperties", dsProps);
HikariConfig hc = new HikariConfig(configProps);
HikariDataSource ds = new HikariDataSource(hc);
reason you cannot use dataSourceClassName because it will throw and exception
Caused by: java.lang.IllegalStateException: both driverClassName and dataSourceClassName are specified, one or the other should be used.
which mean spring boot infers from spring.datasource.url property the Driver and at the same time setting the dataSourceClassName creates this exception. To make it right your application.properties should look something like this for HikariCP datasource:
# hikariCP
spring.jpa.databasePlatform=org.hibernate.dialect.MySQLDialect
spring.datasource.url=jdbc:mysql://localhost:3306/exampledb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.poolName=SpringBootHikariCP
spring.datasource.maximumPoolSize=5
spring.datasource.minimumIdle=3
spring.datasource.maxLifetime=2000000
spring.datasource.connectionTimeout=30000
spring.datasource.idleTimeout=30000
spring.datasource.pool-prepared-statements=true
spring.datasource.max-open-prepared-statements=250
Note: Please check if there is any tomcat-jdbc.jar or commons-dbcp.jar in your classpath added most of the times by transitive dependency. If these are present in classpath Spring Boot will configure the Datasource using default connection pool which is tomcat. HikariCP will only be used to create the Datasource if there is no other provider in classpath. there is a fallback sequence from tomcat -> to HikariCP -> to Commons DBCP.
You can find a complete tutorial along with downloadable example here in this link:
With the later spring-boot releases switching to Hikari can be done entirely in configuration. I'm using 1.5.6.RELEASE and this approach works.
build.gradle:
compile "com.zaxxer:HikariCP:2.7.3"
application YAML
datasource:
type: com.zaxxer.hikari.HikariDataSource
idleTimeout: 60000
minimumIdle: 2
maximumPoolSize: 20
connectionTimeout: 30000
poolName: MyPoolName
connectionTestQuery: SELECT 1
Change connectionTestQuery to suit your underlying DB. That's it, no code required.
3,86411127
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled我的包裹号CPMY和CPMY,3月2日从马来西亚寄出,目的地是上海松江,还没收到。谁能帮我?_百度知道
我的包裹号CPMY和CPMY,3月2日从马来西亚寄出,目的地是上海松江,还没收到。谁能帮我?
我有更好的答案
47:00Item received and processed from posting at Post OfficesJohor Bharu PPL 19CPMY 16:00Item Dispatch Out From Origin StationJohor Bharu PPL 09:00Item dispatch out from transit officeJohor Bharu PPLCPMY 16:47:00Item received and processed from posting at Post OfficesJohor Bharu PPL 19:08:08:00Item Dispatch Out From Origin StationJohor Bharu PPL 09:10
谢谢你~至少我知道包裹真的在3月2日寄出了~但是已经经过一个多月,还没收到。真的担心。
估计在路上,速度慢点
为您推荐:
其他类似问题
上海松江的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。linux下安装mysql时,初始化的时候提示找不到cp support-files/my-medium.cnf /etc/my.cnf 中的my-medium._百度知道
linux下安装mysql时,初始化的时候提示找不到cp support-files/my-medium.cnf /etc/my.cnf 中的my-medium.
linux下安装mysql时,初始化的时候提示找不到cp support-files/my-medium.cnf /etc/my.cnf 中的my-medium.cnf文件,之后我查找了一下,在support-files下,有个/my-medium.cnf.sh文件。但是打不开。我看网上一些朋友安装都没.sh的文件啊。有哪位大侠可否告诉在...
没有的。运行cp support-files/my-medium.cnf /etc/my.cnf ,提示没有my-medium.cnf这个文件
我有更好的答案
ibncurses,然后安装上就可以了.so.5这个链接库的问题,你查找一个这个库在那个包里面。 请参考,希望能帮到你
采纳率:10%
find / -name &my-medium.cnf& -print 找到后再执行copy操作.
命令找一下
找不到根本没装上啊
/etc/my.cnf是默认的配置文件,默认往往没有,所以需要拷贝得到。cp support-files/my-medium.cnf /etc/my.cnf这个命令是在哪个目录运行的,路径对吗?support-files这个目录一般位于源代码目录中。
会不会是一个隐藏文件 你用 ls -a 试试
其他1条回答
为您推荐:
其他类似问题
cnf的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 myaa024d 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信