How to Check if a Database Supports Scrollable Result Sets
In this article
You can determine whether a database supports scrollable result sets by checking its JDBC driver's DatabaseMetaData method supportsResultSetType(int type) for ResultSet.TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE, or by consulting the driver's documentation for supported result set types. In practice, most modern relational databases (PostgreSQL, MySQL, Oracle, SQL Server) support scrollable result sets at the driver level, but the exact behavior depends on the driver version and configuration.
What Scrollable Result Sets Are
A scrollable result set is a JDBC ResultSet that allows moving the cursor both forward and backward, as well as jumping to an absolute or relative row position. By default, many JDBC drivers return forward-only result sets, which are more memory-efficient because they stream rows from the server. Scrollable result sets, by contrast, often require the driver to buffer rows client-side or use server-side cursors that support random access.
JDBC defines three result set types:
TYPE_FORWARD_ONLY– cursor moves forward only.TYPE_SCROLL_INSENSITIVE– cursor can scroll; the result set does not reflect changes made to the underlying data after it was created.TYPE_SCROLL_SENSITIVE– cursor can scroll; the result set may reflect changes made to the underlying data.
Scrollability is separate from concurrency (read-only vs. updatable). A result set can be scrollable and read-only, which is common.
How to Check Programmatically with JDBC
The most reliable way to check support at runtime is through DatabaseMetaData. The method supportsResultSetType(int type) returns true if the driver supports the given type. Here is a typical sequence:
- Obtain a
Connectionobject from your data source. - Call
connection.getMetaData()to get aDatabaseMetaDatainstance. - Invoke
metaData.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)andmetaData.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE). - Optionally, check
supportsResultSetConcurrency(int type, int concurrency)to see if a scrollable result set can also be updatable.
Note that a driver may report support for a type but still fall back to a less capable type when you request it. To be certain, create a statement with the desired type and concurrency, execute a simple query, and inspect the actual type via resultSet.getType(). If it returns TYPE_FORWARD_ONLY when you asked for scrollable, the driver downgraded your request.
Checking Documentation and Driver Behavior
Driver documentation is the authoritative source for supported result set types. For example, the PostgreSQL JDBC driver documentation lists supported types and notes that scrollable result sets are implemented by fetching all rows into memory unless a server-side cursor is used. MySQL Connector/J supports scrollable result sets but may require useCursorFetch=true or specific statement settings for large result sets. Oracle JDBC supports scrollable result sets natively, and SQL Server's JDBC driver supports them with certain limitations.
Always verify the driver version, because support can change between releases. Also consider connection properties that affect cursor behavior, such as holdability and fetchSize.
Common Problems and Misconceptions
A frequent misconception is that if a database supports scrollable result sets, every query will return one. In reality, the driver may downgrade the type based on the statement, the query, or the connection settings. For instance, some drivers return forward-only result sets for stored procedure calls or when the query contains certain constructs.
Another issue is performance. Scrollable result sets often require buffering the entire result set in memory, which can cause out-of-memory errors for large queries. Some drivers use server-side cursors to avoid this, but those cursors may have their own limitations, such as requiring a transaction to remain open.
Also, scrollable result sets are not the same as updatable result sets. You can have a scrollable, read-only result set. If you need to update rows via the result set, check supportsResultSetConcurrency separately.
How to Choose the Right Result Set Type
Use forward-only result sets for simple sequential processing, as they are the most efficient. Choose scrollable insensitive result sets when you need to navigate back and forth but do not require live updates. Scrollable sensitive result sets are rarely needed and may not be fully supported; use them only if your application genuinely requires seeing changes made by others during iteration.
- Forward-only: best for large result sets and streaming.
- Scrollable insensitive: good for small to medium result sets where random access is needed.
- Scrollable sensitive: use with caution; support is inconsistent.
When in doubt, test with your actual driver and database version. Write a small program that requests each type and prints the actual type returned.
Alternatives to Scrollable Result Sets
If scrollable result sets are not supported or are too memory-intensive, consider these alternatives:
- Fetch the entire result set into an application-level collection (e.g., a list) and navigate that.
- Use pagination with
LIMITandOFFSET(or equivalent) to retrieve rows in chunks. - Use a server-side cursor with forward-only access and cache rows as needed.
- For updatable needs, use separate UPDATE statements rather than updatable result sets.
These approaches often provide better control over memory and performance.
Frequently Asked Questions
Does MySQL support scrollable result sets?
Yes, MySQL Connector/J supports scrollable result sets, but the driver may buffer the entire result set in memory. For large result sets, consider using forward-only with pagination.
How can I tell if my driver actually returned a scrollable result set?
After executing a query, call resultSet.getType(). If it returns TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE, the result set is scrollable. If it returns TYPE_FORWARD_ONLY, the driver downgraded your request.
Are scrollable result sets supported in all databases?
Most major relational databases support them at the driver level, but support varies by driver and version. Always check the driver documentation and test with your specific setup.
What is the difference between scrollable and updatable result sets?
Scrollable refers to cursor movement (forward, backward, absolute). Updatable refers to whether you can modify rows through the result set. They are independent properties; a result set can be scrollable but not updatable.
Why does my scrollable result set cause out-of-memory errors?
Many drivers implement scrollable result sets by buffering all rows in memory. For large queries, this can exhaust heap space. Use forward-only result sets or server-side cursors with pagination instead.
Can I use scrollable result sets with connection pooling?
Yes, but be aware that some connection pools or drivers may reset result set types. Always verify the actual type after obtaining a connection from the pool.