且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JDBC使用SELECT FOR UPDATE锁定一行,不起作用

更新时间:2023-01-30 14:35:47

Y你是非常困惑的,但至少你的编辑后事情看起来更好。有多种方法可以做到这一点,但我发现***的方法是实际使用JDBC的 ResultSet.update * 方法:

You are super-confused, but at least things look better after your edits. There are multiple ways to do this, but the best way I've found is to actually use JDBC's ResultSet.update* methods:

首先,您需要使用 ResultSet.CONCUR_UPDATABLE 准备 SELECT ... FOR UPDATE 语句参数,如下所示:

First, you need to prepare your SELECT ... FOR UPDATE statement with the ResultSet.CONCUR_UPDATABLE argument, like this:

ps = conn.prepareStatement(query,
                           ResultSet.TYPE_FORWARD_ONLY,
                           ResultSet.CONCUR_UPDATABLE);

然后,您必须使用ResultSet实际更新表:

Then, you have to actually update the table using the ResultSet:

if(rs.next())
{
    rs.updateString(columnIndex, "new_hostname");
    rs.updateRow();
}

第三,你可能需要使用一个我可以在你看到的交易更新。希望你的 DbUtil.close 方法不会抛出任何异常,检查null等等。另外,如果你的方法变得更复杂,你应该在那里有回滚逻辑也是。

Third, you probably need to use a transaction, which I can see in your update. Hopefully, your DbUtil.close methods won't throw any exceptions, check for null, etc. Also, if your method gets any more complicated, you should have rollback logic in there, too.

你不应该因任何原因修改 my.ini

You should not have to modify my.ini for any reason.