For update with separate select and update

Refresh

April 2019

Views

26 time

1

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
    method1();
    // some other operations need to be done here...
    // pass value to method2 and execute
    method(value);
}

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