Daniel Prince
Daniel Prince

Reputation: 9

Why my ResultSet reflect the changes made externally, even though it's TYPE_SCROLL_INSENSITIVE?

Why my Java ResultSet showing updated data when I use TYPE_SCROLL_INSENSITIVE with an external database update ?

I'm working with a MySQL database in Java, and I have a ResultSet with TYPE_SCROLL_INSENSITIVE. I'm expecting to see the changes made to the data in the database should not be reflected in my ResultSet after an external update. However, the data is showing the updated values.

Java :

Statement statement = connection.createStatement(
    ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY
);
ResultSet resultSet = statement.executeQuery("SELECT * FROM table1");

if (resultSet.next()) {
    System.out.print(resultSet.getInt("id") + " ");
    System.out.print(resultSet.getString("name") + " ");
    System.out.print(resultSet.getString("value"));
    System.out.println();
}

// Externally changing the value in database
Thread.sleep(10 * 1000);  // Simulate delay for external change

resultSet.refreshRow(); 
resultSet.beforeFirst();

if (resultSet.next()) {
    System.out.print(resultSet.getInt("id") + " ");
    System.out.print(resultSet.getString("name") + " ");
    System.out.print(resultSet.getString("value"));
    System.out.println();
}

SQL :

START TRANSACTION;
UPDATE table1 SET name = 'data100' WHERE id = 1;
COMMIT;

Actual Output : 1 data1 2800 1 data100 2800

Expected Output : 1 data1 2800 (Previous data) 1 data1 2800 (Previous Data)

I'm using ResultSet.TYPE_SCROLL_INSENSITIVE expecting that the changes made by the external transaction would be reflected in the ResultSet. I also tried adding a Thread.sleep(10 * 1000) to simulate some delay between the fetch and the external update, but the ResultSet still doesn't reflect the updated data.

Why my ResultSet reflect the changes made externally, even though it's TYPE_SCROLL_INSENSITIVE? What should I do to see the updated data in the ResultSet with TYPE_SCROLL_SENSITIVE not TYPE_SCROLL_INSENSITIVE? Is there any other configuration or approach I need to use to achieve this behavior?

Upvotes: 0

Views: 31

Answers (0)

Related Questions