I have the following method that I use with Spring JDBC
public String getState() {
String stateLink = template.queryForObject(
"select state_url from state_scrape_queue where in_use = false ORDER BY scrape_timestamp NULLS FIRST LIMIT 1",
(result, rowNum) -> {
return result.getString("state_url");
});
return stateLink;
}
I can't find an example of how to do a for update with Spring JDBC. I want in_use
to be set to true using for update.
I need to use select for update since this application will be used in a multi-threaded fashion. I don't want more than one thread to get the same row and the way to prevent that is by using select for update
I was able to do this with plain JDBC, here is the question I asked how to do it with plain JDBC
select "for update" with JDBC?
Anyone know how this would be done?
FOR UPDATE
doesn't update anything, it only locks selected rows as if it was updated. So you can't set something to true usingFOR UPDATE
, you would need to execute a separateUPDATE
statement. In any case, things work the same using spring-jdbc, as it would executing statements directly. As it stands, it is unclear what you're really asking. – Morrismorrisonselect for update
locks the row and in this example will updatein_use
to true, so another thread will not get the same row. – Relievefor update
clause in a select will not update a row, it will only lock the row so concurrent transactions can't update and - depending on the lock model - can't read that row. Your assumption that it will changein_use
to true is wrong. – Morrismorrisonselect for update
using plain JDBC. https://mcmap.net/q/1616988/-select-quot-for-update-quot-with-jdbc I have used it for a very long time and it works perfectly. Two threads never got the same row. I just need to do the same thing with Spring JDBC – Relieve@Transactional
, if not, the transaction has been committed and the locks released at the timequeryForObject
returns. – MorrismorrisongetState
to set autoCommit to false, then call the select for update to lock the row, then do the update and then commit – RelieveUPDATE
for this directly? – Morrismorrison