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

Record number of result set rows in DataSourceQueryCountListener #85

Open
jochenberger opened this issue Jul 4, 2022 · 2 comments
Open

Comments

@jochenberger
Copy link

I'd like to test the number of returned rows from some JPA select queries. Would it be possible to add that to net.ttddyy.dsproxy.QueryCount?

@fretfan
Copy link

fretfan commented Jan 16, 2023

+1 for this feature. Or is there a manual way how to count number of rows returned, by intercepting how many times next() method was called?

@ttddyy
Copy link
Member

ttddyy commented Apr 15, 2023

DataSourceQueryCountListener may or may not be appropriate because it is an accumulated number by thread or global, based on the strategy.
This means, if it is thread based, the count for the ResultSet#next becomes a total of all the ResultSet reads for the thread(usually typical request-response lifecycle in servlet). So, if one request-response makes 3 select queries and each reads 10 records, the QueryCount will have count=30. I think what people more interested is a read count for each query.

The manual way of achieving this is by implementing MethodExecutionListener and reacting on the callback of ResultSet#next.
A drawback of this approach is that you need to manage the lifecycle of ResultSet open/close and associate the count to the resultset.

This is how I have done to get the row count in the micrometer observation for DataSource.
https://github.com/jdbc-observations/datasource-micrometer/blob/main/datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/DataSourceObservationListener.java#L205-L217

I think the best approach to the library is to add a callback specific to the ResultSet lifecycle.
For example, every time next is called, trigger a callback with a value that contains how many times it is called for the specific ResultSet. (actually, r2dbc-proxy has this callback.)
Also, need a callback when ResultSet#close is called to receive the total count for the next. A thing that makes it more interesting is that calling ResultSet#close is an optional operation that Statement#close implies the close of the ResultSet.
(Just a memo to myself about what needs to be done to achieve this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants