31.8.4. Lazy loading result sets
By default, when a multi-object finder or select method is executed the JDBC result set is read to the end immediately. The client receives a collection of
EJBLocalObject
or CMP field values which it can then iterate through. For big result sets this approach is not efficient. In some cases it is better to delay reading the next row in the result set until the client tries to read the corresponding value from the collection. You can get this behavior for a query using the lazy-resultset-loading
element.
<query>
<query-method>
<method-name>findAll</method-name>
</query-method>
<jboss-ql><![CDATA[select object(o) from A o]]></jboss-ql>
<lazy-resultset-loading>true</lazy-resultset-loading>
</query>
The are some issues you should be aware of when using lazy result set loading. Special care should be taken when working with a
Collection
associated with a lazily loaded result set. The first call to iterator()
returns a special Iterator
that reads from the ResultSet
. Until this Iterator
has been exhausted, subsequent calls to iterator()
or calls to the add()
method will result in an exception. The remove()
and size()
methods work as would be expected.