Phit

Scrollable Result Sets: How to Create and Use Them

· 6 min read · Exercise How-To

In this article
  1. What a Scrollable Result Set Actually Is
  2. How to Create One: Step by Step
  3. Driver Support and the Insensitive/Sensitive Distinction
  4. Common Problems and How to Avoid Them
  5. Scrollable Result Sets vs. SQL-Level Paging
  6. Safety and Resource Management
  7. Frequently Asked Questions

To create a scrollable result set in JDBC, you request it when preparing a statement: call Connection.createStatement(int resultSetType, int resultSetConcurrency) or Connection.prepareStatement(String sql, int resultSetType, int resultSetConcurrency) with ResultSet.TYPE_SCROLL_INSENSITIVE or ResultSet.TYPE_SCROLL_SENSITIVE as the type, and ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE as the concurrency. The driver then returns a ResultSet that supports moving backward and forward, absolute positioning, and relative positioning rather than only forward iteration.

Scrollable result sets are a JDBC feature, but the concept applies to any database cursor that lets you move around the result set instead of streaming it once. This article explains the mechanics, the trade-offs, and the practical details you need to use them correctly.

What a Scrollable Result Set Actually Is

A standard JDBC ResultSet is forward-only by default: you call next() repeatedly and can only move from the first row to the last. A scrollable result set adds a cursor that can move in both directions. The JDBC API exposes this through methods such as previous(), first(), last(), beforeFirst(), afterLast(), absolute(int row), and relative(int rows). You can also ask where the cursor is with getRow() and test boundaries with isBeforeFirst(), isAfterLast(), and isFirst().

Two type constants control scrollability:

  • ResultSet.TYPE_FORWARD_ONLY — the default; cursor moves forward only.
  • ResultSet.TYPE_SCROLL_INSENSITIVE — scrollable, but the result set does not reflect changes made to the underlying data after it was created.
  • ResultSet.TYPE_SCROLL_SENSITIVE — scrollable and intended to reflect changes, though driver support varies widely.

The concurrency constant controls whether you can update rows through the result set: CONCUR_READ_ONLY or CONCUR_UPDATABLE. Most applications use read-only scrollable result sets.

How to Create One: Step by Step

The exact code depends on your language and driver, but the JDBC pattern is consistent. Here is the sequence for a Java application using JDBC:

  1. Obtain a Connection from your data source or connection pool.
  2. Decide on the result set type. Use TYPE_SCROLL_INSENSITIVE unless you specifically need sensitive behavior and your driver supports it reliably.
  3. Decide on concurrency. Use CONCUR_READ_ONLY unless you need to update rows through the result set.
  4. Create the statement with the type and concurrency arguments: Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  5. Execute the query to get the ResultSet: ResultSet rs = stmt.executeQuery("SELECT id, name FROM customers");
  6. Navigate with first(), last(), absolute(n), relative(n), previous(), or next() as needed.
  7. Close the result set, statement, and connection in a finally block or try-with-resources.

For prepared statements, the overload is conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY). Some drivers also accept a third argument for holdability, but that is separate from scrollability.

Driver Support and the Insensitive/Sensitive Distinction

Not every JDBC driver supports every combination. The JDBC specification requires drivers to support TYPE_FORWARD_ONLY with CONCUR_READ_ONLY, but scrollable types are optional. A driver may downgrade your request: if you ask for TYPE_SCROLL_SENSITIVE and the driver only supports insensitive scrolling, it can return an insensitive result set. You can check what you actually got with rs.getType() and rs.getConcurrency().

The sensitive/insensitive distinction matters in practice. An insensitive scrollable result set is essentially a snapshot: once created, it does not see rows inserted or updated by other transactions, even if you scroll back to the same position. A sensitive result set is supposed to reflect such changes, but implementations differ, and many drivers either do not support it or implement it partially. For most reporting, paging, and UI grid scenarios, insensitive is the safer and more predictable choice.

Common Problems and How to Avoid Them

Scrollable result sets are convenient but can cause trouble if used carelessly:

  • Memory use. To scroll backward, the driver often has to buffer rows client-side. A query returning millions of rows can exhaust heap memory. Add a WHERE clause, a LIMIT, or paginate at the SQL level instead.
  • Cursor holdability. Some databases close cursors at transaction commit. If you need to scroll after a commit, check Connection.getHoldability() and consider HOLD_CURSORS_OVER_COMMIT where supported.
  • Downgraded types. As noted, a driver may return a forward-only result set even when you asked for scrollable. Always verify with getType() if your code depends on scrolling.
  • Concurrency surprises. An updatable scrollable result set can behave differently across drivers; updates may not be visible until you call refreshRow() or move the cursor.
  • Isolation and locking. Scrolling does not change transaction isolation. Long-lived scrollable result sets can hold locks or consume database resources; close them promptly.

Scrollable Result Sets vs. SQL-Level Paging

A scrollable result set lets the client move around a result, but it does not reduce the amount of data the database sends. For large datasets, SQL-level paging is usually better: use LIMIT and OFFSET (or a keyset pagination approach) to fetch only the rows you need for the current page. Scrollable result sets are most useful when the result is modest in size and the application needs random access, such as a small grid that supports jumping to the last row or sorting client-side.

Another alternative is a server-side cursor with scroll support, which some databases offer natively. That keeps memory on the server but still requires the driver to expose the scrolling methods. The JDBC API is designed to abstract these differences, but performance characteristics vary by database.

Safety and Resource Management

Always close result sets, statements, and connections. A scrollable result set may hold server-side resources longer than a forward-only one because the cursor remains open while you navigate. Use try-with-resources in Java or the equivalent in your language. Avoid holding a scrollable result set open across user interactions or long-running operations; instead, read the rows you need into memory and close the result set. Also be mindful of transaction boundaries: scrolling does not commit or roll back anything, and an open result set inside a transaction can keep locks in place.

Frequently Asked Questions

Can I make an existing ResultSet scrollable after creation?

No. Scrollability is determined when the statement is created. You must request a scrollable type before executing the query; you cannot convert a forward-only result set into a scrollable one.

What is the difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE?

Insensitive means the result set does not see changes made to the underlying data after it was created. Sensitive is intended to reflect such changes, but driver support is inconsistent, so insensitive is more predictable.

Do scrollable result sets work with all databases?

Most major JDBC drivers support at least insensitive scrollable result sets, but support is not guaranteed by the JDBC specification. Check your driver documentation and verify with ResultSet.getType().

Why does my scrollable result set still behave like forward-only?

The driver may have downgraded your request because it does not support the combination you asked for. Verify the actual type with getType() and consult the driver documentation for supported combinations.

Are scrollable result sets bad for performance?

They can be, especially for large results, because the driver may buffer rows to support backward movement. For large datasets, prefer SQL-level paging and forward-only result sets.

Can I update rows through a scrollable result set?

Yes, if you request CONCUR_UPDATABLE and the driver supports it. You can then call updateString(), updateInt(), and similar methods followed by updateRow(). Support and behavior vary by driver.