Record number of result set rows in `DataSourceQueryCountListener`
Opened this issue · 2 comments
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?
+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?
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)