Saturday, 23 August 2014

JDBC : TYPE_SCROLL_INSENSITIVE Vs. TYPE_SCROLL_SENSITIVE ResultSet

Mastering Java & Database Concepts?

Subscribe to our YouTube channel for simple, practical, and beginner-friendly programming tutorials!

SUBSCRIBE NOW

Introduction to Scrollable ResultSets

In JDBC, a standard ResultSet cursor moves strictly forward, row by row. However, JDBC also allows you to create scrollable ResultSets that let you navigate backwards, jump to specific rows, or move relative to your current position.

When working with scrollable cursors, JDBC offers two distinct types:

  • ResultSet.TYPE_SCROLL_INSENSITIVE
  • ResultSet.TYPE_SCROLL_SENSITIVE

While both allow bi-directional scrolling, they handle live database updates and performance in fundamentally different ways.

Understanding TYPE_SCROLL_INSENSITIVE

A TYPE_SCROLL_INSENSITIVE ResultSet creates a static snapshot of the query data at the moment it is executed.

  • Snapshot of Data: The data returned represents a frozen state of the database rows matching your query condition.
  • Changes Not Visible: If another transaction or user modifies or updates the underlying records in the database table while the ResultSet remains open, those changes will not be reflected.
  • Better Performance: Whenever you invoke getXXX() methods, data is retrieved directly from local memory/snapshot rather than making round trips to the database.

Understanding TYPE_SCROLL_SENSITIVE

In contrast, a TYPE_SCROLL_SENSITIVE ResultSet does not hold a full snapshot. Instead, it maintains dynamic pointers to the rows matching your query.

  • Live Row Pointers: It references active rows that satisfy the SQL criteria.
  • Changes Are Visible: If underlying records are modified by another process while your ResultSet is still active, those updates are immediately visible when reading the row again.
  • Performance Overhead: Because every read operation may require verification or a round trip to the database server, it involves higher overhead and slower execution compared to insensitive scroll.

Comparison Summary

Feature TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE
Internal Model Static snapshot of data Dynamic row pointers
Live Updates Database changes are NOT reflected Database changes ARE reflected
Performance Faster (reads from local snapshot) Slower (requires round trips to database)
Driver Support Widely supported across JDBC drivers Varies; depends on database and driver support

Related Tutorials You Might Like


Click here to watch in Youtube : https://www.youtube.com/watch?v=_7aJ77RKTtI

Click the below Image to Enlarge
JDBC : TYPE_SCROLL_INSENSITIVE Vs. TYPE_SCROLL_SENSITIVE ResultSet
See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Tutorials