I am confused about the behaviour of a ResultSet
that is of type TYPE_SCROLL_SENSITIVE
.
My understanding of this is:
Thread.sleep(10000)
, which halts the program for 10 seconds.In step 4, I expect the printed column value to be different from the value printed in step 1. But I always get the same value (even if my ResultSet
is of type SCROLL_TYPE_SENSITIVE
).
Am I misunderstanding something here ?
Below is the code I use.
private void doStuff() throws Exception
{
final String query = "select * from suppliers where sup_id=420";
Statement stmt = this.con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query);
rs.next();
System.out.println("City : " + rs.getString("city"));
Thread.sleep(10000); // While this executes, I do a manual update !
System.out.println("City : " + rs.getString("city"));
}
Am I mis-understanding something here ?
Yes. You must fetch again to get the latest state of the table, either by firing up a SELECT
yourself, or calling ResultSet.refreshRow()
. Moreover, read the docs of ResultSet.refreshRow()
before using it, otherwise you might get unexpected results.
The doc states regarding TYPE_SCROLL_SENSITIVE,
TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others.
Which merely means that it would be sensitive to the changes made by others in the same ResultSet object. To understand the concept, I would advise to look at this official JDBC Tutorial: Updating Tables.
Okay, editing my post to include the specific line from the original tutorial,
With a scrollable result set, you can move to rows you want to change, and if the type is TYPE_SCROLL_SENSITIVE, you can get the new value in a row after you have changed it.