当另一个进程同时写入同一个 table 时,Java ResultSet 的行为是什么?

What is the behavior of Java ResultSet when another process is writing to the same table at the same time?

如果我有一个简单的 Java 程序生成准备好的语句来执行 SQL like

select * from person 

和这个准备好的语句 returns a ResultSet,如果另一个进程正在向 person table 写入行,则在遍历结果集时的行为是什么同一时间?

例如,新添加的行在下面的循环中是否可见

while (resultSet.next()) {
    // Some processing 
    // Which might take a while
}

我在别处读到可以使用 setFetchSize 来控制 ResultSet 中返回的行数。

如果我使用这种方法,答案会改变吗? (假设写入行的速度比读取和处理行的速度快)

来自 Oracle 文档:

You access the data in a ResultSet object through a cursor. Note that this cursor is not a database cursor. This cursor is a pointer that points to one row of data in the ResultSet. Initially, the cursor is positioned before the first row. The method ResultSet.next moves the cursor to the next row. This method returns false if the cursor is positioned after the last row. This method repeatedly calls the ResultSet.next method with a while loop to iterate through all the data in the ResultSet.

参见:https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html


关于你的第二个问题:

What and when should I specify setFetchSize()?

所以不,设置要分配多少堆的大小不会改变语义。

原则上这取决于驱动程序的实现。然而实际上,驱动程序要么将整个结果集加载到内存中,要么使用数据库游标将更多结果提取到集合中。

在这两种情况下,并行发生的插入都是不可见的。数据库游标通常看不到创建后提交的插入。