【问题标题】:Why this QueryDSL query without @Transactional will cause Unable to acquire JDBC Connection为什么这个没有 @Transactional 的 QueryDSL 查询会导致无法获取 JDBC 连接
【发布时间】:2022-08-22 09:42:34
【问题描述】:

Spring Boot 应用程序抛出异常

Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection

在多次请求 api 之后

public Response getChainObjectDepartments(@RequestBody List<UUID> ids) {
  val ud = QUserDepartmentPlain.userDepartmentPlain;
  val d = QDepartmentPlain.departmentPlain;
  val result = jpaQueryFactory.select(d).from(d).join(ud).on(ud.departmentId.eq(d.id)).where(ud.userId.in(ids)).transform(
      groupBy(ud.userId).as(GroupBy.list(d))
  );
  return Response.ok(result);
}

我使用 PostgreSQL 并且 pgAdmin 显示很多会话的最后一个查询是由这个 QueryDSL 查询生成的:

    select
        userdepart1_.\"user_id\" as col_0_0_,
        department0_.\"id\" as col_1_0_,
        department0_.\"id\" as id1_29_,
        department0_.\"charge_leader_id\" as charge_11_29_,
        department0_.\"dingtalk_id\" as dingtalk2_29_,
        department0_.\"is_audit\" as is_audi12_29_,
        department0_.\"is_enable\" as is_enabl3_29_,
        department0_.\"name\" as name4_29_,
        department0_.\"parent_id\" as parent_i5_29_,
        department0_.\"remark\" as remark6_29_,
        department0_.\"sort\" as sort7_29_,
        department0_.\"tz_create\" as tz_creat8_29_,
        department0_.\"tz_retire\" as tz_retir9_29_,
        department0_.\"tz_update\" as tz_upda10_29_
    from
        \"organization\".\"department\" department0_
    inner join
        \"organization\".\"rel_user_department\" userdepart1_
            on (
                userdepart1_.\"department_id\"=department0_.\"id\"
            )
    where
        userdepart1_.\"user_id\" in (
            ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?
        )

当所有会话都填满此查询时,应用程序开始抛出异常Unable to acquire JDBC Connection

最后,我通过在函数中添加@Transactional 来解决问题(我不将@Transactional 添加到函数中,一般只有选择查询)。 但是我仍然不知道这个 QueryDSL 查询有什么问题,为什么它必须有@Transactional。

    标签: postgresql spring-boot jpa querydsl


    【解决方案1】:

    我一直遇到同样的问题。我认为 QueryDSL 5 存在连接泄漏。

    【讨论】:

      猜你喜欢
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      相关资源
      最近更新 更多