Hikari连接池-慢,阻止,连接不可用:SpringBoot

时间:2019-03-28 16:27:38

标签: java spring-boot jdbc hikaricp

问题:

我有一个配置了hikari(自动)的springboot应用程序。我遇到错误

  

连接不可用,请求在30113ms后超时

当我只是在数据库中执行插入操作时,流程就像Controller> Service> Repository> save(entity)也不在存储库中使用@Transactional,但是如果使用它,结果是相同的。

  1. 该服务的负载测试50个请求/ 1秒是否依次成功获得20-30个请求?其余失败,但出现以下异常。

    2019-03-28 20:58:29.507 ERROR 90260 --- [http-nio-8080-exec-234] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
    
    java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30113ms.
        at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:697) ~[HikariCP-3.3.1.jar:na]
    

    我正在执行某种负载测试,因为它触发了50req / 1sec,一半又一半的成功和失败。也启用了泄漏检测,但日志中没有跟踪。

  2. 我是否为此测试过度配置,还是需要调整池连接?还是只支持那么多?

  3. 第二个请求和后续请求之后的hikari getconnection也几乎需要增加5秒钟以上的时间(块),为什么?它为什么不平行?请帮我或指导我,我需要调整多少才能接受每1分钟200个请求。

application.yml

spring:
  application:
    name: demo
  datasource:
    hikari:
      connection-timeout: 20000
      minimum-idle: 5
      maximum-pool-size: 50
      idle-timeout: 300000
      max-lifetime: 1200000
      auto-commit: true
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url:: jdbc:sqlserver://ip:port;databaseName=sample
      username: username
      leak-detection-threshold: 30000

BootApplication.java

@SpringBootApplication
public class Sample{
public static void main(String[] args) {
        SpringApplication.run(Sample.class, args);
    }

@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
    public DataSource dataSource() {
     HikariDataSource dataSource=new HikariDataSource();
          //configuring pass from vault
        return dataSource;
    }
}

SampleService.java

@Service
public class SampleService implements SampleService {
    @Autowired
    private SampleRepository sampleRepository;

    @Override
    public List<String> getAll() {
        return (List<String>) sampleRepository.findAll();
    }

    @Override
    public String saveOrUpdate(Sample obj) {
        return sampleRepository.save(obj);
    }
}

0 个答案:

没有答案