- JPA Query counter with dynamic proxy
- Implemented to count the number of queries occurring in JPA
- QueryCounter Post
1. Specify the path to the Entity in the EntityManager.
/config/QueryCounterConfig.java
@Configuration
public class QueryCountConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(queryCountDataSource());
em.setPackagesToScan(
// Specify the path to the entity
"com.example.querycounter.domain",
...
);
...
return em;
}
...
}
2. Inject a Counter bean and start query count
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class QueryCounterTest {
@Autowired
private Counter counter;
@Test
void testQueryCount() {
counter.startQueryCount();
// Business logic that wants to count the number of queries
assertThat(counter.getQueryCount()).isEqualTo("Expected number of queries");
counter.printQueryCount();
}
}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class QueryCounterTest {
@Autowired
private Counter counter;
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
private PostRepository postRepository;
@BeforeEach
void setUp() {
for (int i = 1; i <= 10; i++) {
User user = userRepository.save(new User(i + "번 user"));
Post post = Post.builder()
.writer(user)
.contents(i + "번 post")
.build();
postRepository.save(post);
}
}
@Test
void N_Plus_1_Problem() {
counter.startQueryCount();
List<String> userPostContentsList = userService.getAllPostContents();
assertThat(userPostContentsList).hasSize(10);
assertThat(counter.getQueryCount()).isEqualTo(11);
counter.printQueryCount();
}
}