Skip to content

Commit

Permalink
Resolves yannbriancon#26 Added HibernateQueryOnRequestResetIntercepto…
Browse files Browse the repository at this point in the history
…r which can be used to reset query detection state on each request
  • Loading branch information
Martin Hollerweger committed May 18, 2021
1 parent b0c9abb commit 6eaa8c4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@ public class NotificationResourceIntTest {
}
```

### Reset Query Detection State on each Request
SpringBoot/Tomcat is reusing executer threads. This can lead to wrong N+1 detection due to old queries in ThreadLocal.
Register following interceptor in you WebMvcConfigurer:

```java
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(applicationContext.getBean(HibernateQueryOnRequestResetInterceptor.class));
}
```



<!-- CHANGELOG -->
## Changelog

Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.yannbriancon</groupId>
<artifactId>spring-hibernate-query-utils</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<name>spring-hibernate-query-utils</name>
Expand Down Expand Up @@ -57,6 +57,11 @@
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public HibernateQueryInterceptor(
/**
* Reset the N+1 query detection state
*/
private void resetNPlusOneQueryDetectionState() {
public void resetNPlusOneQueryDetectionState() {
threadPreviouslyLoadedEntities.set(new HashSet<>());
threadSelectQueriesInfoPerProxyMethod.set(new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yannbriancon.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@ComponentScan(basePackages = {"com.yannbriancon"})
public class HibernateQueryOnRequestResetInterceptor implements HandlerInterceptor {

@Autowired
HibernateQueryInterceptor hibernateQueryInterceptor;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

// Reset query detection state on each new request
hibernateQueryInterceptor.resetNPlusOneQueryDetectionState();
return true;
}
}

0 comments on commit 6eaa8c4

Please sign in to comment.