Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #26 Added HibernateQueryOnRequestResetInterceptor #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a big dependency that may break projects of other users.
@reitzmichnicht Do you have an opinion since you optimized the dependencies?

</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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected should be enough

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;
}
}