0

I have a Visit entity which refers to a Patient entity by ManyToOne relationship. The repository for Visit is:

@RepositoryRestResource(collectionResourceRel = "visits", path = "visits", excerptProjection=VisitProjection.class)
public interface VisitRepository extends PagingAndSortingRepository<Visit, Long> {

    @RestResource(path="all")
    List<Visit> findByPatientIdContaining(@Param("keyword") String keyword);

}

When searching visits by patient ID with /visits/search/all?keyword=1 which may return millions of records, the query is forever pending and never ends. In the console there are dozens of hibernate sqls printed every second. How can I set the request timeout from server side?

I have tried:

  1. And Transactional annotation with timeout attribute to repository method: (works a little but still takes long to timeout)
@RestResource(path="all")
@Transactional(timeout=2)
List<Visit> findByPatientIdContaining(@Param("keyword") String keyword);
  1. add some timeout properties to application.properties: (just doesn't work at all):
spring.jpa.properties.hibernate.c3p0.timeout=2
spring.jpa.properties.javax.persistence.query.timeout=2
spring.mvc.async.request-timeout=2
server.connection-timeout=2
rest.connection.connection-request-timeout=2
rest.connection.connect-timeout=2
rest.connection.read-timeout=2
server.servlet.session.timeout=2
spring.session.timeout=2
spring.jdbc.template.query-timeout=2
spring.transaction.default-timeout=2
spring.jpa.properties.javax.persistence.query.timeout=2
javax.persistence.query.timeout=2
server.tomcat.connection-timeout=5
CDT
  • 10,165
  • 18
  • 66
  • 97

1 Answers1

2

Okay, no one using your API is going to want millions of records in one hit so use the provided paging functionality to make the result set more manageable:

https://docs.spring.io/spring-data/rest/docs/3.1.6.RELEASE/reference/html/#paging-and-sorting

@RestResource(path="all")
Page<Visit> findByPatientIdContaining(@Param("keyword") String keyword, Pageable p);

Clients can specify the records they want the records returned by adding the params:

?page=1&size=5

CDT
  • 10,165
  • 18
  • 66
  • 97
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • I know pagination is a solution, but what if some client sets `size=10000` accidentally? Still looking for some way to set a request timeout. – CDT Apr 25 '19 at 12:53
  • 1
    https://stackoverflow.com/questions/33675349/spring-data-rest-max-page-size-does-not-seem-to-work – Alan Hay Apr 25 '19 at 13:09