For update with separate select and update


April 2019


26 time


I'm facing an issue regarding for update query. I have two methods as following:

The first one select rows and return a map contains primary key and value.

public Map<String,String> method1() throws SQLException  {
    String sql = "select * from table where col1='someValue' for update";
    // ... do something and construct map, so map contains {colName, value}
    return map;

The second method takes the map from the first method, then update row based on the value of the map.

public boolean method2(String pk){
    String sql = "update tableName set value = 1234 where primaryKey = pk";
    // ... execute query and return boolean of if row affected
    return booleanValue;

In a third method, method 1 & 2 will execute.

public void method3(){
    // execute method1, get value from map
    // some other operations need to be done here...
    // pass value to method2 and execute

I did some research, and I found out some posts say using for update should not open transaction for too long.

My question is: should I take select..for update and actual update separately?

0 answers