Hello David,
--- Mikhail Krivoshein <[EMAIL PROTECTED]> wrote:
> I'm wondering why DbUtils has no simple way to combine
> ResultSetHandlers.
> In my code it is very common when I need to provide two methods: one
> retrieves List of items and another retrieve only one item.
>
> Currently I use three methods in such case:
> 1) public Object get(int id) { ... } - retrieves one row and convert it
> into JavaBean with read method.
> 2) public List list() { ... } - retrieves many rows and convert them
> into List of JavaBeans with read method also.
> 3) private Object read(ResultSet rs) { ... } - converts current
> ResultSet row data into JavaBean object.
>
> Now I'm moving to DbUtils library and want to use the same approach.
>
> I plan:
> 1) implement custom ResultSetHandler to translate ResultSet row into
> JavaBean.
> 2) use ListHandler (sources bolow) to convert ResultSet into List with
> help from ResultSetHandler from point 1.Doesn't BeanListHandler provide this exact behavior?
David
Not exactly. BeanListHandler generates List of JavaBeans.
But I may want to generate List of Strings for example. Or I may need to make some additional processing while
filling JavaBean with data.
Just simple example of code that I need to have:
========================
/**
* Method retrieves keywords as comma separated string.
*
* @param serviceId service id
* @return keywords as comma separated string.
* @throws SQLException error occurs
*/
private static String getKeywords(int serviceId) throws SQLException {
QueryRunner runner = new QueryRunner(DbConnectionManager.getDataSource());
List keywords =
(List) runner.query(
"SELECT keyword FROM keyword WHERE service_id = ?",
new Integer(serviceId),
new ListHandler(new ScalarHandler()));
return StringUtils.combineString(keywords);
}
========================
With ScalarHandler applied to individual rows and ListHandler used to combine them into List code became
very straighforward.
And I see performance problem with BasicRowProcessor because it just forwards calls to BeanProcessor that
definetely has performance problems because of heavy reflection use and absance of any metadata caching.
Assume I need to generate 5 item list about 30 times per minute (caching can't be applied).
With BeanProcessor I need to study metadata of the table 30 times!
I prefer to avoid this cost and write JavaBean <-> DB table mapping code by hands.
From my point of view clearest way is to implement one custom ResultSetHandler for each JavaBean.
And if I combine this custom ResultSetHandler with discussed ListHandler I will reuse List generation/result set iteration
code for all my handlers.
I see another solution - implement one RowProcessor for each JavaBean. But I dislike this solution and it looks less clear
than one with custom ResultSetHandlers. Also I guess it is not way how DbUtils is designed to be used.
I appreciate your help with this topic.
Best regards, Mikhail Krivoshein
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
