Trước tiên, bạn thêm for update vào lựa chọn của bạn (và các cột khác của bạn mà bạn muốn cập nhật), và sau đó bạn cập nhật chúng. Ngoài ra, như đã lưu ý trong các nhận xét, hãy đảm bảo rằng getConnection của bạn trả về một Connection không có autocommit. Và bạn cần đặt Statement nhập để cuộn và CONCUR_UPDATABLE . Một cái gì đó như,
String[] colNames = { "email", "already_linked", "account_link_timestamp" };
String query = "select " + Stream.of(colNames).collect(Collectors.joining(", "))
+ "from email_accounts where already_linked = false for update";
try (Connection conn = getConnection(); // Make sure conn.setAutoCommit(false);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query)) {
while (rs.next()) {
// Get the current values, if you need them.
String email = rs.getString(colNames[0]);
boolean linked = rs.getBoolean(colNames[1]);
Timestamp time = rs.getTimestamp(colNames[2]);
// ...
rs.updateBoolean(colNames[1], true);
rs.updateTimestamp(colNames[2], //
new Timestamp(System.currentTimeMillis()));
rs.updateRow();
}
} catch (SQLException e) {
e.printStackTrace();
}