Skip to content

Commit 3018ac6

Browse files
authored
Merge pull request #13 from ekennedy247/eugene/master/addEnabledPropertyForNPlusOneDetection
Add enabled property for N+1 detection
2 parents 98a3dbb + afb3105 commit 3018ac6

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ public void afterTransactionCompletion(Transaction tx) {
9292
*/
9393
@Override
9494
public Object getEntity(String entityName, Serializable id) {
95-
detectNPlusOneQueriesOfMissingQueryEagerFetching(entityName, id);
96-
97-
detectNPlusOneQueriesOfMissingEntityFieldLazyFetching(entityName, id);
95+
if (hibernateQueryInterceptorProperties.isnPlusOneDetectionEnabled()) {
96+
detectNPlusOneQueriesOfMissingQueryEagerFetching(entityName, id);
97+
detectNPlusOneQueriesOfMissingEntityFieldLazyFetching(entityName, id);
98+
}
9899

99100
Set<String> previouslyLoadedEntities = threadPreviouslyLoadedEntities.get();
100101

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ enum ErrorLevel {
1818
*/
1919
private ErrorLevel errorLevel = ErrorLevel.ERROR;
2020

21+
private boolean nPlusOneDetectionEnabled = true;
22+
2123
public ErrorLevel getErrorLevel() {
2224
return errorLevel;
2325
}
2426

2527
public void setErrorLevel(String errorLevel) {
2628
this.errorLevel = ErrorLevel.valueOf(errorLevel);
2729
}
30+
31+
public boolean isnPlusOneDetectionEnabled() {
32+
return nPlusOneDetectionEnabled;
33+
}
34+
35+
public void setnPlusOneDetectionEnabled(boolean nPlusOneDetectionEnabled) {
36+
this.nPlusOneDetectionEnabled = nPlusOneDetectionEnabled;
37+
}
2838
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.yannbriancon.interceptor;
2+
3+
import com.yannbriancon.utils.entity.Message;
4+
import com.yannbriancon.utils.repository.MessageRepository;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(properties = { "hibernate.query.interceptor.error-level=EXCEPTION", "hibernate.query.interceptor.n-plus-one-detection-enabled=false" })
20+
@Transactional
21+
class NPlusOneQueriesDisabledTest {
22+
23+
@Autowired
24+
private MessageRepository messageRepository;
25+
26+
@Autowired
27+
private HibernateQueryInterceptor hibernateQueryInterceptor;
28+
29+
@Test
30+
void nPlusOneQueriesDetection_whenDisabled_stillCountsQueries_doesNotThrowException() {
31+
// Fetch the 2 messages without the authors
32+
List<Message> messages = messageRepository.findAll();
33+
34+
hibernateQueryInterceptor.startQueryCount();
35+
36+
// Trigger N+1 queries, if this WAS enabled, it would throw an exception and fail the test.
37+
messages.stream()
38+
.map(message -> message.getAuthor().getName())
39+
.collect(Collectors.toList());
40+
41+
//Assert that counting still runs when n+1 detection is disabled
42+
assertThat(hibernateQueryInterceptor.getQueryCount()).isEqualTo(2);
43+
}
44+
}

0 commit comments

Comments
 (0)