Delay Access to Metadata
Most
people do not realize just how expensive metadata
operations—that is, calls to
DatabaseMetaData
,
ResultSetMetaData
,
and
ParameterMetaData
methods—can be. When calling for a result
set’s metadata, for example, many database engines
need to finish pulling all rows in the current fetch batch before
they can get the metadata for that result set. Consider the following
code fragment:
conn = ds.getConnection( );
stmt = conn.prepareStatement("SELECT * FROM Person");
rs = stmt.excecuteQuery( );
md = rs.getMetaData( );
while( rs.next( ) ) {For some database engines and drivers, you will not be able to read anything from the first row until the driver retrieves all the rows from the server. Of course, you cannot avoid calling metadata. The solution is therefore to delay accessing metadata until you need it.