Search code examples
springspring-bootspring-data-jpahikaricp

When does JPA return connection to db pool


I have a Restendpoint that makes a database call (jpa) to check the permission and after that an external call is made. The result of this external call is returned to the caller. When the external call takes very long i got the following exception:

java.lang.Exception: Apparent connection leak detected
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128)
        25 lines skipped for [org.hibernate]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:196)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143)

It seems that jpa keeps the connection of the db pool til the restendpoint returns the response. I would like to force jpa to return the connection after the permission check is done. Is there a way to do this (like a annotation)?


Solution

  • The solution to my problem was to disable spring.jpa.open-in-view. In that case the database connection is imediatly returned to the db pool after jpa has used it. disadvantage is that related data has to be loaded separately.