Commons DbUtils
  1. Commons DbUtils
  2. DBUTILS-37

BeanListHandler#handle(ResultSet) is not optimal

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2
    • Labels:
      None

      Description

      I use the BeanListHandler for huge ResultSets (about 1000000 rows), and I searched through the code to see if I could gain little time.

      It appeared to me that the following code - in BeanProcessor.class - was executed too many times:

              PropertyDescriptor[] props = this.propertyDescriptors(type);
              ResultSetMetaData rsmd = rs.getMetaData();
              int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
      

      for the following reason.
      Since BeanListHandler extends GenericListHandler, the method #handle(ResultSet) calls #handleRow(ResultSet) for each row in the ResultSet,
      which in the case of a BeanListHandler, calls RowProcessor#toBean(ResultSet, Class),
      which itself calls BeanProcessor#toBean(ResultSet, Class).

      A very simple way to make the BeanListHandler#handle(ResultSet) method faster is to override the GenericListHandler#handle(ResultSet) method by this code:

          public Object handle(ResultSet rs) throws SQLException {
              return this.convert.toBeanList(rs, type);
          }
      

      This way, the code I showed would be called only once, as it would not call BeanProcessor#toBean(ResultSet, Class) for each row but BeanProcessor#toBeanList(ResultSet, Class).

      1. OptimalBeanListHandler.java
        3 kB
        Julien Aymé
      2. OptimalBeanListHandler.patch
        2 kB
        Julien Aymé

        Activity

        Hide
        Julien Aymé added a comment -

        The new OptimalBeanListHandler class (whole file and patch).

        With the change I propose, the BeanListHandler#handleRow(ResultSet) method is never called anymore. So, any subclass of BeanListHandler which would override this method would not work properly anymore.

        In order to prevent this from happening, I preferred to create a new class (named OptimalBeanListHandler - created from BeanListHandler with svn copy), which would implements ResultSetHandler directly rather than extends GenericListHandler.

        Show
        Julien Aymé added a comment - The new OptimalBeanListHandler class (whole file and patch). With the change I propose, the BeanListHandler#handleRow(ResultSet) method is never called anymore. So, any subclass of BeanListHandler which would override this method would not work properly anymore. In order to prevent this from happening, I preferred to create a new class (named OptimalBeanListHandler - created from BeanListHandler with svn copy), which would implements ResultSetHandler directly rather than extends GenericListHandler.
        Hide
        Dan Fabulich added a comment -

        I don't think it's necessary to create a separate OptimalBeanListHandler... I think nobody should be using the old implementation.

        I checked in a change to the "bugfixing" branch in revision 743097 that just replaces the old implementation with this new faster implementation.

        Show
        Dan Fabulich added a comment - I don't think it's necessary to create a separate OptimalBeanListHandler... I think nobody should be using the old implementation. I checked in a change to the "bugfixing" branch in revision 743097 that just replaces the old implementation with this new faster implementation.
        Hide
        Henri Yandell added a comment -

        svn ci -m "Merging in Dab Fabulich's work on https://svn.apache.org/repos/asf/commons/sandbox/dbutils/bugfixing from -r741987:747723. Resolving DBUTILS-34 - DBUTILS-37 - DBUTILS-29 - DBUTILS-14 - DBUTILS-31 - DBUTILS-39 - DBUTILS-41 - DBUTILS-44 - DBUTILS-33 - DBUTILS-42 - DBUTILS-40"

        Sending pom.xml
        Sending src/java/org/apache/commons/dbutils/BasicRowProcessor.java
        Sending src/java/org/apache/commons/dbutils/BeanProcessor.java
        Sending src/java/org/apache/commons/dbutils/QueryRunner.java
        Adding src/java/org/apache/commons/dbutils/handlers/AbstractListHandler.java
        Sending src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java
        Sending src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java
        Sending src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java
        Deleting src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java
        Sending src/java/org/apache/commons/dbutils/handlers/MapListHandler.java
        Sending src/test/org/apache/commons/dbutils/BaseTestCase.java
        Adding src/test/org/apache/commons/dbutils/QueryRunnerTest.java
        Transmitting file data .........
        Committed revision 747724.

        Show
        Henri Yandell added a comment - svn ci -m "Merging in Dab Fabulich's work on https://svn.apache.org/repos/asf/commons/sandbox/dbutils/bugfixing from -r741987:747723. Resolving DBUTILS-34 - DBUTILS-37 - DBUTILS-29 - DBUTILS-14 - DBUTILS-31 - DBUTILS-39 - DBUTILS-41 - DBUTILS-44 - DBUTILS-33 - DBUTILS-42 - DBUTILS-40 " Sending pom.xml Sending src/java/org/apache/commons/dbutils/BasicRowProcessor.java Sending src/java/org/apache/commons/dbutils/BeanProcessor.java Sending src/java/org/apache/commons/dbutils/QueryRunner.java Adding src/java/org/apache/commons/dbutils/handlers/AbstractListHandler.java Sending src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java Sending src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java Sending src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java Deleting src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java Sending src/java/org/apache/commons/dbutils/handlers/MapListHandler.java Sending src/test/org/apache/commons/dbutils/BaseTestCase.java Adding src/test/org/apache/commons/dbutils/QueryRunnerTest.java Transmitting file data ......... Committed revision 747724.

          People

          • Assignee:
            Dan Fabulich
            Reporter:
            Julien Aymé
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development