Skip to content

Commit add4078

Browse files
authored
Merge pull request #8 from yannbriancon/bug-fix-null-thread-local-value
[Bug fix] null thread local value
2 parents 8c562cb + a25c940 commit add4078

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.yannbriancon</groupId>
77
<artifactId>spring-hibernate-query-utils</artifactId>
8-
<version>1.0.0</version>
8+
<version>1.0.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>spring-hibernate-query-utils</name>

src/main/java/com/yannbriancon/interceptor/HibernateQueryInterceptor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@
1212
import java.io.Serializable;
1313
import java.util.HashSet;
1414
import java.util.Set;
15+
import java.util.function.Supplier;
1516

1617
@Component
1718
@EnableConfigurationProperties(HibernateQueryInterceptorProperties.class)
1819
public class HibernateQueryInterceptor extends EmptyInterceptor {
1920

2021
private transient ThreadLocal<Long> threadQueryCount = new ThreadLocal<>();
2122

22-
private transient ThreadLocal<Set<String>> threadPreviouslyLoadedEntities = new ThreadLocal<>();
23+
private transient ThreadLocal<Set<String>> threadPreviouslyLoadedEntities =
24+
ThreadLocal.withInitial(new EmptySetSupplier());
2325

2426
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateQueryInterceptor.class);
2527

2628
private final HibernateQueryInterceptorProperties hibernateQueryInterceptorProperties;
2729

2830
public HibernateQueryInterceptor(HibernateQueryInterceptorProperties hibernateQueryInterceptorProperties) {
29-
threadPreviouslyLoadedEntities.set(new HashSet<>());
3031
this.hibernateQueryInterceptorProperties = hibernateQueryInterceptorProperties;
3132
}
3233

@@ -117,3 +118,9 @@ private void logDetectedNPlusOneQuery(String entityName) {
117118
}
118119
}
119120
}
121+
122+
class EmptySetSupplier implements Supplier<Set<String>> {
123+
public Set<String> get(){
124+
return new HashSet<>();
125+
}
126+
}

0 commit comments

Comments
 (0)