我对 TYPE_SCROLL_SENSITIVE
类型的 ResultSet
的行为感到困惑。
我的理解是:
- 我执行了一个返回结果集的选择查询。我打印出第一行中特定列的值。
- 然后我执行
Thread.sleep(10000)
,这会使程序暂停 10 秒。 - 当程序处于 hibernate 状态时,我手动对数据库中的同一列进行了更新(通过 SQL 提示符)。
- 10 秒后,我再次打印结果集第一行中同一列的值。
在第 4 步中,我希望打印的列值与第 1 步中打印的值不同。但我总是得到相同的值(即使我的 ResultSet
是 SCROLL_TYPE_SENSITIVE 类型
)。
我是不是误解了什么?
下面是我使用的代码。
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 ?
是的。您必须通过自己启动 SELECT
或调用 ResultSet.refreshRow()
来再次获取表的最新状态。此外,请阅读 ResultSet.refreshRow()
的文档在使用它之前,否则您可能会得到意想不到的结果。
有关 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.
这仅仅意味着它会对同一 ResultSet 对象中其他人所做的更改敏感。要理解这个概念,我建议看看这个官方 JDBC Tutorial: Updating Tables .
好的,编辑我的帖子以包含原始教程中的特定行,
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.
关于java - ResultSet.TYPE_SCROLL_SENSITIVE 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2091659/