Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Setting up query counter with Spring Boot #14

Closed
rodelrod opened this issue Mar 5, 2019 · 5 comments
Closed

Setting up query counter with Spring Boot #14

rodelrod opened this issue Mar 5, 2019 · 5 comments

Comments

@rodelrod
Copy link

rodelrod commented Mar 5, 2019

Hi Vlad,

TL;DR To make this work with Spring Boot I had to add as a dependency the library https://github.com/gavlyukovskiy/spring-boot-data-source-decorator and add the property decorator.datasource.datasource-proxy.count-query=true to application.properties.

First let me thank you for your fantastic work. Coming to Java/Spring Boot from other platforms has not been a pleasant experience due to the lack of community resources and your contribution is a shining exception.

My latest challenge has been into finding the equivalent functionality to Django's assertNumQueries for testing, since we have been finding hard to keep the Hibernate N+1 situation under control. Your library fills the bill.

However, It was challenging to make it work on our Spring Boot 2 configuration, so I'd like to contribute with the (certainly clumsy) solution I eventually found, and have your opinion if there's a better one.

Just adding the db-util dependency to the pom.xml and calling SQLStatementCountValidators .reset() and .assertSelectCount() was not enough. The counter was not incrementing. I am using the @Transactional and @SpringBootTest annotations in the test class. After much fiddling I ended up coming up with a solution:

  1. Add the dependency to spring-boot-data-source in the pom.xml:
        <dependency>
            <groupId>com.github.gavlyukovskiy</groupId>
            <artifactId>datasource-proxy-spring-boot-starter</artifactId>
            <version>1.5.3</version>
        </dependency>
  1. Add the property decorator.datasource.datasource-proxy.count-query=true to application.properties.

Thanks

@vladmihalcea
Copy link
Owner

Just adding the db-util dependency to the pom.xml and calling SQLStatementCountValidators .reset() and assertSelectCount() was not enough.

Yes, that's not enough. You also need to wrap the DataSource as explained in this article. Now, doing that in Spring Boot is more work than with a plain Spring application, and that's would explain why you needed that starter.

Anyway, I'm going to close this issue since it is Spring Boot responsibility to add support for other libraries, not the other way around.

@rodelrod
Copy link
Author

rodelrod commented Mar 6, 2019

Thanks for the very quick answer. I've read that article multiple times and the following line led me to believe that all you needed was to add the maven dependency to set up the interceptor:

Since it’s already available in Maven Central Repository, you can easily use it by just adding this dependency to your pom.xml [...]

In any event there was no indication of what else I could do, so I thought that the technique I describe could be useful for other users.

@vladmihalcea
Copy link
Owner

I can't add the dependency to the project. What if someone wants to use it with Java EE?

This is like adding a Spring dependency into Log4J.

@rodelrod
Copy link
Author

rodelrod commented Mar 6, 2019

I agree and I'm not asking for you to do that, just documenting my experience to help other users that may find themselves in the same position (which I imagine is a common one, since it's provided by a bunch of popular gems in Rails and core Django functionality since 2011; and we haven't been having any less N+1 problems with JPA/Hibernate than I've had with other ORM's, even after I read your excellent book on the subject).

Anyway, no action asked from you. Feel free to document this or let Google take care of it. Thanks again for your work, if there were just a few others like you around, life in the Java/Spring ecosystem would be much less painful.

@vladmihalcea
Copy link
Owner

I think it's better if you send me a Pull Request with the README.md file updated with this info for Spring Boot.

bluething added a commit to bluething/playground-spring-data that referenced this issue Jul 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants