Difference between ResultSet TYPE_SCROLL_SENSITIVE and TYPE_SCROLL_INSENSITIVE

gstackoverflow picture gstackoverflow · Aug 30, 2014 · Viewed 11.6k times · Source

I am trying to understand the difference between these two methods of creating a statement:

1:

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

2:

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

second argument are same but first - different

from java doc:

resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE

and

TYPE_SCROLL_INSENSITIVE
The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.


TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet.

Thus I want to show difference between sensitivity of underlies the ResultSet. I want to understand what this "sensitive to changes to the data that underlies the ResultSet" (from Javadoc) means.

Please provide an example that demonstrates and explains the difference.

Answer

Filip Nguyen picture Filip Nguyen · Aug 30, 2014

Sensitivness is with regards to the underlying data (a database).

Suppose you have PEOPLE table in a database. You create insensitive statement:

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

and at time 8:20 you issue a query

SELECT * FROM PEOPLE;

now you leave the result set open and you scroll through it using next(), previous() and absolute(int)) methods.

At 8:23 somebody updates data in PEOPLE table.

At 8:24 you are still scrolling the result set but because you have INSENSITIVE result set, you see old data.

Now comes the difference. If you had created the statement with SENSITIVE then you would see all the changes that were being done at 8:23.