PGSimpleDataSource + HikariCP连接获取时间长

时间:2018-09-16 19:16:40

标签: spring-boot postgresql-9.1 hikaricp

拥有一个Spring Boot应用1.4.1.RELEASE。在服务启动期间,我可以看到Hikari连接池是使用以下配置创建的,但是获取JDBC连接需要很长时间。来自日志:

**34880058484** nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
8074041 nanoseconds spent preparing 6 JDBC statements;
58549577 nanoseconds spent executing 3 JDBC statements;
350394122 nanoseconds spent executing 3 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
394751462 nanoseconds spent executing 2 flushes (flushing a total of 4 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

build.gradle相关配置:

compile('org.springframework.boot:spring-boot-starter-data-jpa') {
    exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
}
// HikariCP
compile('com.zaxxer:HikariCP:3.2.0') {
    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'
}

spring.datasource配置:

spring.datasource.platform=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

HikariCP配置:

# Hikari will use the above plus the following to setup connection pooling
spring.datasource.hikari.data-source-class-name=org.postgresql.ds.PGSimpleDataSource
spring.datasource.hikari.data-source-properties.url=${spring.datasource.url}
spring.datasource.hikari.data-source-properties.user=${spring.datasource.username}
spring.datasource.hikari.data-source-properties.password=${spring.datasource.password}
spring.datasource.hikari.data-source-properties.reWriteBatchedInserts=true
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=50
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=SessionServiceHikariCP
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1 from session
spring.datasource.hikari.auto-commit=true
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider

Application.java

@Bean
public HikariDataSource hikariDataSource() throws Exception{
    return new HikariDataSource(hikariConfig());
}
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public HikariConfig hikariConfig() throws Exception{
    return new HikariConfig();
}

@Bean
public JdbcTemplate getJdbcTemplate() throws Exception {
    return new JdbcTemplate(hikariDataSource());
}

似乎没有从池中使用连接,但是JDBC每次都会创建一个新连接。可能是什么原因?

0 个答案:

没有答案